<template>
  <div class="admin-dashboard">
    <!-- 顶部图片 -->
    <div class="top-image-container">
      <img src="/images/teacher/顶部.png" alt="顶部图片" class="top-image">

    </div>

    <div class="main-content">
      <!-- 侧边栏 -->
      <div class="sidebar-container">
        <!-- 头像 -->
        <div class="avatar-container">
          <img src="https://picsum.photos/200/200" alt="头像" class="avatar">
          <div class="avatar-text">
            用户昵称~
          </div>
        </div>

        <!-- 导航栏 -->
        <div class="nav-container">
          <router-link to="/teacher/course-management" class="nav-item" :class="{ active: activeNavItem === 0 }"
            @click="setActiveNavItem(0)">
            <img :src="activeNavItem === 0 ? '/images/teacher/课程管理(选中).png' : '/images/teacher/课程管理.png'" alt="">
            <span>课程管理</span>
          </router-link>
          
          <!-- 考试管理 - 可展开菜单 -->
          <div class="nav-item" :class="{ active: activeNavItem === 4 }" @click="toggleExamMenu">
            <img :src="activeNavItem === 4 ? '/images/teacher/练考通-选中.png' : '/images/teacher/练考通.png'" alt="">
            <span>考试管理</span>
            <n-icon class="expand-icon" :class="{ expanded: examMenuExpanded }">
              <ChevronDownOutline />
            </n-icon>
          </div>
          
          <!-- 考试管理子菜单 -->
          <div class="submenu-container" :class="{ expanded: examMenuExpanded }">
            <router-link 
              to="/teacher/exam-management/question-management" 
              class="submenu-item" 
              :class="{ active: activeSubNavItem === 'question-management' }"
              @click="setActiveSubNavItem('question-management')"
            >
              <span>试题管理</span>
            </router-link>
            <router-link 
              to="/teacher/exam-management/exam-library" 
              class="submenu-item" 
              :class="{ active: activeSubNavItem === 'exam-library' }"
              @click="setActiveSubNavItem('exam-library')"
            >
              <span>试卷管理</span>
            </router-link>
            <router-link 
              to="/teacher/exam-management/marking-center" 
              class="submenu-item" 
              :class="{ active: activeSubNavItem === 'marking-center' }"
              @click="setActiveSubNavItem('marking-center')"
            >
              <span>阅卷中心</span>
            </router-link>
          </div>
          
          <router-link to="/teacher/student-management" class="nav-item" :class="{ active: activeNavItem === 1 }"
            @click="setActiveNavItem(1)">

            <img :src="activeNavItem === 1 ? '/images/teacher/学院管理(选中).png' : '/images/teacher/学员管理.png'" alt="">
            <span>学员管理</span>
          </router-link>
          <router-link to="/teacher/my-resources" class="nav-item" :class="{ active: activeNavItem === 2 }"
            @click="setActiveNavItem(2)">
            <img :src="activeNavItem === 2 ? '/images/teacher/我的资源(选中).png' : '/images/teacher/我的资源.png'" alt="">
            <span>我的资源</span>
          </router-link>
          <router-link to="/teacher/personal-center" class="nav-item" :class="{ active: activeNavItem === 3 }"
            @click="setActiveNavItem(3)">

            <img :src="activeNavItem === 3 ? '/images/teacher/个人中心(选中).png' : '/images/teacher/个人中心.png'" alt="">
            <span>个人中心</span>
          </router-link>
        </div>
      </div>

      <!-- 右侧路由视图 -->
      <div class="router-view-container">
        <!-- 面包屑 -->
        <div class="breadcrumb">
          <span class="breadcrumb-separator"></span>
          <n-breadcrumb>
            <n-breadcrumb-item v-for="(item, index) in breadcrumbItems" :key="index" :href="item.path">
              {{ item.title }}
            </n-breadcrumb-item>
          </n-breadcrumb>
        </div>
        <router-view></router-view>
      </div>
    </div>

  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, watch } from 'vue'
import { useRoute } from 'vue-router'
import { ChevronDownOutline } from '@vicons/ionicons5'

const width = window.innerWidth;
const height = window.innerHeight;
console.log(`当前屏幕宽度: ${width}px, 高度: ${height}px`);

// 添加导航项激活状态管理
const activeNavItem = ref(0); // 0: 课程管理, 1: 学员管理, 2: 我的资源, 3: 个人中心, 4: 考试管理
const activeSubNavItem = ref(''); // 子菜单激活状态
const examMenuExpanded = ref(false); // 考试管理菜单展开状态
const route = useRoute();

