﻿<template>
  <div class="layout">
    <aside class="sidebar">
      <div class="logo">
        <div class="logo-title">新材料数据库</div>
        <div class="logo-sub">管理系统</div>
      </div>
      <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="/dictionary">材料分类管理</el-menu-item>
        <el-menu-item index="/materials">材料管理</el-menu-item>
        <el-menu-item v-if="isAdmin" index="/screen/overview">数据大屏</el-menu-item>
      </el-menu>
    </aside>
    <main class="main">
      <header class="topbar">
        <div class="breadcrumb">{{ 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>
      <section class="content">
        <router-view />
      </section>
    </main>
  </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 titleMap = {
  '/users': '用户管理',
  '/factories': '工厂管理',
  '/dictionary': '材料分类管理',
  '/materials': '材料管理'
}
const title = computed(() => titleMap[`/${route.path.split('/')[1]}`] || '系统首页')
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;
  min-height: 100vh;
}

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

.logo {
  padding: 20px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.logo-title {
  font-size: 18px;
  font-weight: 600;
}

.logo-sub {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
}

.menu {
  border-right: none;
  background: transparent;
  color: #fff;
}

: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);
}

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

.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);
}

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

.user-info {
  text-align: right;
}

.content {
  flex: 1;
  background: var(--bg);
}

.breadcrumb {
  font-weight: 600;
}
</style>
