<template>
  <div class="analysis-manage">
    <!-- 顶部标签导航 -->
    <el-tabs
      v-model="activeMenu"
      type="card"
      class="page-tab"
      @tab-click="handleTabClick"
    >
      <el-tab-pane v-for="tab in tabs" :key="tab.name" :name="tab.name">
        <template #label>
          <el-button :type="activeMenu === tab.name ? 'primary' : 'default'">{{
            tab.label
          }}</el-button>
        </template>
      </el-tab-pane>
    </el-tabs>

    <!-- 子页面内容区域 -->
    <router-view />
  </div>
</template>

<script setup lang="ts">
  import { ref, watch, onMounted } from 'vue';
  import { useRoute, useRouter } from 'vue-router';

  defineOptions({
    name: 'AnalysisManage',
  });

  const route = useRoute();
  const router = useRouter();

  // 标签页配置
  const tabs = [
    { name: 'TotalAnalysis', label: '总量分析' },
    { name: 'SegmentAnalysis', label: '分段统计' },
    { name: 'CollegeAnalysis', label: '学院分析' },
    { name: 'TeacherAnalysis', label: '任课老师分析' },
    { name: 'ClassAnalysis', label: '班级分析' },
    { name: 'ObjectiveQuestionAnalysis', label: '客观题分析' },
    { name: 'SubjectiveQuestionAnalysis', label: '主观题分析' },
    { name: 'BigQuestionAnalysis', label: '大题分析' },
  ];

  // 当前激活的菜单项
  const activeMenu = ref<string>('');

  // 标签点击处理
  function handleTabClick(tab: any) {
    const tabName = tab.paneName || tab.props.name;
    if (tabName !== route.name) {
      router.push({ name: tabName });
    }
  }

  // 监听路由变化，更新激活菜单
  watch(
    () => route.name,
    (newName) => {
      activeMenu.value = newName as string;
    },
    { immediate: true }
  );

  // 组件挂载时初始化
  onMounted(() => {
    // 如果当前路径是父路由，默认跳转到总量分析
    if (route.name === 'AnalysisManage') {
      router.replace({ name: 'TotalAnalysis' });
    } else {
      activeMenu.value = route.name as string;
    }
  });
</script>