const setActiveNavItem = (index: number) => {
  activeNavItem.value = index;
  // 如果不是考试管理，关闭考试管理子菜单
  if (index !== 4) {
    examMenuExpanded.value = false;
    activeSubNavItem.value = '';
  }
}

// 考试管理菜单切换
const toggleExamMenu = () => {
  examMenuExpanded.value = !examMenuExpanded.value;
  activeNavItem.value = 4;
  
  // 如果展开且没有选中子菜单，默认选中第一个
  if (examMenuExpanded.value && !activeSubNavItem.value) {
    activeSubNavItem.value = 'question-management';
  }
}

// 设置子菜单激活状态
const setActiveSubNavItem = (subItem: string) => {
  activeSubNavItem.value = subItem;
  activeNavItem.value = 4;
  examMenuExpanded.value = true;
}

// 动态生成面包屑
const breadcrumbItems = computed(() => {
  // 获取当前路由的matched数组
  const matchedRoutes = route.matched;

  // 处理matchedRoutes，过滤掉'管理后台'并生成面包屑项
  return matchedRoutes
    .filter(item => item.meta.title !== '管理后台')
    .map(item => ({
      title: item.meta.title || '未知页面',
      path: item.path
    }));
});

// 监听路由变化，更新激活的导航项
onMounted(() => {
  // 初始设置
  updateActiveNavItem();
});

// 使用watch监听路由变化
watch(route, () => {
  updateActiveNavItem();
});

// 更新激活的导航项
const updateActiveNavItem = () => {
  const path = route.path;
  if (path.includes('course-management')) {
    activeNavItem.value = 0; // 课程管理
    examMenuExpanded.value = false;
    activeSubNavItem.value = '';
  } else if (path.includes('student-management')) {
    activeNavItem.value = 1; // 学员管理
    examMenuExpanded.value = false;
    activeSubNavItem.value = '';
  } else if (path.includes('my-resources')) {
    activeNavItem.value = 2; // 我的资源
    examMenuExpanded.value = false;
    activeSubNavItem.value = '';
  } else if (path.includes('personal-center')) {
    activeNavItem.value = 3; // 个人中心
    examMenuExpanded.value = false;
    activeSubNavItem.value = '';
  } else if (path.includes('exam-management')) {
    activeNavItem.value = 4; // 考试管理
    examMenuExpanded.value = true;
    
    // 根据具体路径设置子菜单激活状态
    if (path.includes('question-management')) {
      activeSubNavItem.value = 'question-management';
    } else if (path.includes('exam-library')) {
      activeSubNavItem.value = 'exam-library';
    } else if (path.includes('marking-center')) {
      activeSubNavItem.value = 'marking-center';
    } else {
      // 默认选中第一个子菜单
      activeSubNavItem.value = 'question-management';
    }
  }
}

</script>

<style scoped>
.top-image-container {
  position: relative;
  width: 100%;
  height: 130px;
  overflow: hidden;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .top-image-container {
    height: 100px;
  }
}

@media screen and (max-width: 480px) {
  .top-image-container {
    height: 80px;
  }
}

.top-image {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0px;
  top: 0px;
  opacity: 100%;
  object-fit: cover;
}

.main-content {
  display: flex;
}

@media screen and (max-width: 768px) {
  .main-content {
    flex-direction: column;
  }
  
  .sidebar-container {
    width: 100%;
    height: auto;
    min-height: 300px;
  }
  
  .router-view-container {
    height: auto;
    min-height: calc(100vh - var(--top-height, 100px) - 300px);
  }
}

.sidebar-container {
  width: 294px;
  height: calc(100vh - var(--top-height, 130px));
  background: #FFFFFF;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

@media screen and (max-width: 768px) {
  .sidebar-container {
    --top-height: 100px;
  }
}

@media screen and (max-width: 480px) {
  .sidebar-container {
    --top-height: 80px;
    width: 240px;
  }
}

.avatar-container {
  height: 230px;
  position: relative;
  /* 移除原来的边框 */
}

/* 添加带间距的横线 */
.avatar-container::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 20px;
  right: 20px;
  height: 1px;
  background-color: #E6E6E6;
}

.avatar-container img {
  width: 95px;
  height: 95px;
  /* 圆角 */
  border-radius: 50%;
  margin-top: 55px;
  margin-left: 100px;
  margin-right: 99;
}

