<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 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> -->
               <img class="menu-icon" :src="`/src/assets/menu/${item.meta.icon}.png`" 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"
                            @click="handleClick(item_son.path)"
                            class="submenu-title-noDropdown"
              >
                <span>{{getMenuTitle(item_son)}}</span>
              </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> -->
            <img class="menu-icon" :src="`/src/assets/menu/${item.meta.icon}.png`" alt="">
            <span>{{getMenuTitle(item)}}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-col>
  </el-row>
</template>

<script setup>
import { computed } from 'vue';
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from "@element-plus/icons-vue";
import {routes} from "../router/index.js";
import { useRoute, useRouter } from 'vue-router';
import { menuStore } from '@/pinia/menu.js';
import { storeToRefs } from 'pinia';
const route = useRoute();
const router = useRouter();
const { sidebar } = storeToRefs(menuStore());
const handleClick = (path) => {
  console.log(path);
  if (path.startsWith('/')) {
    router.push(path) // 绝对路径，直接跳转
  } else {
    router.push('/' + path) // 相对路径，补全
  }
}

const routerList = routes[0].children;

const activeMenu = computed(()=>{
   console.log('123--', route.path);
   return route.path;
});


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;
  }
  .menu-icon {
    width: 16px;
    // height: 18px;
    margin-right: 5px;
  }
}


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

.el-menu-item {
  background: #055F7CFF !important;
}

.submenu-box .is-active {
  background-color: #fff !important;
  span {
    color: #055f7c !important;
  }
}

</style>