<template>
  <el-row class="tac">
    <el-col :span="24">
      <el-menu
        class="el-menu-vertical-demo"
        @open="handleOpen"
        @close="handleClose"
        :collapse="sidebar"
        :active-text-color="'#1890FF'"
        :default-active="activeMenu"
        :collapse-transition="false"
        :router="true"
      >
        <template v-for="(item, index) in filteredRouterList" :key="item.path">
          <el-sub-menu
            :index="item.path"
            v-if="item.children && item.children.length > 0"
            class="submenu-box"
          >
            <template #title>
              <!-- <el-icon><location /></el-icon> -->
               <img class="menu-icon" :src="getIcon(item.meta.icon)" alt="">
              <span>{{getMenuTitle(item)}}</span>
            </template>
            <el-menu-item 
              v-for="(item_son, index_son) in item.children"
              :key="item_son.path"
              :index="item_son.path"
              class="submenu-title-noDropdown"
            >
              <span>{{getMenuTitle(item_son)}}</span>
            </el-menu-item>
          </el-sub-menu>
          <el-menu-item 
            v-else 
            :index="item.path"  
            class="submenu-box"
          >
            <!-- <el-icon><setting /></el-icon> -->
            <img class="menu-icon" :src="getIcon(item.meta.icon)" alt="">
            <span>{{getMenuTitle(item)}}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-col>
  </el-row>
</template>

<script setup>
import { computed, ref, onMounted } from 'vue';
import {routes} from "../router/index.js";
import { useRoute, useRouter } from 'vue-router';
import { menuStore } from '@/pinia/menu.js';
import { storeToRefs } from 'pinia';
import closeIcon from '@/assets/menu/closeIcon.png';
import collectorList from '@/assets/menu/collectorList.png';
import dashboard from '@/assets/menu/dashboard.png';
import deviceManage from '@/assets/menu/deviceManage.png';
import dustOverview from '@/assets/menu/dustOverview.png';
import monitor from '@/assets/menu/monitor.png';
import warnning from '@/assets/menu/warnning.png';


const route = useRoute();
const router = useRouter();
const { sidebar } = storeToRefs(menuStore());

const routerList = routes[0].children;
const menuListData = ref(JSON.parse(localStorage.getItem("menuList")) || []);

const getIcon = (iconName) => {
  switch (iconName) {
    case 'closeIcon':
      return closeIcon;
    case 'collectorList':
      return collectorList;
      case 'dashboard':
      return dashboard;
      case 'deviceManage':
      return deviceManage;
      case 'dustOverview':
      return dustOverview;
      case 'monitor':
      return monitor;
      case 'warnning':
      return warnning;
  }
}
// 递归过滤路由
const filterRoutes = (routes) => {
  if (!routes) return [];
  
  return routes.filter(route => {
    // 检查当前路由是否在菜单列表中
    const isInMenuList = menuListData.value.some(menuItem => 
      menuItem.name === route.meta?.title && 
      menuItem.url === route.path
    );

    // 如果当前路由有子路由，递归过滤子路由
    if (route.children && route.children.length > 0) {
      const filteredChildren = filterRoutes(route.children);
      // 如果子路由有匹配项，保留父路由并更新子路由
      if (filteredChildren.length > 0) {
        route.children = filteredChildren;
        return true;
      }
    }

    // 如果当前路由在菜单列表中，或者有匹配的子路由，则保留
    return isInMenuList;
  });
};

// 过滤后的路由列表
const filteredRouterList = computed(() => {
  if (!menuListData.value || menuListData.value.length === 0) {
    return [];
  }
  return filterRoutes(routerList);
});

onMounted(() => {
});

const activeMenu = computed(() => route.path);

const handleOpen = (key, keyPath) => {
};

const handleClose = (key, keyPath) => {
};

const getMenuTitle = (item) => {
  return item.meta && item.meta.title || '未命名菜单'
}
</script>
<style scoped lang="scss">
  a {
  text-decoration: none;
  color: #fff;
  font-weight: 400;
}

.submenu-box {
  background: #181d21;
  color: #fff !important;
  span, a, i {
    color: #fff !important;
  }
  .menu-icon {
    width: 16px;
    // height: 18px;
    margin-right: 5px;
  }
}


.is-active {
  color: #fff !important;
  // background: #03a1b3 !important;
  background: linear-gradient(225deg, rgba(128, 255, 178, .4), rgba(58, 120, 255, .1) 100%, #0049fa 0);
  span {
    color: #fff !important;
  }
}


  .is-active {
    .el-submenu__title {
      color: #FFFFFFFF !important;
      // background: #03A1B3FF !important;

      .el-icon-arrow-down {
        color: #FFFFFFFF;
      }
      .svg-icon {
        opacity: 1;
      }
    }
  }
  .ns-active {
    background-color: #fff !important;
    span {
      color: #055f7c !important;
    }
  }
</style>

<style lang="scss">

.el-menu {
  background: none !important;
}

.el-sub-menu .el-icon  {
  color: #fff !important;
}

.el-menu-item:hover, .el-sub-menu__title:hover {
  color: #fff !important;
  background: linear-gradient(225deg, rgba(128, 255, 178, .6), rgba(58, 120, 255, .1) 100%, #0049fa 0) !important;
  span {
    color: #fff !important;
  }
}
// menu hover
.submenu-title-noDropdown,
.el-submenu__title {
  // background: linear-gradient(225deg, rgba(128, 255, 178, .2), rgba(58, 120, 255, .1) 100%, #0049fa 0) !important;
  color: #FFFFFF !important;
  &:hover {
    background: linear-gradient(225deg, rgba(128, 255, 178, .6), rgba(58, 120, 255, .1) 100%, #0049fa 0) !important;
  }
}

</style>