<template>
  <div class="user-wrapper">
    <div class="content-box">
      <p class="help-cont" v-if="showHelp" @click="showVideo">
        <img style="width: 22px;height: 22px;vertical-align: middle;margin: 0 5px;" src="../../assets/icon_movie.png"/>
        <span style="vertical-align: middle;">帮助</span>
      </p>
      <notice-icon class="action"/>
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <!-- <a-avatar class="avatar" size="small" :src="avatar==''?defaultAvatar:avatar"/> -->
          <a-icon type="user" />
          <span style="margin: 0 5px;">{{ nickname }} - 【{{ userInfo.orgName || '--' }}】</span>
          <a-icon type="down" />
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item v-for="(item, index) in authOrgsList" :key="index" @click="changeRoles(item.orgSn)">
            <a-icon type="link" style="vertical-align: super;"/>
            <span style="display: inline-block;white-space: nowrap;width: 100%;overflow: hidden;text-overflow:ellipsis;">{{ item.orgName }}</span>
          </a-menu-item>
          <a-menu-divider/>
          <a-menu-item key="5">
            <router-link :to="{ name: 'changePwd' }">
              <a-icon type="user"/>
              <span>修改密码</span>
            </router-link>
          </a-menu-item>
          <a-menu-item key="6">
            <a href="javascript:;" @click="handleLogout">
              <a-icon type="logout"/>
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
      <a-icon type="fullscreen" title="全屏" :style="{ fontSize: '20px', color: '#08c', marginLeft: '5px' }" @click="screen" />
    </div>
  </div>
</template>

<script>
import screenfull from 'screenfull'
import NoticeIcon from '@/components/NoticeIcon'
import { mapActions, mapGetters } from 'vuex'
import defaultAvatar from '@/assets/avatar2.jpg'
import store from '../../store'
import Vue from 'vue'
export default {
  name: 'UserMenu',
  components: {
    NoticeIcon
  },
  computed: {
    ...mapGetters(['nickname', 'avatar', 'authOrgs', 'userInfo', 'nowRoute']),
    authOrgsList () { //  过滤掉当前登录账户(不可由自己切换为自己)
      const _this = this
      const arr = []
      if (this.authOrgs && this.authOrgs.length > 0) {
        this.authOrgs.map(item => {
          if (item.orgSn != _this.userInfo.orgSn) {
            arr.push(item)
          }
        })
      }
      return arr
    },
    // 是否显示帮助视频按钮
    showHelp () {
      const isSaleOrder = this.nowRoute.indexOf('salesQuery') > -1 // 当前展示是否为销售单模块
      const isSalesReturn = this.nowRoute.indexOf('salesReturn') > -1 // 当前展示是否为销售退货模块
      const isPurchaseOrder = this.nowRoute.indexOf('purchaseOrder') > -1 // 当前展示是否为采购单管理模块
      const isPurchaseReturn = this.nowRoute.indexOf('purchaseReturn') > -1 // 当前展示是否为采购退货模块
      return isSaleOrder || isSalesReturn || isPurchaseOrder || isPurchaseReturn
    }
  },
  data () {
    return {
      defaultAvatar: defaultAvatar,
      isFullscreen: false
    }
  },
  methods: {
    ...mapActions(['Logout', 'SetOrgSn', 'Login']),
    // 打开视频播放弹窗
    showVideo () {
      store.commit('IS_ShOW_VIDEO', true)
    },
    handleLogout () {
      this.$confirm({
        title: '提示',
        content: '确定要退出登录吗 ?',
        centered: true,
        onOk: () => {
          return this.Logout({}).then(() => {
            setTimeout(() => {
              // window.location.reload()
              this.$router.push({ path: '/user/login' })
            }, 16)
          }).catch(err => {
            this.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel () {
        }
      })
    },
    screen () {
      // 如果不允许进入全屏，发出不允许提示
      if (!screenfull.isEnabled) {
        this.$message.info('您的浏览器不能全屏')
        return false
      }
      screenfull.toggle()
    },
    // 切换角色
    changeRoles (sn) {
      return this.SetOrgSn(sn).then(() => {
        // 重新登录
        const params = { username: '', password: '' }
        return this.Login(params).then(() => {
          store.commit('SET_ROLES', Vue.ls.get('rolesAccess-qpls-md'))
          this.$router.push({ path: '/home' })
          setTimeout(() => {
            window.location.reload() //  刷新页面
          }, 100)
        }).catch(err => this.requestFailed(err))
      })
    },
    requestFailed (err) {
      console.log(err, 'error')
      this.isLoginError = true
      this.$notification['error']({
        message: '提示',
        description: ((err.response || {}).data || {}).message || err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>
<style scoped="scoped">
.help-cont{
	display: inline-block;
	cursor: pointer;
	transition: all 0.3s;
	padding: 0 5px;
  margin: 0;
  vertical-align: bottom;
}
.help-cont:hover {
	background-color: rgba(0, 0, 0, 0.04);
}
</style>
