<template>
  <div :class="{ 'has-logo': showLogo }">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <!--<div class="www el-scrollbar" style="float: left;color:red">-->
    <!--<div v-for="route in this.$router.options" class="box">-->
    <!--<el-button>{{ route.name }}</el-button>-->
    <!--</div>-->
    <!--</div>-->
    <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 menuList" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from '@/assets/sass/variables.scss'

export default {
  components: { SidebarItem, Logo },
  computed: {
    ...mapGetters(['sidebar', 'sidebarList']),
    routes() {
      return this.$router.options.routes
    },
    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 {
      menuList: []
    }
  },
  methods: {
    ...mapActions({
      setSidebarList: 'app/setSidebarList'
    }),
    formatList(menu, state, parentPath = null) {
      menu.map(item => {
        if (parentPath) {
          item.path = '/' + parentPath + '/' + item.url
        } else {
          item.path = item.url
        }
        if (item.otherInfo1 == 0) {
          item.name = item.menuName
          item.alwaysShow = state
          item.meta = {
            title: item.menuName,
            icon: item.img
          }
          if (item.children != null) {
            this.formatList(item.children, undefined, item.path)
          } else {
            item.children = []
          }
        } else {
          item.children = []
        }
      })
      return menu
    }
  },

  created() {
    if (sessionStorage.token !== 'MockToken') {
      // 暂时处理 配置按钮的隐藏
      let data = this.$store.state.app.sidebarList
      data.map(item => {
        item.children.map(p => {
          if (p.children.length != 0) {
            p.children = []
          }
        })
      })
      this.menuList = data
    } else {
      this.menuList = this.routes
    }
  }
}
</script>
<style scoped lang="scss">
#app .sidebar-container {
  /*width: 200px !important;*/
}
.www {
  width: 100px;
  height: calc(100% - 50px);
}
.box {
  text-align: center;
  line-height: 100px;
  height: 100px;
  width: 100px;
  color: #000;
  background: #ccc;
  font-size: 15px;
  .el-button {
    padding: 5px;
  }
}
</style>
