<template>
  <div id="menuInfo">
    <el-row>
      <el-col :span="24">
        <el-col :span="24" class="top">
          <el-image :src="topUrl"></el-image>
          <span>管理中心</span>
        </el-col>
        <el-col :span="24">
          <el-menu :default-active="active" @select="setRight" text-color="#999" active-text-color="#044b79">
            <el-menu-item index="20">绑定微信</el-menu-item>
            <template v-for="(i, index) in menuList">
              <el-menu-item :key="index" :index="`${index}`" v-if="i.cpt">
                <template slot="title">
                  <!-- <i class="el-icon-pie-chart"></i> -->
                  <span>{{ i.name }}</span>
                </template>
              </el-menu-item>
            </template>
          </el-menu>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('role');
export default {
  name: 'menuInfo',
  props: {},
  components: {},
  data: function() {
    return {
      topUrl: require('@/assets/live/square_big.png'),
      active: '0',
      haveMsg: false,
      menuList: [],
    };
  },
  created() {
    //TODO 子管理员需要查询对应的权限表
    this.search();
  },
  methods: {
    ...mapActions(['query', 'create', 'update', 'delete']),
    async search() {
      let data;
      if (this.user.role == '0' && !_.get(this.user, 'pid')) {
        const res = await this.query();
        if (this.$checkRes(res)) {
          data = res.data.reverse();
          data = this.dataChange(data);
        }
      } else {
        let menus = _.get(this.user, 'menus');
        data = this.dataChange(JSON.parse(menus));
      }
      this.$set(this, `menuList`, data);
      this.setRight(this.active);
    },
    dataChange(data) {
      data = data.map(i => {
        let { role_name: name, url: cpt } = i;
        return { name, cpt };
      });
      return data;
    },

    // 菜单跳转

    setRight(index) {
      if (index == 20) {
        let r = { cpt: 'bindWx', name: '绑定微信' };
        if (r) this.$emit('setRight', r);
      }
      let r = this.menuList[index];
      if (r) this.$emit('setRight', r);
    },
  },
  watch: {
    active: {
      handler(val) {
        this.setRight(val);
      },
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.top {
  height: 50px;
  line-height: 50px;
  text-align: center;
  border-bottom: 1px solid #2d64b3;
  .el-image {
    float: left;
    padding: 10px 40px;
    width: 30px;
    height: 30px;
  }
  span {
    font-size: 24px;
    color: #92959a;
    font-weight: 600;
    float: left;
    text-align: left;
  }
}
/deep/.el-menu-item {
  font-weight: bold;
  font-size: 20px;
  text-align: center;
  border-bottom: 1px solid #2d64b3;
  margin: 0 20px;
}
</style>
