<template>
  <div class="user-wrapper">
    <div class="content-box">
      <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>
    </div>
  </div>
</template>

<script>
import NoticeIcon from '@/components/NoticeIcon'
import { mapActions, mapGetters } from 'vuex'
import defaultAvatar from '@/assets/avatar2.jpg'
export default {
  name: 'UserMenu',
  components: {
    NoticeIcon
  },
  computed: {
    ...mapGetters(['nickname', 'avatar', 'authOrgs', 'userInfo']),
    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
    }
  },
  data () {
    return {
      defaultAvatar: defaultAvatar
    }
  },
  methods: {
    ...mapActions(['Logout', 'SetOrgSn', 'Login']),
    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 () {
        }
      })
    },
    // 切换角色
    changeRoles (sn) {
      return this.SetOrgSn(sn).then(() => {
        // 重新登录
        const params = { username: '', password: '' }
        return this.Login(params).then(() => {
          window.location.reload() //  刷新页面
        }).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>
