<template>
  <div :class="{'has-logo':showLogo}">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <!-- :background-color="variables.menuBg" -->
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"

        :text-color="variables.menuText"
        :unique-opened="false"
        :active-text-color="variables.menuActiveText"
        :collapse-transition="false"
        mode="vertical"
      >
        <sidebar-item v-for="route in menuRoutesTree" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
import Vue from 'vue';
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([
      'permission_routes',
      'sidebar'
    ]),
    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
    }
  },
  data() {
    return {
      menuRoutesTree: []
    }
  },
  created() {
    let arr = this.returnMenuTree(this.permission_routes, [])
    let menuTree = Vue.ls.get('menuTree').children
    let [...menuTreeArr] = JSON.parse(JSON.stringify(menuTree))
    this.menuRoutesTree = this.returnInterFaceMenuTree(menuTreeArr, arr)
  },

  methods: {
    returnMenuTree(json, temp) {
      for (let i=0;i<json.length;i++) {
        let item = json[i]
        temp.push(item)
        if (item.children && item.children.length !== 0) {
          this.returnMenuTree(item.children, temp)
        }
      }
      return temp
    },
    returnInterFaceMenuTree(json, sysMenuTree) {
      for (let i=0;i<json.length;i++) {
        let item = json[i]
        let obj = sysMenuTree.find(ele => ele.path === item.url)
        if (obj) {
          let { children, ...obj1 } = obj
          item = Object.assign(item, obj1)
        }
        if (item.children && item.children.length !== 0) {
          this.returnInterFaceMenuTree(item.children, sysMenuTree)
        }
      }
      return json
    }
  }

}
</script>
