<template>
  <el-row class="tac">
    <el-col :span="24">
      <el-menu
        class="el-menu-vertical-demo"
        @open="handleOpen"
        @close="handleClose"
      >
        <template v-for="(item, index) in routerList" :key="item.path">
          <el-sub-menu
            :index="index + ''"
            v-if="item.children && item.children.length > 0"
            class="submenu-box"
          >
            <template #title>
              <el-icon><location /></el-icon>
              <span>{{getMenuTitle(item)}}</span>
            </template>
              <el-menu-item v-for="(item_son, index_son) in item.children" :key="item_son.path" :index="index + '_' + index_son"
              @click="handleClick(item_son.path)">
                <span>{{getMenuTitle(item_son)}}</span>
                <!-- <router-link :to="item.path + '/' + item_son.path">{{getMenuTitle(item_son)}}</router-link> -->
              </el-menu-item>
          </el-sub-menu>
          <el-menu-item v-else :index="index + ''"  class="submenu-box"  @click="handleClick(item.path)">
            <el-icon><setting /></el-icon>
            <span>{{getMenuTitle(item)}}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-col>
  </el-row>
</template>

<script setup>
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from "@element-plus/icons-vue";
import {routes} from "../router/index.js";
import { useRouter } from 'vue-router'

const router = useRouter()

const handleClick = (path) => {
  console.log(path);
  if (path.startsWith('/')) {
    router.push(path) // 绝对路径，直接跳转
  } else {
    router.push('/' + path) // 相对路径，补全
  }
}

const routerList = routes[0].children;
const handleOpen = (key, keyPath) => {
  console.log(key, keyPath);
};
const handleClose = (key, keyPath) => {
  console.log(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: linear-gradient(225deg, rgba(128, 255, 178, .2), rgba(58, 120, 255, .1) 100%, #0049fa 0);
  color: #fff !important;
  span, a, i {
    color: #fff !important;
  }
}


.is-active {
  color: #fff !important;
  background: #03a1b3 !important;
  span {
    color: #fff !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: #03a1b3 !important;
  span {
    color: #fff !important;
  }
}
</style>