<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { useRoute, useRouter, type RouteRecordRaw } from 'vue-router'
import { useSettingsStore } from '@/store/settings'
import { useAppStore } from '@/store/app'
import { storeToRefs } from 'pinia'
import SidebarItem from './SidebarItem.vue'
import Logo from './Logo.vue'

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

const settingsStore = useSettingsStore()
const appStore = useAppStore()
const { sidebar } = storeToRefs(appStore)
const { sidebarLogo } = storeToRefs(settingsStore)
const routes = computed(() => {
  // 递归过滤路由，包括子路由
  
  const filterHiddenRoutes = (routes: RouteRecordRaw[]): RouteRecordRaw[] => {
    return routes.filter((route: any) => {
      // console.log(route,'每一项');
      // 如果当前路由标记为 hidden，则过滤掉
      if (route.meta?.hidden || route.hidden) {
        return false;
      }
      // 如果有子路由，递归过滤子路由
      if (route.children && route.children.length > 0) {
        route.children = filterHiddenRoutes(route.children);
        // 如果过滤后子路由为空，且父路由没有 component，则过滤掉整个路由
        if (route.children.length === 0 && !route.component) {
          return false;
        }
      }
      return true;
    });
  };
  return filterHiddenRoutes([...router.options.routes]);
});

const activeMenu = computed(() => {
  const { meta, path } = route
  // 如果设置了 path，侧边栏会高亮显示你设置的路径
  if (meta.activeMenu) {
    return meta.activeMenu as string
  }
  return path
})

const isCollapse = computed(() => !sidebar.value.opened)

onMounted(()=>{
})
</script>

<template>
  <div class="sidebar-container-wrapper">
    <logo v-if="sidebarLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        background-color="#304156"
        text-color="#bfcbd9"
        :unique-opened="false"
        active-text-color="#409eff"
        :collapse-transition="false"
        mode="vertical"
        class="el-menu-vertical-demo"
      >
        <sidebar-item
          v-for="route in routes"
          :key="route.path"
          :item="route"
          :base-path="route.path"
          :sidebarStatus="sidebar.opened" 
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<style lang="scss" scoped>
@use "@/styles/variables" as vars;
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.sidebar-container-wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
  margin-left: -6px;
  // 主题定制
  :deep(.el-menu) {
    border: none;
    height: 100%;
    width: 100% !important;
    
  }

  :deep(.submenu-title-noDropdown:hover), :deep(.el-submenu__title:hover) {
    background-color: vars.$menuHover !important;
  }

  :deep(.is-active > .el-submenu__title) {
    color: vars.$subMenuActiveText !important;
  }
  
  .el-menu-item, .el-sub-menu {
    background-color: transparent !important;
  }
}

.scrollbar-wrapper {
  flex: 1;
  overflow-x: hidden !important;
}
</style>
