<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
  Collection,
  DataAnalysis,
  OfficeBuilding,
  Operation,
  Reading,
  Tickets,
  Calendar,
  CircleCheck,
  DocumentChecked,
  AlarmClock,
  User,
  UserFilled,
} from '@element-plus/icons-vue'
import { useAuthStore } from '../stores/auth'

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const collapsed = ref(false)
const mobileMenu = ref(false)
const workspaceLabel = computed(() => {
  const roles = auth.user?.roles ?? []
  if (roles.includes('SuperAdmin')) return '系统全域管理'
  if (roles.includes('AcademicAdmin')) return '校级教务管理'
  if (roles.includes('CollegeAdmin')) return '学院教务管理'
  if (roles.includes('Counselor')) return '辅导员班级工作'
  if (roles.includes('Teacher')) return '教师教学工作'
  if (roles.includes('Student')) return '学生学业服务'
  return '教务工作台'
})

const pageTitle = computed(() => {
  const titles: Record<string, string> = {
    dashboard: '教务总览',
    'base-data': '基础数据',
    personnel: '人员档案',
    courses: '课程库',
    curriculum: '培养方案',
    'teaching-tasks': '教学任务',
    schedules: '排课与课表',
    'course-selections': '选课与教学班',
    grades: '成绩与学业档案',
    exams: '考试与考场',
    users: '用户与权限',
  }
  return titles[String(route.name)] ?? '教务管理'
})

function signOut() {
  auth.logout()
  router.push('/login')
}

onMounted(() => auth.refresh().catch(() => undefined))
</script>

<template>
  <div class="shell" :class="{ 'is-collapsed': collapsed }">
    <aside class="sidebar" :class="{ 'is-mobile-open': mobileMenu }">
      <div class="brand">
        <div class="brand-mark" aria-hidden="true">
          <span v-for="index in 9" :key="index" />
        </div>
        <div v-if="!collapsed">
          <strong>明序教务</strong>
          <small>ACADEMIC OFFICE</small>
        </div>
      </div>

      <div v-if="!collapsed" class="term-stamp">
        <span>当前工作区</span>
        <b>{{ workspaceLabel }}</b>
      </div>

      <el-menu
        router
        :default-active="route.path"
        :collapse="collapsed"
        class="nav-menu"
        @select="mobileMenu = false"
      >
        <el-menu-item index="/dashboard">
          <el-icon><DataAnalysis /></el-icon>
          <template #title>教务总览</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin'].includes(role))"
          index="/base-data"
        >
          <el-icon><OfficeBuilding /></el-icon>
          <template #title>基础数据</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'CollegeAdmin', 'Counselor'].includes(role))"
          index="/personnel"
        >
          <el-icon><UserFilled /></el-icon>
          <template #title>人员档案</template>
        </el-menu-item>
        <el-menu-item index="/courses">
          <el-icon><Collection /></el-icon>
          <template #title>课程库</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'CollegeAdmin'].includes(role))"
          index="/curriculum"
        >
          <el-icon><Reading /></el-icon>
          <template #title>培养方案</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'CollegeAdmin'].includes(role))"
          index="/teaching-tasks"
        >
          <el-icon><Tickets /></el-icon>
          <template #title>教学任务</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin'].includes(role))"
          index="/schedules"
        >
          <el-icon><Calendar /></el-icon>
          <template #title>排课与课表</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'CollegeAdmin', 'Student'].includes(role))"
          index="/course-selections"
        >
          <el-icon><CircleCheck /></el-icon>
          <template #title>{{ auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'CollegeAdmin'].includes(role)) ? '选课管理' : '学生选课' }}</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'CollegeAdmin', 'Teacher', 'Student'].includes(role))"
          index="/grades"
        >
          <el-icon><DocumentChecked /></el-icon>
          <template #title>{{ auth.user?.roles.includes('Student') ? '学业成绩' : '成绩管理' }}</template>
        </el-menu-item>
        <el-menu-item
          v-if="auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'Teacher', 'Student'].includes(role))"
          index="/exams"
        >
          <el-icon><AlarmClock /></el-icon>
          <template #title>{{ auth.user?.roles.includes('Student') ? '我的考试' : auth.user?.roles.includes('Teacher') ? '我的监考' : '考试管理' }}</template>
        </el-menu-item>
        <el-menu-item v-if="auth.isSuperAdmin" index="/users">
          <el-icon><User /></el-icon>
          <template #title>用户与权限</template>
        </el-menu-item>
      </el-menu>

      <div v-if="!collapsed" class="phase-note">
        <span>第一阶段 · 核心可用版</span>
        <p>教学运行、选课与成绩管理已就绪</p>
      </div>
    </aside>

    <div v-if="mobileMenu" class="mobile-mask" @click="mobileMenu = false" />

    <main class="main-area">
      <header class="topbar">
        <button class="menu-toggle desktop-only" type="button" @click="collapsed = !collapsed">
          <el-icon><Operation /></el-icon>
        </button>
        <button class="menu-toggle mobile-only" type="button" @click="mobileMenu = true">
          <el-icon><Operation /></el-icon>
        </button>
        <div class="page-heading">
          <span>教务工作台</span>
          <h1>{{ pageTitle }}</h1>
        </div>
        <div class="user-block">
          <div class="avatar">{{ auth.user?.displayName?.slice(0, 1) ?? '管' }}</div>
          <div class="user-copy">
            <b>{{ auth.user?.displayName ?? '系统管理员' }}</b>
            <span>{{ auth.user?.roles?.[0] ?? '教务人员' }}</span>
          </div>
          <el-button text @click="signOut">退出</el-button>
        </div>
      </header>
      <section class="content">
        <RouterView />
      </section>
    </main>
  </div>
</template>
