<template>
  <div :class="{ 'has-logo': showLogo }">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        v-if="menuList && menuList.length > 0"
        :default-active="activeMenu"
        :collapse="isCollapse"
        router
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :active-text-color="variables.menuActiveText"
        :unique-opened="false"
        :collapse-transition="false"
        mode="vertical"
      >
        <el-menu-item
          v-for="(route, index) in menuList"
          :key="route.path + index"
          :route="route.path"
          :index="index + ''"
        >
          <template v-if="!route.hidden">
            <i :class="route.meta.icon"></i>
            <span slot="title">{{ route.meta.title }}</span>
          </template>
        </el-menu-item>
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Logo from "./Logo";
import variables from "@/styles/variables.scss";

export default {
  components: {
    Logo,
  },
  data() {
    return {
      active: -1,
      menuList: [],
    };
  },
  computed: {
    ...mapGetters(["navList", "sidebar"]),
    activeMenu() {
      const route = this.$route;
      const { path } = route;
      let list = path.split("/");
      return (
        this.menuList.findIndex((v1) => v1.path === list[list.length - 1]) + ""
      );
    },
    showLogo() {
      return true;
    },
    variables() {
      return variables;
    },
    isCollapse() {
      return !true;
    },
  },
  watch: {
    $route(route) {
      if (route.path.startsWith("/redirect/")) {
        return;
      }
      this.getBreadcrumb();
    },
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      );

      let index = -1;
      if (matched.length > 1 && matched[0] && matched[0].path) {
        const { path } = matched[0];
        index = this.navList.findIndex((v1) => v1.path === path);
      }
      if (index !== this.active) {
        this.menuList = this.navList[index].children;
        this.active = index;
      }
    },
  },
};
</script>