.avatar-text {
  margin-left: 98px;
  height: 31px;
  font-family: AppleSystemUIFont;
  font-size: 22px;
  color: #000000;
  line-height: 26px;
  text-align: left;
  font-style: normal;
  text-transform: none;
}

@media screen and (max-width: 768px) {
  .avatar-container {
    height: 180px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  
  .avatar-container img {
    margin: 0 0 15px 0;
  }
  
  .avatar-text {
    margin: 0;
    text-align: center;
  }
}

@media screen and (max-width: 480px) {
  .avatar-container {
    height: 150px;
  }
  
  .avatar-container img {
    width: 80px;
    height: 80px;
  }
  
  .avatar-text {
    font-size: 18px;
  }
}

.nav-container {
  margin-top: 30px;
  /* 鼠标变小手 */
  cursor: pointer;

}

.nav-container .nav-item {
  margin-left: 20px;
  width: 254px;
  height: 54px;
  margin-bottom: 20px;
  /* 圆角 */
  border-radius: 10px;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
  gap: 8px;
}

@media screen and (max-width: 768px) {
  .nav-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 15px;
  }
  
  .nav-container .nav-item {
    width: 200px;
    margin: 0 10px 15px;
  }
  
  .submenu-container {
    margin-left: 10px;
    width: 200px;
  }
  
  .submenu-item {
    margin-left: 20px;
    padding-left: 30px;
    font-size: 14px;
  }
}

@media screen and (max-width: 480px) {
  .nav-container .nav-item {
    width: 150px;
    height: 45px;
    margin: 0 5px 10px;
  }
  
  .nav-container .nav-item img {
    margin-left: 20px;
  }
  
  .submenu-container {
    margin-left: 5px;
    width: 150px;
  }
  
  .submenu-item {
    margin-left: 10px;
    padding-left: 25px;
    font-size: 13px;
    height: 35px;
  }
  
  .expand-icon {
    margin-right: 10px;
  }
}

.nav-container .nav-item:hover {
  background: rgba(102, 183, 227, 0.05);
}

/* 添加激活状态样式 */
.nav-container .nav-item.active {
  background: rgba(102, 183, 227, 0.1);
}

.nav-container .nav-item.active span {
  color: #0C99DA;
}

.nav-container .nav-item img {
  height: 20px;
  margin-left: 50px;
  margin-top: 0;
  margin-right: 5px;
}

.nav-container .nav-item span {
  width: 80px;
  height: 28px;
  font-family: AppleSystemUIFont;
  font-size: 20px;
  color: #666666;
  line-height: 23px;
  text-align: left;
  font-style: normal;
  text-transform: none;
}

/* 展开图标样式 */
.expand-icon {
  margin-left: auto;
  margin-right: 20px;
  transition: transform 0.3s ease;
  color: #666;
}

.expand-icon.expanded {
  transform: rotate(180deg);
}

/* 子菜单容器 */
.submenu-container {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.3s ease-out;
  margin-left: 20px;
  width: 254px;
}

.submenu-container.expanded {
  max-height: 200px;
}

/* 子菜单项样式 */
.submenu-item {
  display: flex;
  align-items: center;
  height: 40px;
  margin-bottom: 8px;
  margin-left: 30px;
  padding-left: 40px;
  border-radius: 8px;
  transition: all 0.3s ease;
  text-decoration: none;
  color: #666;
  font-size: 16px;
  position: relative;
}

.submenu-item:hover {
  background: rgba(102, 183, 227, 0.05);
}

.submenu-item.active {
  background: rgba(102, 183, 227, 0.1);
  color: #0C99DA;
}

/* 子菜单项前的小圆点 */
.submenu-item::before {
  content: '';
  width: 6px;
  height: 6px;
  background-color: #ccc;
  border-radius: 50%;
  position: absolute;
  left: 20px;
  transition: background-color 0.3s ease;
}

.submenu-item.active::before {
  background-color: #0C99DA;
}

.router-view-container {
  flex: 1;
  padding: 20px;
  background: #F5F7FA;
  height: calc(100vh - var(--top-height, 130px));
}

@media screen and (max-width: 768px) {
  .router-view-container {
    --top-height: 100px;
    padding: 15px;
  }
}

@media screen and (max-width: 480px) {
  .router-view-container {
    --top-height: 80px;
    padding: 10px;
  }
}

.breadcrumb {
  display: flex;
  align-items: center;
}

.breadcrumb-separator {
  width: 4px;
  height: 17px;
  margin-right: 10px;
  background-color: #0C99DA;
}
</style>
