<template>
  <div :class="wrpCls">
    <!-- <a-icon type="question-circle" class="icon"/> -->
    <a-badge :count="99" class="icon">
      <a-icon type="bell" />
    </a-badge>
    <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
      <span class="ant-pro-account-avatar">
        <a-avatar size="small" src="https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png" class="antd-pro-global-header-index-avatar" />
        <span>{{ currentUser.name }}</span>
      </span>
      <template v-slot:overlay>
        <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
          <a-menu-item v-if="menu" key="center" @click="handleToCenter">
            <a-icon type="unlock" />
            修改密码
          </a-menu-item>
          <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
            <a-icon type="qq" />
            在线客服
          </a-menu-item>
          <a-menu-divider v-if="menu" />
          <a-menu-item key="logout" @click="handleLogout">
            <a-icon type="poweroff" />
            退出登录
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
    <span v-else>
      <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
    </span>
  </div>
</template>

<script>
  import {
    Modal
  } from 'ant-design-vue'
  import store from '@/store'

  export default {
    name: 'AvatarDropdown',
    props: {
      menu: {
        type: Boolean,
        default: true
      }
    },
    data () {
      return {
        showMenu: true,
        currentUser: {
          name: store.getters.nickname
        }
      }
    },
    computed: {
      wrpCls () {
        return {
          'ant-pro-global-header-index-right': true,
          [`ant-pro-global-header-index-${(this.isMobile || !this.topMenu) ? 'light' : this.theme}`]: true
        }
      }
    },
    methods: {
      handleToCenter () {
        this.$router.push({
          path: '/user/password'
        })
      },
      handleToSettings () {
        this.$router.push({
          path: '/user/chat'
        })
      },
      handleLogout (e) {
        Modal.confirm({
          title: '提示',
          content: '确定退出登录?',
          cancelText:'取消',
          okText:'确定',
          onOk: () => {
            // return new Promise((resolve, reject) => {
            //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
            // }).catch(() => console.log('Oops errors!'))
            window.localStorage.removeItem('wifitoken')
            this.$router.push({
              name: 'login'
            })
            // return this.$store.dispatch('Logout').then(() => {
            //   this.$router.push({
            //     name: 'login'
            //   })
            // })
          },
          onCancel () {}
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .ant-pro-drop-down {
    /deep/ .action {
      margin-right: 8px;
    }

    /deep/ .ant-dropdown-menu-item {
      min-width: 160px;
    }
    /deep/ .ant-dropdown-menu {
      right: 20px
    }
  }
  .icon{
    font-size: 20px;
    margin-right: 50px;
  }
</style>
