<template>
  <div id="Sidebar">
    <el-row class="sidebar">
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="false" unique-opened router>
            <template v-for="item in items">
              <!-- 二级菜单 -->
              <template v-if="item.type === '0'">
                <el-submenu :index="item._id || item.index" :key="item._id">
                  <template slot="title">
                    <i :class="['iconfont', item.icon]"></i>
                    <span slot="title">{{ item.name }}</span>
                  </template>
                  <template v-for="subItem in item.children">
                    <!-- 三级菜单 -->
                    <el-submenu
                      v-if="subItem.children && subItem.children.length > 0 && subItem.children.every((f) => f.type === '0' || f.type === '1')"
                      :index="subItem._id"
                      :key="subItem._id"
                    >
                      <template slot="title">
                        <i :class="['iconfont', subItem.icon]"></i>
                        <span slot="title">{{ subItem.name }}</span>
                      </template>
                      <el-menu-item v-for="(threeItem, i) in subItem.children" :key="i" :index="threeItem.path">
                        <template slot="title">
                          <i :class="['iconfont', threeItem.icon]"></i>
                          <span slot="title">{{ threeItem.name }}</span>
                        </template>
                      </el-menu-item>
                    </el-submenu>
                    <el-menu-item v-else :index="subItem.path" :key="subItem.path">
                      <template slot="title">
                        <i :class="['iconfont', subItem.icon]"></i>
                        <span slot="title">{{ subItem.name }}</span>
                      </template>
                    </el-menu-item>
                  </template>
                </el-submenu>
              </template>
              <!-- 一级菜单 -->
              <template v-else>
                <el-menu-item class="first" :index="item.path" :key="item.path">
                  <i :class="['iconfont', item.icon]"></i>
                  <span slot="title">{{ item.name }}</span>
                </el-menu-item>
              </template>
            </template>
          </el-menu>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const { system, adminMenu, devMenu } = require('./data/menu');
const { menuInfo } = require('./data/site');
import { mapState, createNamespacedHelpers } from 'vuex';
import _ from 'lodash';
import bus from './bus';
export default {
  name: 'Sidebar',
  props: {},
  components: {},
  data: function () {
    return {
      menuInfo: menuInfo,
      collapse: false,
      items: [...system, ...adminMenu],
    };
  },
  created() {
    bus.$on('collapse', (msg) => {
      this.collapse = msg;
      bus.$emit('collapse-content', msg);
    });
  },
  methods: {
    async getMenu() {
      const { options } = this.$router;
      if (!options) {
        console.warn('获取菜单:解析base错误');
        return;
      }
      if (this.$dev_env) this.items.unshift(...devMenu);
    },
  },
  computed: {
    ...mapState(['user', 'menuList']),
    onRoutes() {
      return this.$route.path;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    user: {
      deep: true,
      immediate: true,
      handler(val) {
        this.getMenu();
      },
    },
  },
};
</script>

<style lang="less" scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 60px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 200px;
}
.sidebar > ul {
  height: 100%;
}
.main {
  .one {
    .iconfont {
      font-size: 18px;
      margin: 0 5px 0 0;
    }
  }
}
</style>
