<template>
  <div>
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :show-timeout="200"
        :default-active="defaultActive"
        :collapse="isCollapse"
        mode="vertical"
        @select="selectMenu"
      >
        <el-menu-item v-if="systemName" :title="systemName" index="1" disabled class="system-item" style="padding-left: 10px">
          {{ systemName }}
        </el-menu-item>
        <sidebar-item v-for="route in protectedRouters" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>
    </el-scrollbar>
    <hamburger :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import SidebarItem from './SidebarItem'
import Vue from 'vue'
import { getLanguageWithLocale } from '../../../../utils/util'
import { isShowSystemMenu } from '../../../../utils/common-util'
import Cookies from 'js-cookie'
export default {
  components: {
    SidebarItem
  },
  data() {
    let systemName
    if (isShowSystemMenu() === true) {
      const language = getLanguageWithLocale()
      const systemNameObj = Vue.prototype.systemNameObj
      if (systemNameObj) {
        systemName = systemNameObj[language]
      }
    }
    return {
      systemName,
      defaultActive: ''
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'protectedRouters'
    ]),
    isCollapse() {
      return !this.sidebar.opened
    }
  },
  watch: {
    $route() {
      this.getDefaultActiveMenu()
    }
  },
  created() {
    this.getDefaultActiveMenu()
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('toggleSidebar')
    },
    selectMenu(index, indexPath) {
      Vue.prototype._selectMenu = index
      localStorage.setItem('selectMenu', index)
    },
    getDefaultActiveMenu() {
      const cookieMenu = localStorage.getItem('selectMenu')
      const leftSelectMenu = Vue.prototype._selectMenu
      if (leftSelectMenu) {
        this.defaultActive = leftSelectMenu
      } else if (cookieMenu) {
        this.defaultActive = cookieMenu
      } else {
        // 初始进入系统
        const parentPaths = []
        this.defaultActive = this.getDefaultActiveRouteInChildren(this.protectedRouters, parentPaths)
        Vue.prototype._selectMenu = this.defaultActive
      }
    },
    getDefaultActiveRouteInChildren(children, parentPaths) {
      let defaultActive
      for (let i = 0; i < children.length; i++) {
        const route = children[i]
        if ((route.hidden === undefined || route.hidden === false) && (route.path && route.path !== '') && (route.path !== '/redirect/:path*')) {
          // 表示不是隐藏的路由
          if (route.children === undefined || route.children.length === 0) {
            // 表示当前路由就是要默认选中的
            let path = route.path
            if (path && path.indexOf('/') !== 0) {
              // 表示是相对路径，需要拼接父路由路径
              path = parentPaths.join('/') + '/' + path
            }
            if (this.$route.path === path) {
              const title = route.meta ? route.meta.title : ''
              defaultActive = path + '~~' + title
              break
            }
          } else {
            parentPaths.push(route.path)
            defaultActive = this.getDefaultActiveRouteInChildren(route.children, parentPaths)
            if (defaultActive) {
              break
            }
          }
        }
        parentPaths = []
      }
      return defaultActive
    }
  }
}
</script>
