<template>
  <div class="settingMenu">
    <div class="menu">
      <div class="appInfo">
        <div class="img" v-loading="imgLoading">
          <template v-if="imgUrl">
            <img :src="imgUrl" alt="" style="width: 64px; height: 64px"/>
          </template>
          <template v-if="!imgUrl">
            <img src="./images/icon.png" alt="" style="width: 64px; height: 64px"/>
          </template>
        </div>
        <div class="appName">{{ name }}</div>
        <div class="version" v-if="code">编码：{{ code }}</div>
      </div>
      <div class="menuList">
        <div class="item" v-for="(item, index) in settingMunu" :key="index" @click="menuClick(index, item)">
          <div class="icon" :class="{ actived: index == menuActived }">
            <svg-icon class="iconfont" :icon-class="item.icon" fill="currentColor"/>
          </div>
          <div class="title">{{ item.title }}</div>
        </div>
      </div>
    </div>
    <div class="content">
      <router-view ref="routerChunk" :build="billid" @saveSuccess="saveSuccess" @saveSuccessInit="saveSuccessInit"
                   @flashActive="flashActive"
      ></router-view>
    </div>
  </div>
</template>

<script>
import { getApiModuleApi } from '@/api/apiChunks/index.js'
import configData from './configData'
// 应用信息
import masterDataAdd from './masterDataAdd.vue'
import request from '@/utils/request'

export default {

  data() {
    return {
      menuActived: 0,
      settingMunu: configData.settingMenu,
      query: this.$route.query,
      name: '',
      code: '',
      imgUrl: '',
      versionNumber: '',
      imgLoading: false,
      billid: ''
    }
  },
  methods: {
    //当新增完基本信息后
    saveSuccessInit(res, fun) {
      console.log(res, 'res')
      this.billid = res.attribute.id
      this.mdmCode = res.attribute.mdmCode
      this.$refs.routerChunk.billid = this.billid
      this.initEditFormData()
      fun()
    },
    flashActive(val) {
      this.menuActived = val
    },
    async initEditFormData() {
      const res = await getApiModuleApi({
        tl: 'mdmModuleService',
        as: '',
        dj: 'getMdm'
      }, { id: this.billid })
      if (res.status === '200') {
        this.name = res.attribute.mdmName
        this.code = res.attribute.mdmCode
        //下载logo
        this.imgLoading = true
        return request({
          url: '/kangarooDataCenterV3/entranceController/fileDownloadNew?id=' + res.attribute.mdmLogo,
          method: 'get',
          responseType: 'arraybuffer'
        }).then((res) => {
          this.imgLoading = false
          if (!res.byteLength) {
            this.imgUrl = false
            return
          }
          this.imgUrl =
              'data:image/png/jpg;base64,' + btoa(new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''))
        })
      }
    },
    //菜单点击事件
    saveSuccess() {
      this.initEditFormData()
    },
    menuClick(index, item) {
      console.log(index, item)
      if (index !== 0 && !this.billid) {
        this.$vmNews('当前为新增请先新增后再跳转该模块')
        return
      }
      if (item.path && !this.$route.path.includes(item.path)) {
        this.menuActived = index
        this.$set(this.query, 'id', this.billid)
        this.$set(this.query, 'mdmCode', this.mdmCode)
        this.$router.replace({
          path: item.path,
          query: this.query
        })
      }
    }
  },
  components: {
    masterDataAdd
  },
  mounted() {
    if (this.$route.query.flag !== 'new') {
      this.billid = this.$route.query.id
      this.mdmCode = this.$route.query.mdmCode
      this.initEditFormData()
    }
  }
}
</script>

<style scoped lang="scss">
.settingMenu {
  width: 100%;
  display: flex;
  position: relative;
  background: #fafafa !important;
  overflow: auto;

  .menu {
    position: fixed;
    width: 180px;
    height: 90vh;
    //margin-right: 1%;
    background-color: #fff;
    border-radius: 16px;
    overflow: auto;

    .appInfo {
      padding: 10px 0;
      border-bottom: 1px solid #f3f3f3;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      > .img {
        width: 64px;
        height: 64px;
      }

      .appName {
        margin-top: 8px;
        font-weight: 600;
        font-size: 18px;
      }

      .version {
        margin-top: 10px;
        font-size: 12px;
        padding: 1px 2px;
        color: #ff8b0f;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 14px;
        color: #333333;
        line-height: 20px;
        text-align: left;
        font-style: normal;
      }
    }

    .menuList {
      margin-top: 20px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .item {
        margin-bottom: 20px;

        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        > .icon {
          width: 50px;
          height: 50px;
          text-align: center;
          border-radius: 12px;
          display: flex;
          justify-content: center;
          align-items: center;

          > .iconfont {
            font-weight: 300;
            font-size: 35px;
            color: #000;
          }

          &:hover {
            background-color: #1478f6 !important;

            > .iconfont {
              color: #fff !important;
            }
          }
        }

        > .title {
          margin-top: 10px;
          color: #333333;
          font-size: 16px;
          font-weight: 600;
          user-select: none;
        }
      }

      .actived {
        background-color: #1478f6 !important;

        > .iconfont {
          color: #fff !important;
        }
      }
    }
  }

  .content {
    margin-left: 180px;
    flex: 1;
    width: calc(100% - 190px);
    border-radius: 16px;
  }
}
</style>
