<template>
  <div class="user-avatar-dropdown">
    <Dropdown trigger="custom" :visible="visible" @click.native="handleOpen">
      <Badge :dot="!!messageUnreadCount">
        <Avatar v-if="userAvatar!=''" :src="userAvatar"/>
        <Avatar v-else icon="ios-person"/>
        <span style="font-size: 15px;margin:4px 8px 0">{{userName}}</span>
      </Badge>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list" >
        <div class="personinfo">
          <div class="personavatar">
            <Avatar  class="useravtar" v-if="userAvatar!=''" :src="userAvatar"/>
            <Avatar  class="useravtar" v-else icon="ios-person"/>
          </div>
          <div class="username"> {{userName}}</div>
          <div class="userinfo flex">
            <div class="userinfo-depname" style="text-align: right;">
              <span style=" color: #727475;">部门/科室</span>：
            </div>
            <div class="userinfo-depname">{{depName}}</div>
            <div class="userinfo-depname" style="text-align: right;">
              <span style=" color: #727475;">职务</span>：
            </div>
            <div class="userinfo-depname">
              {{postNameAndRankName}}
            </div>
          </div>
        </div>
        <div class="flex">
          <div v-if="this.$config.deploymentStandAlone" class="bt" @click="handleClick('logout')">
            <Icon type="md-power" />
            退出登录
          </div>
          <div v-else class="bt" @click="handleClick('logoutPT')">
            <Icon type="md-power" />
            退出登录
          </div>
          <div class="bt" v-if="this.$config.deploymentStandAlone" @click="handleClick('toEditPwd')">
            <Icon type="md-lock" />
            修改密码
          </div>
          <div class="bt" v-else @click="handleClick('returnToCas')">返回平台</div>
          <div class="bt" @click="handleClick('clearCache')">清除缓存</div>
        </div>
      </DropdownMenu>
    </Dropdown>
    <change-pwdModal ref="changePwdModal"  :show.sync="showModal"></change-pwdModal>
  </div>
</template>
<script>
import './user.less'
import { mapActions } from 'vuex'
import  changePwdModal  from './changepwd-modal.vue'
import  bcDictUtil  from '../../../bc-dict/dict-util.js'
export default {
  components: {
    changePwdModal
  },
  name: 'User',
  props: {
    userAvatar: {
      type: String,
      default: ''
    },
    messageUnreadCount: {
      type: Number,
      default: 0
    },
    userName: {
      type: String,
      default: ''
    },
    depName: {
      type: String,
      default: ''
    },
    postNameAndRankName: {
      type: String,
      default: ''
    },
  },
  data () {
    return {
      visible:false,
      showModal: false,
    }
  },
  methods: {
    handleOpen(){
      this.visible = !this.visible
    },
    ...mapActions([
      'handleLogOut'
    ]),
    toHome () {
      this.$router.push({
        name: 'home'
      })
    },
    returnToCas () {
      this.$Modal.confirm({
        title: '系统提示',
        width: '360',
        loading:true,
        content: '<p>确定返回平台吗？</p>',
        onCancel: () => {
        },
        onOk: () => {
          window.location.href = this.$config.cas_ui_url + '?token=' + this.$store.state.user.token + '&refresh_token=' + this.$store.state.user.refreshToken;
        }
      });
    },
    logout () {
      this.handleLogOut().then(() => {
        this.$router.push({
          name: 'login'
        })
      })
    },
    logoutPT () {
      this.$Modal.confirm({
        title: '系统提示',
        width: '360',
        loading:true,
        content: '<p>确定退出登录吗？</p>',
        onCancel: () => {

        },
        onOk: () => {
          sessionStorage.removeItem("areaList");
          window.location.href= this.$config.logout_url;
        }
      });
    },
    message () {
      this.$router.push({
        name: 'message_page'
      })
    },
    toSystemLicense () {
      this.$router.push({
        name: 'system-license'
      })
    },
    toEditPwd () {
      this.showModal = true;
    },
    clearCache() {
      //先清字典
      bcDictUtil.clearCacheAll()
      this.$Message.info("缓存清除完毕")
    },
    handleClick (name) {
      switch (name) {
        case 'logout': this.logout()
          break
        case 'logoutPT': this.logoutPT()
          break
        case 'message': this.message()
          break
        case 'toHome': this.toHome()
          break
        case 'returnToCas': this.returnToCas()
          break
        case 'toSystemLicense': this.toSystemLicense()
          break
        case 'toEditPwd': this.toEditPwd()
          break
        case 'clearCache': this.clearCache()
          break
      }
    }
  }
}
</script>
