<template>
  <div class="layout">
    <header class="topbar">
      <div class="logo-title">房地产新材料选材管理数据系统</div>
      <div class="user">
        <div class="user-info">
          <div class="name">{{ user?.username || '用户' }}</div>
          <div class="role">{{ isAdmin ? '管理员' : '普通账号' }}</div>
        </div>
        <el-button size="small" @click="openPassword">修改密码</el-button>
        <el-button size="small" @click="onLogout">退出</el-button>
      </div>
    </header>
    <div class="body">
      <aside class="sidebar">
        <el-menu
          :default-active="active"
          class="menu"
          router
        >
          <el-menu-item v-if="isAdmin" index="/users">用户管理</el-menu-item>
          <el-menu-item index="/factories">供应商库</el-menu-item>
          <el-menu-item v-if="isAdmin" index="/brands">品牌库</el-menu-item>
          <el-menu-item index="/materials">材料管理</el-menu-item>
          <el-menu-item v-if="isAdmin" index="/screen/overview">数据大屏</el-menu-item>
          <el-sub-menu v-if="isAdmin" index="config">
            <template #title>配置项</template>
            <el-menu-item index="/dictionary">材料分类</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </aside>
      <main class="main">
        <section class="content">
          <router-view />
        </section>
      </main>
    </div>
  </div>

  <el-dialog v-model="passwordVisible" title="修改密码" width="420px" class="dialog-scroll">
    <el-form :model="passwordForm" label-width="90px">
      <el-form-item label="原密码" required>
        <el-input v-model="passwordForm.old_password" type="password" show-password />
      </el-form-item>
      <el-form-item label="新密码" required>
        <el-input v-model="passwordForm.new_password" type="password" show-password />
      </el-form-item>
      <el-form-item label="确认密码" required>
        <el-input v-model="passwordForm.new_password_confirm" type="password" show-password />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="passwordVisible = false">取消</el-button>
      <el-button type="primary" @click="onChangePassword">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed, reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuth } from '@/store/auth'
import { changePassword } from '@/api/auth'

const route = useRoute()
const router = useRouter()
const { state, isAdmin, clearAuth } = useAuth()

const active = computed(() => route.path)
const user = computed(() => state.user)

const passwordVisible = ref(false)
const passwordForm = reactive({
  old_password: '',
  new_password: '',
  new_password_confirm: ''
})

const openPassword = () => {
  passwordForm.old_password = ''
  passwordForm.new_password = ''
  passwordForm.new_password_confirm = ''
  passwordVisible.value = true
}

const onChangePassword = async () => {
  try {
    await changePassword({ ...passwordForm })
    ElMessage.success('密码已更新')
    passwordVisible.value = false
  } catch (error) {
    ElMessage.error(error.response?.data?.detail || '修改失败')
  }
}

const onLogout = () => {
  clearAuth()
  router.push('/login')
}
</script>

<style scoped>
.layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.topbar {
  height: 64px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #fff;
  box-shadow: 0 12px 24px rgba(15, 26, 42, 0.08);
  flex-shrink: 0;
  z-index: 10;
}

.logo-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--brand-900, #1b2a41);
  white-space: nowrap;
}

.user {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-info {
  text-align: right;
  line-height: 1.2;
}

.user-info .name {
  font-weight: 600;
}

.user-info .role {
  font-size: 12px;
  color: #6b7785;
}

.body {
  flex: 1;
  display: flex;
  min-height: 0;
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: linear-gradient(180deg, var(--brand-900), var(--brand-950));
  color: #fff;
  display: flex;
  flex-direction: column;
}

.menu {
  border-right: none;
  background: transparent;
  color: #fff;
  padding-top: 12px;
}

:deep(.el-menu-item) {
  color: #fff;
  transition: all 0.2s ease;
}

:deep(.el-menu-item.is-active) {
  color: #7cb4e3;
  background-color: rgba(255, 255, 255, 0.12);
  box-shadow: inset 3px 0 0 #7cb4e3;
}

:deep(.el-menu-item:hover) {
  color: #fff;
  background-color: rgba(255, 255, 255, 0.08);
}

:deep(.el-sub-menu__title) {
  color: #fff;
  transition: all 0.2s ease;
}

:deep(.el-sub-menu__title:hover) {
  color: #fff;
  background-color: rgba(255, 255, 255, 0.08);
}

:deep(.el-sub-menu.is-active > .el-sub-menu__title) {
  color: #7cb4e3;
}

:deep(.el-sub-menu .el-menu) {
  background: transparent;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.content {
  flex: 1;
  background: var(--bg);
  min-height: 0;
  overflow: auto;
}
</style>
