<template>
  <div>
    <div class="flex-1">
      <div class="logo">
        <img width="30"
             src="@/assets/images/logo.png"
             alt="">
        <span>运营管理后台</span>
      </div>

      <el-menu :default-active="active"
               :default-openeds="['user']"
               background-color="#001529"
               text-color="#fff"
               active-text-color="#fff"
               @select="menuSelect">
        <template v-for="item in menus">
          <template v-if="item.subs">
            <el-submenu :index="item.index"
                        :key="item.index">
              <template slot="title">
                <img class="icon"
                     :src="require('@/assets/images/' + item.icon + '.png')"
                     alt="">
                <span slot="title">{{ item.title }}</span>
              </template>
              <el-menu-item v-for="(subItem, i) in item.subs"
                            :key="i"
                            :index="subItem.index">{{ subItem.title }}</el-menu-item>
            </el-submenu>
          </template>
          <template v-else>
            <el-menu-item :index="item.index"
                          :key="item.index">
              <img class="icon"
                   :src="require('@/assets/images/' + item.icon + '.png')"
                   alt="">
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>
          </template>
        </template>
      </el-menu>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import Setting from '@/setting'
export default {
  data () {
    return {
      active: this.$route.path,
      menus: [],
      defaultMenus: [
        {
          icon: 'user',
          index: '/user/list',
          title: '用户管理'
        },
        {
          icon: 'preschool',
          index: '/preschool/list',
          title: '幼儿园管理',
        },
        {
          icon: 'supplier',
          index: '/supplier/list',
          title: '供应商管理',
        },
        {
          icon: 'activity',
          index: '/activity/list',
          title: '活动管理'
        }
      ],
    };
  },
  computed: {
    ...mapState('auth', [
      'btns'
    ])
  },
  watch: {
    "$route": {
      handler (to, from) {
        this.active = `/${this.$route.path.split('/')[1]}/list`
      },
      deep: true,
      immediate: true
    }
  },
  created () {
    this.initMenu()
  },
  methods: {
    ...mapActions('user', [
      'logout'
    ]),
    // 菜单选择
    menuSelect (index) {
      this.$router.push(index)
    },
    // 初始化菜单
    initMenu () {
      // 如果开启了动态路由，则取store里的路由匹配
      if (Setting.dynamicRoute) {
        const { btns } = this
        const menus = []
        this.defaultMenus.map(e => {
          if (btns.find(n => n.includes(e.index) || n === e.title)) {
            // 匹配子菜单
            if (e.subs && e.subs.length) {
              const children = []
              e.subs.map((j, i) => {
                btns.find(n => n.includes(j.index)) && children.push(j)
              })
              e.subs = children
            }
            menus.push(e)
          }
        })
        this.menus = menus
      } else {
        this.menus = this.defaultMenus
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.nav {
    height: 100%;
    background-color: #001529;
    box-shadow: 2px 0px 6px 0px rgba(92, 111, 130, 0.35);
    overflow: auto;
    .logo {
        display: flex;
        align-items: center;
        padding-left: 28px;
        margin: 15px 0 26px;
        font-size: 16px;
        color: #fff;
        img {
            margin-right: 8px;
        }
    }
    /deep/.el-menu {
        border-right: 0 !important;
        .icon {
            margin: 0 11px 0 5px;
        }
        .el-menu-item {
            &.is-active {
                background-color: #2962ff !important;
            }
        }
        .el-menu .el-menu-item {
            padding-left: 50px !important;
        }
    }
    .light {
        display: flex;
        height: 100%;
        background-color: #fff;
        overflow: hidden;
        .col-wrap {
            width: 64px;
            background-color: #001529;
        }
        .logo-icon {
            width: 41px;
            margin: 16px 0 30px 7px;
        }
        .col-nav {
            text-align: center;
            li {
                padding: 25px 0;
                cursor: pointer;
            }
        }
        .name {
            margin: 17px 0 27px 15px;
            font-size: 15px;
            font-weight: 600;
            text-align: center;
            color: #333;
            line-height: 32px;
        }
        .col-menu {
            width: 192px;
        }
        /deep/.el-submenu__title {
            padding-left: 14px !important;
        }
        .icon {
            margin-left: 0;
        }
    }
}

.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar > ul {
    height: 100%;
}
</style>