<template>
  <div class="sidebar-container">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoutes"
      :collapse="sidebar.collapse"
      :background-color="sidebar.bgColor"
      :text-color="sidebar.textColor"
    >
      <div class="sidebar-head">
        <div class="sidebar-img">
          <img src="../assets/img/logo.png" alt="" />
        </div>
        <div class="sidebar-text" v-if="!sidebar.collapse">
          <div class="sidebar-text-ch">智能手铐管理系统</div>
          <div class="sidebar-text-en">ZHINENGSHOUKAOGUANLIXITONG</div>
        </div>
      </div>
      <template v-for="(item, index) in menuList">
        <template v-if="item.children">
          <el-sub-menu :index="item.name" :key="index">
            <template #title>
              <el-icon>
                <component :is="item.meta.icon"></component>
              </el-icon>
              <span>{{ item.meta.tabs[0] }}</span>
            </template>
            <template v-for="(subItem, subIde) in item.children">
              <el-sub-menu v-if="subItem.meta.children" :index="subItem.name" :key="`subIde_${subIde}`">
                <template #title>{{ subItem.meta.tabs[0] }}</template>
                <el-menu-item v-for="(threeItem, i) in subItem.children" :key="`subIde_${subIde}_${i}`" :index="threeItem.name" @click="menuClick(threeItem)">
                  {{ threeItem.meta.tabs[0] }}
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="subItem.name" @click="menuClick(subItem)">
                <div class="yuan"></div>
                {{ subItem.meta.tabs[1] }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item :index="item.name" :key="index" @click="menuClick(item)">
            <el-icon>
              <component :is="item.meta.icon"></component>
            </el-icon>
            <template #title>{{ item.meta.tabs[0] }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
import { useSidebarStore } from "../store/sidebar";
import { useRoute, useRouter, RouteRecordRaw } from "vue-router";
import { routes } from "@/router/index";

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

const onRoutes = computed(() => {
  if (String(route.name).indexOf("/") != -1) {
    return String(route.name).split("/")[0];
  }
  return route.name;
});

const menuClick = (item: RouteRecordRaw) => {
  router.push(item.path);
};

const getMenuList = (list: RouteRecordRaw[]) => {
  let array = [];
  list.forEach((item) => {
    if (item.meta.hideMenu == true) {
    } else {
      array.push(item);
    }
    if (item.children) {
      item.children = getMenuList(item.children);
    }
  });
  return array;
};

let menuList = getMenuList(routes[0].children);

const sidebar = useSidebarStore();
// 侧边栏折叠
const collapseChage = () => {
  sidebar.handleCollapse();
};
</script>

<style scoped lang="less">
.sidebar-container {
  bottom: 0px;
  font-size: 0px;
  height: 100%;
  left: 0px;
  position: fixed;
  top: 0px;
  z-index: 1001;
  overflow: visible;
  width: 210px;

  .sidebar-head {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 30px;
    .sidebar-img {
      width: 41px;
      height: 41px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .sidebar-text {
      color: #061550;
      margin-left: 15px;

      .sidebar-text-ch {
        font-size: 18px;
        white-space: nowrap;
      }
      .sidebar-text-en {
        font-size: 8px;
        white-space: nowrap;
      }
    }
  }
}
.collapse-btn {
  height: 40px;
  border-top: 1px solid #ddd;
  border-right: 1px solid #ddd;
  text-align: center;
  line-height: 40px;
  font-size: 24px;
}

// .sidebar::-webkit-scrollbar {
//   width: 0;
// }

.sidebar-el-menu:not(.el-menu--collapse) {
  width: 210px;
  height: 100%;
  padding-top: 30px;
  box-sizing: border-box;
}

.el-menu {
  .is-active {
    color: #061451;
    background: #061451;
    color: #fff;
  }
  .el-menu-item {
    border-radius: 6px;
    width: 198px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    &:hover {
      background: #061451;
      color: #fff;
    }
  }
  .el-sub-menu {
    border-radius: 6px;
    width: 198px;
    margin: 0 auto;
    // color: #fff;
    overflow: hidden;
    &.is-active {
      background-color: transparent;
      /deep/ .el-sub-menu__title {
        border-radius: 6px;
        background: #061451;
        color: #fff;
      }
    }

    .el-sub-menu__title {
      border-radius: 6px;
      width: 228px;
      margin: 0 auto;
    }
    .el-menu {
      .el-menu-item {
        color: #787878;
        &:hover {
          background: #e6e7ed;
        }
        .yuan {
          width: 6px;
          height: 6px;
          background: #787878;
          border-radius: 50%;
          margin-right: 21px;
        }
        &.is-active {
          color: #061451;
          background: #e6e7ed;
          .yuan {
            background: #061451;
          }
        }
      }
    }
  }
}

.sidebar-el-menu {
  flex: 1;
  // border: none;
  // min-height: 100%;
}
</style>
