<template>
  <Dropdown placement="bottomLeft" :overlayClassName="`${prefixCls}-dropdown-overlay`">
    <span :class="[prefixCls, `${prefixCls}--${theme}`]" class="flex">
      <img :class="`${prefixCls}__header`" :src="getUserInfo.avatar" />
      <span :class="`${prefixCls}__info hidden md:block`">
        <span :class="`${prefixCls}__name  `" class="truncate">
          {{ getUserInfo.nickname || 'test' }}
        </span>
      </span>
    </span>

    <template #overlay>
      <Menu @click="handleMenuClick">
        <MenuDivider v-if="getShowDoc" />
        <!-- <MenuItem key="accountCenter" :text="'个人中心'" icon="grommet-icons:user" /> -->
        <MenuItem key="accountSetting" :text="'个人中心'" icon="grommet-icons:user-settings" />
        <MenuItem key="clear" text="清空缓存" icon="rest|iconfont" />
        <MenuItem key="logout" text="退出" icon="poweroff|iconfont" />
      </Menu>
    </template>
  </Dropdown>
  <LockAction @register="register" />
</template>
<script lang="ts">
  // components
  import { Dropdown, Menu } from 'ant-design-vue';

  import { defineComponent, computed } from 'vue';

  import { DOC_URL } from '/@/settings/siteSetting';

  import { useUserStore } from '/@/store/modules/user';
  import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { useModal } from '/@/components/Modal';

  import headerImg from '/@/assets/images/header.png';
  import { propTypes } from '/@/utils/propTypes';
  import { openWindow } from '/@/utils';

  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import { useGo } from '/@/hooks/web/usePage';
  import { createLocalStorage } from '/@/utils/cache';
  import { useMessage } from '/@/hooks/web/useMessage';

  type MenuEvent =
    | 'logout'
    | 'clear'
    | 'doc'
    | 'lock'
    | 'projectAddress'
    | 'accountCenter'
    | 'accountSetting';

  export default defineComponent({
    name: 'UserDropdown',
    components: {
      Dropdown,
      // eslint-disable-next-line vue/no-reserved-component-names
      Menu,
      MenuItem: createAsyncComponent(() => import('./DropMenuItem.vue')),
      MenuDivider: Menu.Divider,
      LockAction: createAsyncComponent(() => import('../lock/LockModal.vue')),
    },
    props: {
      theme: propTypes.oneOf(['dark', 'light']),
    },
    setup() {
      const { prefixCls } = useDesign('header-user-dropdown');
      const { getShowDoc, getUseLockPage } = useHeaderSetting();
      const userStore = useUserStore();

      const ls = createLocalStorage();
      const { createMessage } = useMessage();

      const getUserInfo = computed(() => {
        const { nickname = '', avatar, desc } = userStore.getUserInfo || {};
        return { nickname, avatar: avatar?.includes('png') ? avatar : headerImg, desc };
      });

      const [register, { openModal }] = useModal();

      const go = useGo();

      function handleLock() {
        openModal(true);
      }

      //  login out
      function handleLoginOut() {
        userStore.confirmLoginOut();
      }

      // 清空缓存
      function handleClear() {
        ls.clear();
        createMessage.success('清除缓存成功');
      }

      // open doc
      function openDoc() {
        openWindow(DOC_URL);
      }

      // account center
      function handleClickAccountCenter() {
        go('/account/center');
      }

      // account setting
      function handleClickAccountSetting() {
        go('/sys/accountCenter');
      }

      function handleMenuClick(e: { key: MenuEvent }) {
        switch (e.key) {
          case 'clear':
            handleClear();
            break;
          case 'logout':
            handleLoginOut();
            break;
          case 'doc':
            openDoc();
            break;
          case 'accountCenter':
            handleClickAccountCenter();
            break;
          case 'accountSetting':
            handleClickAccountSetting();
            break;
          case 'lock':
            handleLock();
            break;
        }
      }

      return {
        prefixCls,
        getUserInfo,
        handleMenuClick,
        getShowDoc,
        register,
        getUseLockPage,
      };
    },
  });
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-header-user-dropdown';

  .@{prefix-cls} {
    height: @header-height;
    padding: 0 0 0 10px;
    padding-right: 10px;
    overflow: hidden;
    font-size: 12px;
    cursor: pointer;
    align-items: center;

    img {
      width: 24px;
      height: 24px;
      margin-right: 12px;
    }

    &__header {
      border-radius: 50%;
    }

    &__name {
      font-size: 14px;
    }

    &--dark {
      &:hover {
        background-color: @header-dark-bg-hover-color;
      }
    }

    &--light {
      &:hover {
        background-color: @header-light-bg-hover-color;
      }

      .@{prefix-cls}__name {
        color: @text-color-base;
      }

      .@{prefix-cls}__desc {
        color: @header-light-desc-color;
      }
    }

    &-dropdown-overlay {
      .ant-dropdown-menu-item {
        min-width: 160px;
      }
    }
  }
</style>
