<template>
  <div :class="{ 'has-logo': showLogo }">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :unique-opened="false"
        :active-text-color="variables.menuActiveText"
        :collapse-transition="false"
        mode="vertical"
      >
        <sidebar-item
          v-for="route in routes"
          :key="route.path"
          :item="route"
          :base-path="route.path"
        />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

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

export default {
  components: { SidebarItem, Logo },
  computed: {
    ...mapGetters(["sidebar", "menu"]),
    routes() {
      console.log(this.menu);
      let obj = {};
      let that = this;
      traverseMenuList(this.menu || []);
      that.addParentKeyWrapper(that.$router.options.routes); //为路由树增加parentKey
      let flattenRouterData = that.flattenTreeDataClosure(
        that.$router.options.routes
      );
      // debugger
      let newMenu = getNewMenu(that.$router.options.routes);
      newMenu.map((item) => {
        if (!item.hidden) {
          let parent = that.findParent(item.name, flattenRouterData);
          that.$router.options.routes.map((v) => {
            if (v == parent) {
              v.hidden = false;
            }
          });
        }
      });
      function traverseMenuList(arr) {
        if (arr.length > 0) {
          arr.map((item) => {
            if (item.menuName) {
              obj[item.menuName] = true;
            }
            if (item.children) {
              traverseMenuList(item.children);
            }
          });
        }
      }
      // 设置需要隐藏的菜单
      function getNewMenu(arr) {
        // debugger
        arr.map((item) => {
          if (!item.name || item.hidden) {
            return;
          } else {
            item.hidden = item.hidden ? item.hidden : !obj[item.name];
            // 如果该节点要显示，则获取到该节点的父节点并显示该父节点
            // if (!item.hidden) {
            //   // console.log(222, item);
            //   // debugger;
            //   let parent = that.findParent(item.name, flattenRouterData);
            //   console.log(111, parent);
            // }
            if (item.children) {
              getNewMenu(item.children);
            }
          }
        });

        return arr;
      }

      // console.log(obj);
      return newMenu;
    },
    activeMenu() {
      const route = this.$route;
      const { meta, path } = route;
      // if set path, the sidebar will highlight the path you set
      if (meta.activeMenu) {
        return meta.activeMenu;
      }
      return path;
    },
    showLogo() {
      return this.$store.state.settings.sidebarLogo;
    },
    variables() {
      return variables;
    },
    isCollapse() {
      return !this.sidebar.opened;
    },
  },
  methods: {
    // 1. 为子节点添加父级的key
    addParentKeyWrapper(tree) {
      // debugger
      const data = JSON.parse(JSON.stringify(tree)); // 深度克隆（deepClone）
      function addParentKey(data, parentKey) {
        data.forEach((ele) => {
          ele.parent = parentKey;
          if (ele.children) {
            addParentKey(ele.children, ele.name);
          }
        });
      }
      addParentKey(data, null); // 一开始的时候是null
      return data;
    },

    // 将树拉平,节点之间通过parentKey联系
    flattenTreeDataClosure(data) {
      const treeData = JSON.parse(JSON.stringify(data));
      const flattenData = [];
      function flattenTree(data, parentKey) {
        data.forEach((ele) => {
          // const { name } = ele;
          flattenData.push({ name: ele.name, parentKey });
          if (ele.children) {
            flattenTree(ele.children, ele.name);
          }
        });
      }
      flattenTree(treeData, null);
      return flattenData;
    },

    // 找到某个元素的父节点
    findParent(item, flattenTree) {
      return flattenTree.find((ele) => {
        return ele.name === item;
      });
    },
  },
};
</script>
