<template>
  <div class="navbar">
    <div class="bizIdSet">
      <!-- <hamburger :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" /> -->
      <!-- <breadcrumb class="breadcrumb-container" /> -->
      <el-button v-show="status" :disabled="showBizId" type="primary" @click="changeBizId">
        {{ sumType }}
      </el-button>
    </div>
    <div>
      <div class="right-menu" style="display: fiex; align-items: center; justify-content: space-between;">
        <el-avatar style="vertical-align: middle; background: #FFF;" size="small"><img :src="options.phoneUrl"></el-avatar>
        <el-dropdown placement="bottom" trigger="click" @visible-change="visibleChange">
          <span class="el-dropdown-link avatar-container" style="color: #FFFFFF; cursor: pointer;" @click="show2 = !show2">
            {{ username }}
            <i class="el-icon-arrow-right" :class="{rotateNone:show2,rotate:!show2}" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="Logout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { needIdList } from '@/router/needIdList'
import { EncryptId, desDecryptId } from '@/utils/crypto-js.js'
// import Breadcrumb from '@/components/Breadcrumb' // 面包屑
// import Hamburger from '@/components/Hamburger'  // 导航开关
import { logoutUrl } from '@/apiConfig/requestIP.js'
import { memberGetLoginInMemberInfoByLdap, settingGetBizList, settingUserSetBiz, settingUserGetBiz } from '@/api/projectIndex'

export default {
  // components: {
  //   Breadcrumb
  //   Hamburger
  // },
  data() {
    return {
      show2: false,
      options: {},
      value: '',
      arr_data: [],
      sumTypeArray: [],
      sumType: '',
      formulaArea: '', // 公式的文本框
      typeObj: {},
      showBizId: false
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'status',
      'bizId'
    ]),
    username() {
      return localStorage.getItem('realname')
    }
  },
  watch: {
    $route: {
      async handler(to, from) {
        // 如果是业务线选择页面，不作处理
        if (to.name === '业务线选择') return
        // 如果与原路由相同，不作处理
        if (from && to.path === from.path) return
        // 如果业务线列表不存在，请求获取路由业务线列表
        if (this.sumTypeArray.length < 1) await this.settingGetBizList()
        // 如果参数中bizId和bizId_id都不存在，去获取默认bizId
        if (!to.query.bizId && !to.query.bizId_id) {
          this.settingUserGetBiz()
          return
        }
        const existBizId_id = needIdList.find(item => item === to.name)
        existBizId_id ? this.handlerIdAndBizId() : this.handlerBizId()
      },
      immediate: true
    }
  },
  created() {
    this.getLoginMember()
  },
  methods: {
    handlerBizId() { // 只处理bizId
      const bizId = Number(desDecryptId(this.$route.query.bizId))
      const isExistBizId = this.sumTypeArray.find(item => bizId === item.code)// 业务线id是否存在
      if (isExistBizId) {
        this.$store.dispatch('data/setBizId', bizId)
        this.sumType = isExistBizId.name
      } else {
        this.settingUserGetBiz()
      }
      console.log('mainbizId', this.bizId)
    },
    handlerIdAndBizId() { // 处理bizId和id
      const bizId_id = this.$route.query.bizId_id
      const arr = desDecryptId(bizId_id).split('_')
      const isExistBizId = this.sumTypeArray.find(item => Number(arr[0]) === item.code)// 业务线id是否存在
      if (isExistBizId) {
        this.$store.dispatch('data/setBizId', Number(arr[0]))
        this.sumType = isExistBizId.name
      } else {
        this.settingUserGetBiz()
      }
      console.log('mainbizId', this.bizId)
    },
    async settingGetBizList() { // 获取业务线列表
      const res = await settingGetBizList({})
      this.sumTypeArray = res.data || []
    },
    async settingUserGetBiz() { // 获取人员默认业务线
      const res = await settingUserGetBiz()
      const nowBiz = this.sumTypeArray.find(item => res.data.bizId === item.code)
      if (nowBiz) {
        this.sumType = nowBiz.name
        this.$store.dispatch('data/setBizId', nowBiz.code)
        localStorage.setItem('bizId', nowBiz.code)
        const existBizId_id = needIdList.find(item => item === this.$route.name)
        if (!existBizId_id) { // 如果不是在需要bizId_id的详情页，就去给路由添加bizId
          this.$router.replace({
            path: this.$route.path,
            query: { ...this.$route.query, bizId: EncryptId(nowBiz.code) }
          })
        }
      } else {
        console.log('重定向到业务线选择页面')
      }
    },
    async remoteMethod(query) {
      if (query !== '') {
        const res = await settingGetBizList({ bizName: query })
        this.sumTypeArray = res.data
      } else {
        this.get_bizArr()
      }
    },
    changeBizId(type) { // 下拉列表的点击事件x
      this.$router.push({ name: '业务线选择' })
      // this.sumType = type.name
      // this.$store.dispatch('data/setBizId', type.code)
      // this.settingUserSetBiz(type.code)
      // const name = this.$route.name
      // const isDetailPage = needIdList.find(item => item === name)
      // if (isDetailPage) {
      //   this.$router.push({ name: name.substr(0, 2), query: { bizId: EncryptId(type.code) }})
      // } else {
      //   this.$router.push({
      //     path: this.$route.path,
      //     query: { ...this.$route.query, bizId: EncryptId(type.code) }
      //   })// 更新路由连接
      //   this.$nextTick(() => { this.$router.go(0) }) // 重载页面
      // }
    },
    async settingUserSetBiz(bizId) { // 设置成员登录业务线
      await settingUserSetBiz({ bizId: bizId })
    },
    async getLoginMember() { // 获取登录人员信息
      const res = await memberGetLoginInMemberInfoByLdap()
      this.options = res.data
      localStorage.setItem('UserAvatar', this.options.phoneUrl)
    },
    visibleChange(e) {
      if (!e) {
        this.show2 = false
      }
    },
    Logout() {
      location.href = logoutUrl
    }
  }
}
</script>
<style lang="stylus">
.bizIdSet .el-button--primary.is-disabled, .el-button--primary.is-disabled:active, .el-button--primary.is-disabled:focus, .el-button--primary.is-disabled:hover {
    color: #FFF;
    background-color: #409EFF;
    border-color: #409EFF;
}
</style>

<style lang="scss" scoped>
.navbar .rotateNone {
  transform: rotate(270deg);
}
.navbar img {
    margin: 0 auto;
}
.navbar .rotate {
  transform: rotate(90deg);
}
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 55px;
  overflow: hidden;
  position: relative;
  background: #409EFF;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
    /deep/ .el-breadcrumb__separator {
      color:white !important
    }
  }

  .right-menu {
    &:focus {
      outline: none;
    }

    .right-menu-item {
      // display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #c0c8d8;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
