<!--
<vue-api-aside :activeSelTab="activeSelTab" @setSelectTab="setSelectTab"></vue-api-aside>
import vueApiAside from '@/components/vue-api-aside.vue';

activeSelTab: ''


-->
<template>
  <div class="api-body-left border-box">
    <ul class="tab-left-list border-box">
      <template v-for="(item, index) in leftMenuData">
        <li :class="['tab-left-list-cell color-606266 font-14 border-box my-first-level', item.tabCode == activeTab ? 'active-tab' : '']" :key="index" @click="selectTab(item, false, index)">
          <i :class="['iconfont tab-icon', item.tabIcon, !!item.onlyIconActive ? 'iconActive' : '']"></i>{{ item.tabName }}
          <!-- <i v-if="!!item.children && !!item.children.length" :class="[!!item.collapsFlag ? 'el-icon-arrow-up' : 'el-icon-arrow-down']"></i> -->
        </li>
        <ul class="child-tab-left-list" :key="'childitem' + index" v-if="item.collapsFlag">
          <template v-for="(childitem, childIndex) in item.children">
            <li :class="['tab-left-list-cell color-606266 font-14 border-box', childitem.tabCode == activeTab ? 'active-tab' : '']" :key="childIndex" @click="selectTab(childitem, 'child', index)">{{ childitem.tabName }}</li>
          </template>
        </ul>
      </template>
    </ul>
  </div>
</template>
<script>
import { getRequest } from '@/api/api';
export default {
  name: 'vue-api-aside',
  props: {
    activeSelTab: {
      type: String,
      default() {
        return '1';
      }
    }
  },
  data() {
    return {
      activeTab: this.activeSelTab,
      leftMenuData: [
        {
          tabId: 1,
          tabCode: 1,
          tabName: '概览',
          tabIcon: 'icongailan-',
          collapsFlag: false,
          tabUrl: '/overview',
          children: []
        },
        {
          tabId: 2,
          tabCode: 2,
          tabName: 'API管理',
          tabIcon: 'iconapi',
          collapsFlag: true,
          tabUrl: '/apiProgram',
          children: [
            {
              tabId: 21,
              tabCode: 21,
              tabName: '项目列表',
              tabIcon: '',
              tabUrl: '/apiProgram'
            },
            {
              tabId: 22,
              tabCode: 22,
              tabName: 'API列表',
              tabIcon: '',
              tabUrl: '/apiList'
            }
          ]
        },
        {
          tabId: 3,
          tabCode: 3,
          tabName: '环境管理',
          tabIcon: 'iconhuanjingguanli',
          collapsFlag: false,
          tabUrl: '/envManage',
          children: []
        },
        {
          tabId: 4,
          tabCode: 4,
          tabName: '发布记录',
          tabIcon: 'iconfabu',
          collapsFlag: false,
          tabUrl: '/releaseRecords',
          children: []
        },
        {
          tabId: 5,
          tabCode: 5,
          tabName: '插件管理',
          tabIcon: 'iconchajianguanli',
          collapsFlag: false,
          tabUrl: '/pluginManage',
          children: []
        },
        {
          tabId: 6,
          tabCode: 6,
          tabName: '错误码',
          tabIcon: 'iconcuowu',
          collapsFlag: false,
          tabUrl: '/errorCode',
          children: []
        },
        {
          tabId: 7,
          tabCode: 7,
          tabName: '预警系统',
          tabIcon: 'iconRectangleCopy',
          collapsFlag: false,
          tabUrl: '/',
          children: []
        },
        {
          tabId: 8,
          tabCode: 8,
          tabName: '日志管理',
          tabIcon: 'iconrizhiguanli',
          collapsFlag: false,
          tabUrl: '/',
          children: []
        },
        {
          tabId: 9,
          tabCode: 9,
          tabName: '权限管理',
          tabIcon: 'iconquanxian',
          collapsFlag: false,
          tabUrl: '/',
          children: []
        }
      ]
    };
  },
  methods: {
    /**
     * 选择后触发方法，返回 code
     */
    selectTab(item, flag, index) {
      let that = this;
      if (!!flag) {
        that.leftMenuData[index].onlyIconActive = true;
      } else {
        that.leftMenuData.forEach(ele => {
          ele.onlyIconActive = false;
        });
        // item.collapsFlag = !!item.collapsFlag && item.hasOwnProperty('children') && item.children.length > 0 ? false : true;
        that.$forceUpdate();
      }
      // 判断已选 item, 判断只让 icon 变色
      if (item.hasOwnProperty('children') && item.children.length > 0) {
        that.leftMenuData[index].onlyIconActive = true;
        if (item.children[0].hasOwnProperty('children') && item.children[0].hasOwnProperty('children').length > 0) {
          that.activeTab = item.children[0].children[0].tabCode;
          that.$emit('setSelectTab', item);
          return false;
        }
        that.activeTab = item.children[0].tabCode;
        that.$emit('setSelectTab', item);
        return false;
      }
      that.activeTab = item.tabCode;
      that.$emit('setSelectTab', item);
    },
    /**
     *  获取左侧菜单
     */
    getLeftMenu() {
      const that = this;
      let para = {
        project: that.repProjectName,
        router: that.pathName,
        requestProject: that.repProjectName
      };
      getRequest('/haoban-manage-web/menu-detail', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (!resData.result) {
              that.$message.error({
                duration: 1000,
                message: '暂无数据'
              });
              return;
            }
            return;
          }
          that.$message.error({
            duration: 1000,
            message: resData.message
          });
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    $route: {
      handler: function(val, oldVal) {},
      // 深度观察监听
      deep: true
    },
    activeSelTab: function(val, oldVal) {
      this.activeTab = val;
    }
  },

  /* 接收数据 */
  mounted() {
    const that = this;
    that.activeTab = that.activeSelTab;
  }
};
</script>

<style lang="less" scoped>
.tab-left-list {
  .tab-icon {
    padding-right: 10px;
    display: inline-block;
    vertical-align: middle;
    &.iconRectangleCopy {
      width: 16px;
      font-size: 20px;
    }
    &.icongailan- {
      font-size: 14px;
    }
  }
  li {
    white-space: nowrap;
    overflow: hidden;
  }
  .tab-left-list-cell {
    position: relative;
    text-align: left;
    height: 40px;
    line-height: 40px;
    white-space: nowrap;
    overflow: hidden;
    cursor: pointer;
    color: #606266;
    &.my-first-level {
      padding: 0 0 0 30px;
    }
    .iconActive {
      color: #5584ff;
    }
    &:hover {
      color: #5584ff;
      background: #f5f7fa;
      i {
        color: #5584ff;
      }
    }
    &.active-tab {
      color: #5584ff;
      background: #f5f7fa;
      /* &::before {
        content: ' ';
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 2px;
        background: #5584ff;
        z-index: 1;
      } */
      i {
        color: #5584ff;
      }
    }
  }
  .child-tab-left-list {
    -webkit-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
    li {
      position: relative;
      text-align: left;
      height: 40px;
      line-height: 40px;
      padding: 0 0 0 55px;
      white-space: nowrap;
      overflow: hidden;
      cursor: pointer;

      &:hover {
        color: #5584ff;
        background: #f5f7fa;
        i {
          color: #5584ff;
        }
      }
      &.active-tab {
        color: #5584ff;
        background: #f5f7fa;
        /* &::before {
          content: ' ';
          position: absolute;
          left: 0;
          top: 0;
          bottom: 0;
          width: 2px;
          background: #5584ff;
          z-index: 1;
        } */
        i {
          color: #5584ff;
        }
      }
    }
  }
}
</style>
