<template>
  <div class="navbar">
    <div class="bizIdSet">
      <!-- <hamburger :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" /> -->
      <!-- <breadcrumb class="breadcrumb-container" /> -->
      <el-dropdown v-show="status" placement="bottom" trigger="click" @command="sumTypeCkeck">
        <el-button :disabled="showBizId" type="primary">
          {{ sumType }}<i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <div style="margin: 0 10% 10px;">
            <el-input v-model="value" clearable size="small" style="width: 100%" placeholder="业务线搜索" @input="remoteMethod" />
          </div>
          <div style="max-height: 200px; overflow:scroll; overflow-x: hidden">
            <el-dropdown-item
              v-for="(item,index) in sumTypeArray"
              :key="index"
              :label="item.name"
              :value="item.code"
              :command="item"
              v-text="item.name"
            />
          </div>
        </el-dropdown-menu>
      </el-dropdown>
    </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 { 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) {
        await this.settingGetBizList()
        if (!to.query.bizId && !to.query.bizId_id) {
          this.settingUserGetBiz()
          return
        }// 如果参数中bizId和bizId_id都不存在，去获取默认bizId
        const existBizId_id = needIdList.find(item => item === to.name)
        existBizId_id ? this.handerIdAndBizId() : this.handerBizId()
      },
      immediate: true
    }
  },
  created() {
    this.getLoginMember()
  },
  methods: {
    handerBizId() { // 只处理bizId
      const bizId = Number(desDecryptId(this.$route.query.bizId))
      const isExistBizId = this.sumTypeArray.find(item => bizId === item.code)
      isExistBizId ? this.$store.dispatch('data/setBizId', bizId) : this.settingUserGetBiz()
    },
    handerIdAndBizId() { // 处理bizId和id
      const bizId_id = this.$route.query.bizId_id
      const arr = Number(desDecryptId(bizId_id)).split('_')
      const isExistBizId = this.sumTypeArray.find(item => arr[0] === item.code)
      isExistBizId ? this.$store.dispatch('data/setBizId', arr[0]) : this.settingUserGetBiz()
    },
    async remoteMethod(query) {
      if (query !== '') {
        const res = await settingGetBizList({ bizName: query })
        this.sumTypeArray = res.data
      } else {
        this.get_bizArr()
      }
    },
    async sumTypeCkeck(type) { // 下拉列表的点击事件x
      this.sumType = type.name
      await settingUserSetBiz({ bizId: type.code })
      this.$store.dispatch('data/setBizId', type.code)
      const url = window.location.href
      const urlArr = url.split('?')
      const k = urlArr[0]
      const appU = k.split('/')
      const name = appU[appU.length - 1]
      name === 'projectViewDetails' ? this.$router.push({ name: '项目', query: {}}) : ''
      name === 'taskViewDetails' ? this.$router.push({ name: '任务', query: {}}) : ''
      name === 'requirementDetails' ? this.$router.push({ name: '需求', query: {}}) : ''
      name === 'iterationDetails' ? this.$router.push({ name: '迭代', query: {}}) : ''
      if (name !== 'projectViewDetails' && name !== 'taskViewDetails' && name !== 'requirementDetails' && name !== 'iterationDetails') {
        window.location.reload()
      }
    },
    async settingGetBizList() { // 获取业务线列表
      const res = await settingGetBizList({})
      this.sumTypeArray = res.data || []
    },
    async settingUserGetBiz() { // 获取人员默认业务线
      const res = await settingUserGetBiz()
      if (res.data) {
        const nowBiz = this.sumTypeArray.find(item => res.data.bizId === item.code)
        if (nowBiz) {
          this.sumType = nowBiz.name
          this.$store.dispatch('data/setBizId', nowBiz.code)
        } else {
          console.log('重定向到业务线选择页面')
        }
      }
    },
    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>
