<!-- 带有二级菜单的layout -->
<template>
  <div class="component-container home-container">
    <aside class="aside">
      <header class="aside__header"></header>
      <main class="aside__main">
        <el-menu :default-active="selectedKeys" :default-openeds="openKeys" router class="el-menu-vertical">
          <template v-for="item in subMenus" :key="item.name">
            <el-sub-menu v-if="item.children" :index="item.path">
              <template #title>
                <BottomMenuItem :item="item" style="margin-left: 0px" />
              </template>

              <div v-for="child in item.children" :key="child.name">
                <el-sub-menu v-if="child.children?.length > 0" :index="child.path">
                  <template #title>
                    <BottomMenuItem :item="child" />
                  </template>
                  <el-menu-item v-for="ch in child.children" :key="ch.name" :index="ch.path">
                    <BottomMenuItem :item="ch" style="font-size: 14px" />
                  </el-menu-item>
                </el-sub-menu>
                <el-menu-item :index="child.path" v-else>
                  <BottomMenuItem :item="child" />
                </el-menu-item>
              </div>
            </el-sub-menu>
            <el-menu-item v-else :index="item.path">
              <BottomMenuItem :item="item" style="margin-left: 0px" />
            </el-menu-item>
          </template>
        </el-menu>
      </main>
    </aside>
    <div class="main">
      <slot></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch, computed } from 'vue';
  import { useRoute } from 'vue-router';
  import BottomMenuItem from '../../components/menu-tree/BottomMenuItem.vue';
  // 当前路由
  const currentRoute = useRoute();
  const selectedKeys = ref<string>('');

  const openKeys = ref<string[]>([]);

  function filterHiddenItems(arr: any[]): any[] {
    return arr.filter((item) => {
      if (item.meta && item.meta.hidden) {
        return false;
      }
      if (item.children && Array.isArray(item.children)) {
        item.children = filterHiddenItems(item.children);
      }
      return true;
    });
  }

  const subMenus = computed(() => {
    return filterHiddenItems(currentRoute.matched[0].children);
  });

  function collectMenuPaths(list: any[]): string[] {
    const paths: string[] = [];
    list.forEach((item) => {
      if (item?.path) {
        paths.push(item.path);
      }
      if (item?.children?.length) {
        paths.push(...collectMenuPaths(item.children));
      }
    });
    return paths;
  }

  function resolveSelectedKey(path: string, activeMenu?: string) {
    const menuPaths = collectMenuPaths(subMenus.value || []);
    if (activeMenu && menuPaths.includes(activeMenu)) {
      return activeMenu;
    }
    if (menuPaths.includes(path)) {
      return path;
    }

    // 回退到最深的前缀菜单路径
    const prefixMatched = menuPaths.filter((menuPath) => path.startsWith(menuPath)).sort((a, b) => b.length - a.length);
    if (prefixMatched.length > 0) {
      return prefixMatched[0];
    }

    return activeMenu || path;
  }

  // 跟随页面路由变化，切换菜单选中状态
  watch(
    () => currentRoute.fullPath,
    () => {
      const matched = currentRoute.matched;
      openKeys.value = matched.map((item) => item.name) as string[];
      const activeMenu: string = (currentRoute.meta?.activeMenu as string) || '';
      selectedKeys.value = resolveSelectedKey(currentRoute.path as string, activeMenu);
    },
    {
      immediate: true,
    },
  );
</script>
<style lang="scss">
  .aside__main {
    .el-menu-item,
    .el-sub-menu {
      svg {
        color: $primary-color;
      }
    }
    .el-menu-item {
      &.is-active {
        svg {
          color: #fff;
        }
      }
    }
    .el-sub-menu.is-active .el-sub-menu__title {
      color: $primary-color;
    }
  }
</style>
<style scoped lang="scss">
  :deep(.el-menu-item) {
    transition: unset;
  }
  .home-container {
    display: flex;
    gap: 10px;
    padding: 10px;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }
  .aside {
    display: flex;
    flex-direction: column;
    width: 260px;
    height: 100%;
    min-height: 0;
    flex-shrink: 0;
    border-radius: 4px;
    background-color: $white-color;
    overflow-x: hidden;
    overflow-y: auto;
    &__header {
      width: inherit;
      height: 10px;
      flex-shrink: 0;
    }
    &__main {
      width: inherit;
      flex: 1;
      min-height: 0;
      overflow-y: auto;
      overflow-x: auto;
    }
  }
  .main {
    flex: 1;
    min-height: 0;
    overflow: auto;
    border-radius: 4px;
  }
  .el-menu-vertical {
    width: 100%;
    min-height: 100%;
    border: none;
    border-radius: 4px;
    .el-menu-item,
    :deep(.el-sub-menu__title) {
      font-size: 16px;
      color: #333;
    }
    .el-menu-item.is-active {
      color: $white-color;
      background-color: $primary-color;
    }
  }

  :deep(.el-sub-menu__title),
  :deep(.el-menu-item) {
    display: flex;
    align-items: center;
    gap: 12px;
  }
</style>
