<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"
        :active-text-color="variables.menuActiveText"
        :collapse-transition="false"
        unique-opened
        mode="vertical"
      >
        <sidebar-item v-for="route in sidebarRouters" v-show="checkMine(route)" :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 '@/assets/styles/variables.scss'

export default {
  components: { SidebarItem, Logo },
  data() {
    return {
      mine: {}
    }
  },
  computed: {
    ...mapGetters([
      'sidebarRouters',
      'sidebar',
      'user'
    ]),
    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
    }
  },
  mounted() {
    const selectedMineJson = window.localStorage.getItem('kd.selected.mine')
    if (selectedMineJson) {
      this.mine = JSON.parse(selectedMineJson)
    }
    if (this.user.enterpriseId === 0) { // 应急局
      this.hasPublic = true
    } else { // 非应急局
      if (this.mine && this.mine.mineType) { // 含矿山
        if (this.mine.mineType === 1) { // 地下矿山
          this.hasMineUnderground = true
        }
        if (this.mine.mineType === 2) { // 露天矿山
          this.hasMineOpencast = true
        }
        if (this.mine.mineType === 3) { // 尾矿库
          this.hasMineTailings = true
        }
      } else {
        this.hasPublic = true
      }
    }
  },
  methods: {
    checkMine(route) {
      if (route.hasPublic !== undefined) { // 按hasMine区分矿山的菜单才包含hasPublic属性
        let result = true
        console.log(this.user.enterpriseId, this.mine)
        if (this.user.enterpriseId !== 0 && this.mine && this.mine.hasEnable === false) { // 当矿山停产时，只保留视频监控
          if (route.path !== '/video') {
            result = false
          }
        }
        if (route.hasPublic && this.hasPublic) {
          return result
        }
        if (route.hasMineUnderground && this.hasMineUnderground) {
          return result
        }
        if (route.hasMineOpencast && this.hasMineOpencast) {
          return result
        }
        if (route.hasMineTailings && this.hasMineTailings) {
          return result
        }
        return false
      } else { // 其他菜单不过滤
        return true
      }
    }
  }
}
</script>
