<script>
import menuList from '@/data/json/menuList.json'
import {requireImg} from "@/utils/requireImg";

export default {
  props: {
    collapse: Boolean,
  },
  data() {
    return {
      selectedKey: [],
      iconVerticalAlign: '',
      asideStyle: {},
      menuList,
    }
  },
  watch: {
    collapse(val) {
      this.suitHeight(val)
    },
    $route() {
      this.getCurrRoute()
      this.$forceUpdate();
    }
  },
  mounted() {
    this.suitHeight(this.collapse)
  },
  emits: ['update:collapse'],
  setup(props, context) {
    const methods = {
      toggleCollapse() {
        const val = !props.collapse
        context.emit('update:collapse', val)
      },
    }
    return methods
  },
  methods: {
    requireImg,
    suitHeight(val) {
      this.getCurrRoute()
      this.$nextTick(() => {
        // let height = document.getElementsByClassName('home')[0].clientHeight
        this.asideStyle = { height: `100%` }
        if (val)
          this.asideStyle.width = '60px'
        if (this.collapse) {
          document.getElementsByClassName("aside-collapse")[0].style.textAlign = 'center'
          document.getElementsByClassName("aside-collapse")[0].style.marginLeft = '0'
          this.iconVerticalAlign = '';
        } else {
          document.getElementsByClassName("aside-collapse")[0].style.textAlign = ''
          document.getElementsByClassName("aside-collapse")[0].style.marginLeft = '5%'
          this.iconVerticalAlign = 'text-top';
        }
      })
    },
    getCurrRoute() {
      this.$nextTick(()=>{
        if (this.$route.matched.length>1) {
          this.selectedKey = [this.$route.matched[1].path+'']
        }
        let index = this.menuList.findIndex(i=>i.router==this.selectedKey)
        this.handleClickMenuItem(this.menuList[index])
        this.$forceUpdate()
      })

    },
    handleClickMenuItem(item) {
      this.$store.menuStore().currMenu = item;
    },
    handleSelected(item) {
      this.$router.push({path: item.key})
    },
  },
}
</script>

<template>
  <div class="ioc-aside">
    <div title="收起" class="collapse-btn" style="right: 0;background-color: #23608c;border-radius: 15px 0px 0px 15px" @click="toggleCollapse" v-if="!collapse">
      <div style="margin-top: 30px;margin-left: 2px" >
        <a-icon title="收起" style="color: white;font-size: 12px;vertical-align: text-top" type="menu-fold"  />
      </div>
    </div>

    <div title="展开" class="collapse-btn"  @click="toggleCollapse" v-if="collapse">
      <div style="margin-top: 30px;" >
        <a-icon title="展开" style="color: white;font-size: 12px;vertical-align: text-top;" type="menu-unfold" />
      </div>
    </div>


    <a-menu
      class="menu"
      theme="dark"
      :style="asideStyle"
      v-model:selectedKeys="selectedKey"
      mode="inline"
      @select="handleSelected"
    >
      <!--<a-menu-item-group key="g1">-->
      <!--  <template slot="title">-->
      <!--    <div style="height: 12px"></div>-->
      <!--    <div class="aside-collapse" style="color: white; cursor: pointer;margin-left: 5%" @click="toggleCollapse">-->
      <!--      <a-icon style="font-size: 18px;vertical-align: text-top" type="menu-fold" v-if="!collapse" />-->
      <!--      <a-icon style="font-size: 18px;vertical-align: text-top;" type="menu-unfold" v-if="collapse" />-->
      <!--      <span style="margin-left: 15px;vertical-align: top" v-if="!collapse">展开/收起</span>-->
      <!--    </div>-->
      <!--  </template>-->
      <!--</a-menu-item-group>-->
      <a-menu-item v-for="item in menuList" :key="item.router" @click="handleClickMenuItem(item)" >
        <span class="anticon" :style="{verticalAlign: iconVerticalAlign}">
          <a-avatar class="function-avatar" shape="square" :size="20" :src="requireImg('function/'+item.icon)"></a-avatar>
        </span>
        <span >
            {{ item.name }}
          </span>
      </a-menu-item>
    </a-menu>
  </div>
</template>

<style lang="less" scoped>
@menu-dark-bg: #266a99;
@menu-dark-submenu-bg: #266a99;
@menu-dark-color: #fff;
@menu-dark-arrow-color: #fff;
@menu-dark-highlight-color: #fff;

.ioc-aside {
  //width: 100%;
  height: 100%;
  position: relative;

  .aside-collapse {
    width: 100%;
  }
  .aside-collapse:hover {
    span {
      color: #2ba8e4;
    }
    i {
      color: #2ba8e4;
    }
  }

  .collapse-btn {
    width: 14px;
    height: 80px;
    cursor: pointer;
    background-color: #266999;
    position: absolute;
    top: 40%;
    right: -10px;
    z-index: 999;
    border-top-right-radius: 12px;
    border-bottom-right-radius: 12px;
  }

  /*菜单样式*/
  /deep/ .ant-menu-sub {
    box-shadow: none !important;
  }
  /deep/ .ant-menu-item-selected {
    color: @menu-dark-selected-item-text-color !important;
  }
  .ant-menu-inline-collapsed > .ant-menu-item {
    padding: 0 !important;
    margin-left: 0 !important;
    padding-left: 33% !important;
  }
  /deep/ .ant-menu-inline-collapsed > .ant-menu-item-group > .ant-menu-item-group-list > .ant-menu-item {
    padding: 0 !important;
    margin-left: 0 !important;
    padding-left: 33% !important;
  }

}
</style>
