<template>
  <a-dropdown overlayClassName="header-right-dropdown" v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar size="small" :src="userIcon" class="antd-pro-global-header-index-avatar" />
      <template v-if="currentUser && currentUser.enterpriseName != ''">
        <div style="display: inline-block; line-height: 16px; position: relative; top: 10px">
          <div style="font-size: 14px">{{ currentUser.name }}</div>
          <div style="font-size: 12px; color: rgb(92 186 241); font-style: italic">
            @{{ currentUser.enterpriseName }}
          </div>
        </div>
      </template>
      <template v-else>
        <span>{{ currentUser.name }}</span>
      </template>
      <system-info-modal v-model="visible" />
    </span>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
          <plg-icon type="setting" :pstyle="{ color: themeColor }" />
          {{ $lang('个人设置') }}
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item v-if="menu" key="system-settings" @click="systemSettings">
          <plg-icon type="iconfont iconui" :pstyle="{ color: themeColor }" />
          {{ $lang('界面设置') }}
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item v-if="menu" key="system-info" @click="showSystemInfoModal">
          <plg-icon type="control" :pstyle="{ color: themeColor }" />
          {{ $lang('系统信息') }}
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item key="logout" @click="handleLogout">
          <plg-icon type="logout" :pstyle="{ color: themeColor }" />
          {{ $lang('退出登录') }}
        </a-menu-item>
      </a-menu>
    </template>
    
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'
import userIcon from '@/assets/default_user_icon.jpg'
import SystemInfoModal from "./SystemInfoModal.vue";

export default {
  name: 'AvatarDropdown',
  components: {
    'system-info-modal': SystemInfoModal
  },
  props: {
    currentUser: {
      type: Object,
      default: () => null,
    },
    menu: {
      type: Boolean,
      default: true,
    },
    themeColor: {
      type: String,
      default: '',
      required: false,
    },
  },
  data() {
    return {
      userIcon,
      visible: false
    }
  },
  methods: {
    systemSettings() {
      this.$emit('systemSettings')
    },
    handleToCenter() {
      this.$router.push({ path: '/account/center' })
    },
    handleToSettings() {
      this.$router.push({ path: '/account/settings' })
    },
    handleLogout(e) {
      Modal.confirm({
        title: this.$lang('信息'),
        content: this.$lang('您确定要注销吗？'),
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch('Logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel() {},
      })
    },
    showSystemInfoModal(){
      this.visible = true;
    },
  },
}
</script>


