<template>
  <div class="admin-layout">
    <header class="admin-header">
      <div class="header-brand">
        <div class="brand-mark">建材</div>
        <div class="brand-text">
          <span class="brand-title">Offer 招聘管理平台</span>
          <span class="brand-sub">ADMIN CONSOLE</span>
        </div>
      </div>
      <div class="header-meta"></div>
      <el-dropdown>
        <div class="header-user">
          <div class="user-avatar">{{ initial }}</div>
          <span class="user-email">{{ auth.user?.email }}</span>
          <el-icon><ArrowDown /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="logout">退出登入</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </header>
    <div class="admin-body">
      <aside class="admin-aside">
        <el-menu
          router
          :default-active="$route.path"
          background-color="transparent"
          text-color="#4b5563"
          active-text-color="#C60C1E"
        >
          <div class="aside-label">业务管理</div>
          <el-menu-item index="/admin/dashboard">
            <el-icon><TrendCharts /></el-icon><span>数据概览</span>
          </el-menu-item>
          <el-menu-item index="/admin/jobs">
            <el-icon><Briefcase /></el-icon><span>职位管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/applications">
            <el-icon><Document /></el-icon><span>投递管理</span>
          </el-menu-item>
          <template v-if="auth.isSuperAdmin">
            <div class="aside-label">系统设置</div>
            <el-menu-item index="/admin/organizations">
              <el-icon><OfficeBuilding /></el-icon><span>组织架构</span>
            </el-menu-item>
            <el-menu-item index="/admin/users">
              <el-icon><User /></el-icon><span>用户管理</span>
            </el-menu-item>
          </template>
          <div class="aside-label">其他</div>
          <el-menu-item index="/home">
            <el-icon><HomeFilled /></el-icon><span>返回首页</span>
          </el-menu-item>
        </el-menu>
      </aside>
      <main class="admin-main">
        <router-view />
      </main>
    </div>
  </div>
</template>
<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ArrowDown, TrendCharts, Briefcase, Document, OfficeBuilding, User, HomeFilled } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const auth = useAuthStore()

const initial = computed(() => (auth.user?.email || 'A').slice(0, 1).toUpperCase())

function logout() {
  localStorage.clear()
  ElMessage.success('已退出登入')
  router.push({ name: 'Login' })
}
</script>
<style scoped>
.admin-layout {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* —— 顶栏：央企红通栏 —— */
.admin-header {
  height: 58px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 20px;
  background: linear-gradient(180deg, #C60C1E 0%, #a30815 100%);
  border-bottom: 2px solid var(--adm-gold);
  box-shadow: 0 2px 8px rgba(138, 7, 18, 0.25);
  position: relative;
  z-index: 2;
}
.header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background: #fff;
  color: var(--adm-red);
  font-weight: 800;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(255, 255, 255, 0.6);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-title { color: #fff; font-weight: 700; font-size: 16px; letter-spacing: 0.02em; }
.brand-sub { color: rgba(255, 255, 255, 0.7); font-size: 10px; letter-spacing: 0.18em; }

.header-meta { flex: 1; }

.header-user {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px 4px 4px;
  border-radius: 4px;
  cursor: pointer;
  color: #fff;
  transition: background 0.18s ease;
}
.header-user:hover { background: rgba(255, 255, 255, 0.12); }
.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  background: var(--adm-gold);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.user-email { font-size: 13px; }

.admin-body {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* —— 侧栏：白底 + 红色选中 —— */
.admin-aside {
  width: 214px;
  flex-shrink: 0;
  background: #fff;
  border-right: 1px solid var(--adm-line);
  display: flex;
  flex-direction: column;
  padding: 6px 0 12px;
  overflow-y: auto;
}
.aside-label {
  padding: 16px 18px 6px;
  font-size: 11px;
  letter-spacing: 0.14em;
  color: #9ca3af;
  font-weight: 600;
}
.aside-label:first-child { padding-top: 8px; }

.admin-aside :deep(.el-menu) {
  border-right: none;
  background: transparent;
}
.admin-aside :deep(.el-menu-item) {
  height: 42px;
  line-height: 42px;
  margin: 2px 10px;
  border-radius: 4px;
  padding: 0 14px !important;
  font-size: 14px;
  font-weight: 500;
  color: #4b5563;
  transition: background 0.16s ease, color 0.16s ease;
  position: relative;
  display: flex;
  align-items: center;
}
.admin-aside :deep(.el-menu-item .el-icon) {
  font-size: 17px;
  margin-right: 10px;
  color: #9ca3af;
  transition: color 0.16s ease;
}
.admin-aside :deep(.el-menu-item:hover) {
  background: var(--adm-red-bg);
  color: var(--adm-red);
}
.admin-aside :deep(.el-menu-item:hover .el-icon) { color: var(--adm-red); }
.admin-aside :deep(.el-menu-item.is-active) {
  background: var(--adm-red-bg);
  color: var(--adm-red);
  font-weight: 700;
}
.admin-aside :deep(.el-menu-item.is-active .el-icon) { color: var(--adm-red); }
.admin-aside :deep(.el-menu-item.is-active::before) {
  content: '';
  position: absolute;
  left: -10px;
  top: 8px;
  bottom: 8px;
  width: 3px;
  border-radius: 0 2px 2px 0;
  background: var(--adm-red);
}

/* —— 内容区 —— */
.admin-main {
  flex: 1;
  overflow-y: auto;
  padding: 18px 22px;
  background: var(--adm-page);
}
</style>
