<template>
  <el-aside class="siderbar_container" :width="isCollapse ? '72px' : '150px'">
    <div @click="toggleCollapse" class="collapse_box">
      <img v-if="isCollapse" src="@/assets/icon/open.svg" alt="">
      <img v-else src="@/assets/icon/close.svg" alt="">
    </div>
    <el-menu :collapse="isCollapse" router :default-active="activeMenuRoute">
      <el-menu-item v-for="item in menuItems" :key="item.route" :index="item.route">
        <template #default>
          <i class="iconfont" :class="item.icon"></i>
          <span v-if="!isCollapse">{{ item.title }}</span>
        </template>
        <template #title>
          <span v-if="isCollapse">{{ item.title }}</span>
        </template>
      </el-menu-item>
    </el-menu>
  </el-aside>
</template>
<script lang="ts">
export default {
  name: 'SiderBar'
}
</script>
<script lang="ts" setup> 
import { ref, watch, onMounted, computed } from 'vue';
import { useRoute } from 'vue-router'
import router from '@/router';

const route = useRoute()
const activeRoute = ref(route.path)
watch(() => route.path, (newVal) => {
  activeRoute.value = newVal
})

// 当前应选中的菜单项：精确匹配优先，否则匹配路由前缀（处理详情页等子页面高亮父级菜单）
const activeMenuRoute = computed(() => {
  if (!menuItems.value) return activeRoute.value
  const exact = menuItems.value.find(item => item.route === activeRoute.value)
  if (exact) return exact.route
  const prefix = menuItems.value.find(item => item.route && activeRoute.value.startsWith(item.route))
  return prefix?.route || activeRoute.value
})

const isCollapse = ref(false)
interface menuItem {
  label: string;
  title: string;
  icon: string;
  route: string;
}
const menuItems = ref<menuItem[]>()

onMounted(() => {
  const menuListStr = localStorage.getItem('menuList')
  const userInfoStr = localStorage.getItem('userInfo')

  if (menuListStr) {
    menuItems.value = JSON.parse(menuListStr)

    // 运维人员只显示学校管理和用户查询，不显示日志监控和业务监控
    // 先过滤掉后端可能返回的日志监控和业务监控
    menuItems.value = menuItems.value?.filter(menu =>
      menu.route !== '/main/logMonitor' && menu.route !== '/main/performanceMonitor'
    )

    // 通过 userInfo 判断是否为运维人员
    // 如果是运维账号（没有管理员相关菜单或特定字段），则不添加日志监控和业务监控
    const userInfo = userInfoStr ? JSON.parse(userInfoStr) : {}
    const isOpsUser = userInfo?.userType === 'ops' || userInfo?.roleType === 'ops'

    // 判断是否为管理员账号（有运维管理或联考管理菜单）
    const hasAdminMenu = menuItems.value?.some(menu =>
      menu.route === '/main/manager' || menu.route === '/main/examination'
    )

    console.log('isOpsUser:', isOpsUser, 'hasAdminMenu:', hasAdminMenu)

    if (!isOpsUser && hasAdminMenu) {
      // 非运维人员且有管理员菜单，添加日志监控和业务监控
      const additionalMenuList = [
        {
          icon: "icon_xuexiaoguanli",
          route: "/main/logMonitor",
          title: "日志监控",
          label:'',
        },
        {
          icon: "icon_xuexiaoguanli",
          route: "/main/performanceMonitor",
          title: "业务监控",
          label:'',
        },
      ]

      additionalMenuList.forEach(item => {
        const exists = menuItems.value?.some(menu => menu.route === item.route)
        if (!exists) {
          menuItems.value?.push(item)
        }
      })
    }
  }

});
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<style lang="scss" scoped>
.siderbar_container {
  background: #fff;
  padding: 16px;
}
.el-menu {
  width: 100%;
  border: none;
}
.el-menu-item {
  padding: 0;
}
.iconfont {
  margin-right: 5px;
  display: inline-block;
  color: #666666;
  font-size: 20px;
}
.collapse_box {
  padding-left: 10px;
  // text-align: center;
}
.el-menu.el-menu--vertical :deep(.el-menu-item) {
  height: 40px;
  margin: 10px 0;
  padding: 0 10px;
  border-radius: 4px;
  .iconfont {
    margin-right: 8px;
    text-align: center;
  }
}
.el-menu--collapse .el-menu-item span {
  display: none;
}
.el-menu-item :deep(.el-menu-tooltip__trigger) {
  padding: 0 10px;
}
.el-menu-item {
  &.is-active,
  &.is-active:hover {
    background: #2E64FA;
    color: #fff;
    .iconfont {
      color: #fff;
    }
  }
  &:not(.is-active):hover {
    background: inherit;
    color: #2E64FA;
    .iconfont {
      color: #2E64FA;
    }
  }
}
</style>