<template>
  <div class="my-content-wrap">
    <yw-other-header :headerIcon.sync="headerIcon" :headerName="headerName" :userData="userData"></yw-other-header>
    <div class="my-content-content">
      <div class="dictionary-wrap" :style="{ 'min-height': bgHeight }">
        <div class="my-left-aside">
          <yw-left-aside :tabListData="tabListData" :activeSelTab="activeSelTab" @setSelectTab="setSelectTab"></yw-left-aside>
        </div>
        <div class="my-right-wrap">
          <router-view :activeSelTab="activeSelTab" @showTab="showTab"></router-view>
        </div>
      </div>
    </div>
    <!-- <yw-footer></yw-footer> -->
  </div>
</template>

<script>
import { getRequest } from '@/api/api';
import ywOtherHeader from '@/components/yw-other-header';
import ywLeftAside from '@/components/yw-left-aside';
// import ywFooter from '@/components/yw-footer/yw-footer';
export default {
  name: 'haoban',
  data() {
    return {
      requestProject: 'gic-bizdict',
      bgHeight: window.screen.availHeight - 220 + 'px',
      tabList: [],
      tabListData: [],
      activeSelTab: '',
      // 头部的icon和name
      headerIcon: '',
      headerName: '',
      headerCode: '',
      userData: {}
    };
  },
  beforeMount() {
    this.getHome();
  },
  methods: {
    init() {
      this.headerIcon = this.$route.query.icon;
      this.headerName = this.$route.query.name;
      this.headerCode = this.$route.query.code;
      this.activeSelTab = this.$route.query.tabCode || 'platform_version_haoban';
      this.getMenuTree();
    },

    getHome() {
      let that = this;
      getRequest('/gic-platform-operation/home', {})
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 0) {
            that.init();
          } else {
            window.location.href = window.location.origin + '/operation-platform/#/';
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    // 获取左侧
    getMenuTree() {
      let that = this;
      let para = {
        requestProject: 'gic-authcenter'
      };
      getRequest('/gic-platform-operation/loginuser', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 0) {
            // 获取用户信息
            resData.result.gender = parseInt(resData.result.sex);
            resData.result.clerkCode = resData.result.employeeNumber;
            resData.result.phoneNumber = resData.result.userMobile;
            resData.result.positionName = resData.result.position;
            that.userData = resData.result;
            that.entranceList = [];
            let entranceList = resData.result.menuTree ? resData.result.menuTree : [];
            entranceList.forEach(item => {
              if (item.code == that.headerCode) {
                let tabListData = item.nodeChildren ? item.nodeChildren : [];
                that.tabListData = that.getNewTabList(tabListData);
                if (!that.activeSelTab) {
                  that.activeSelTab = that.tabListData[0].children.length > 0 ? that.tabListData[0].children[0].tabId : that.tabListData[0].tabId;
                }
              }
            });
          } else {
            that.$message.error(resData.message);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    // 整理左侧数据
    getNewTabList(tabListData) {
      let that = this;
      tabListData.forEach(item => {
        if (item.isShow == 1) {
          item.tabId = item.id;
          item.tabName = item.menuName;
          item.icon = item.iconUrl ? item.iconUrl : 'iconcaidan';
          item.onlyIconActive = false;
          item.collapsFlag = true; // 新增收起的字段
          item.children = item.nodeChildren ? item.nodeChildren : [];
          if (item.children.length > 0) {
            item.children.forEach(el => {
              el.icon = '';
            });
            that.getNewTabList(item.children);
          }
        }
      });
      return tabListData;
    },
    // 选择后返回tabCode，做各路由判断
    setSelectTab(item) {
      let tabCode = item.code;
      console.log(item);
      if (item.hasOwnProperty('children') && !!item.children.length) {
        tabCode = item.children[0].code;
        if (item.children[0].hasOwnProperty('children') && !!item.children[0].hasOwnProperty('children').length) {
          tabCode = item.children[0].children[0].code;
        }
      }
      this.$router.push({
        path: item.uri,
        query: {
          name: this.headerName,
          icon: this.headerIcon,
          code: this.headerCode,
          tabCode: tabCode
        }
      });
    },
    // 各路由返回的tabcode
    showTab(tabCode) {
      console.log(tabCode);
      this.activeSelTab = tabCode;
    }
  },
  components: {
    ywOtherHeader,
    ywLeftAside
    // ywFooter
  }
};
</script>

<style lang="less" scoped>
.my-content-content {
  background: #f0f2f5;
  padding: 20px;
}
.dictionary-wrap {
  display: flex;
  height: 100%;
  background: #fff;
}
</style>
