<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.realName }}
        </span>
      </span>
    </span>

    <template #overlay>
      <Menu @click="handleMenuClick">
        <MenuItem
          key="personal"
          :text="t('layout.header.dropdownItemPersonal')"
          icon="ion:document-text-outline"
        />
        <MenuItem
          v-if="hasPermission('system:password:view')"
          key="changePassword"
          :text="t('layout.header.dropdownItemChangePassword')"
          icon="ant-design:unlock-twotone"
        />
        <!-- <MenuItem
          key="aboutSoftware"
          :text="handleDecode(t('routes.aboutSoftware.aboutSoftware'))"
          icon="ant-design:message-outline"
        /> -->
        <MenuItem
          v-if="getUseLockPage"
          key="lock"
          :text="t('layout.header.tooltipLock')"
          icon="ion:lock-closed-outline"
        />
        <MenuItem
          key="logout"
          :text="t('layout.header.dropdownItemLoginOut')"
          icon="ion:power-outline"
        />
      </Menu>
    </template>
  </Dropdown>
  <LockAction @register="register" />
  <PersonalChild
    @refreshPersonal="refreshPersonalFunc"
    ref="personalRef"
    @register="registerPersonal"
  />
  <AboutSoftwareModal @register="registerModal" />
</template>
<script lang="ts">
  // components
  import { Dropdown, Menu } from 'ant-design-vue';
  import { defineComponent, computed, ref, reactive } from 'vue';
  import { useUserStore } from '/@/store/modules/user';
  import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { useModal } from '/@/components/Modal';
  import headerImg from '/@/assets/images/logo.png';
  import { propTypes } from '/@/utils/propTypes';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import { USER_INFO_KEY } from '/@/enums/cacheEnum';
  import { getAuthCache } from '/@/utils/auth';
  import { useRouter } from 'vue-router';
  import { usePermission } from '/@/hooks/web/usePermission';
  import AboutSoftwareModal from '../AboutSoftwareModal.vue';
  import { AesEncryption } from '/@/utils/cipher';
  import { cacheCipher } from '/@/settings/encryptionSetting';

  type MenuEvent = 'logout' | 'doc' | 'lock' | 'personal' | 'changePassword' | 'aboutSoftware';

  export default defineComponent({
    name: 'UserDropdown',
    components: {
      Dropdown,
      Menu,
      MenuItem: createAsyncComponent(() => import('./DropMenuItem.vue')),
      LockAction: createAsyncComponent(() => import('../lock/LockModal.vue')),
      PersonalChild: createAsyncComponent(() => import('../personal/index.vue')),
      AboutSoftwareModal,
    },
    props: {
      theme: propTypes.oneOf(['dark', 'light']),
    },
    setup() {
      //修复修改密码根据后端返回的权限标识来显隐
      const { hasPermission } = usePermission();
      //修复修改密码根据后端返回的权限标识来显隐
      const refreshPersonlData = reactive({
        avatar: '',
        realName: '',
      });
      const updataPersonlData = reactive({
        avatar: '',
        realName: '',
      });
      const userInfo = getAuthCache(USER_INFO_KEY);
      const personalRef = ref(null);
      const { prefixCls } = useDesign('header-user-dropdown');
      const { t } = useI18n();
      const { getShowDoc, getUseLockPage } = useHeaderSetting();
      const userStore = useUserStore();

      const getUserInfo = computed(() => {
        const { realName = '', avatar } = userStore.getUserInfo || {};
        return { realName, avatar: avatar || headerImg };
      });

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

      function handleLock() {
        openModal(true);
      }

      const [registerModal, { openModal: openAboutSoftwareModal }] = useModal();
      function handleOpenAboutSoftwareModal() {
        openAboutSoftwareModal();
      }

      //  login out
      function handleLoginOut() {
        userStore.confirmLoginOut();
      }
      function handleMenuClick(e: { key: MenuEvent }) {
        switch (e.key) {
          case 'logout':
            handleLoginOut();
            break;
          case 'personal':
            openPersonalFunc();
            break;
          case 'lock':
            handleLock();
            break;
          case 'changePassword':
            changePassword();
            break;
          case 'aboutSoftware':
            handleOpenAboutSoftwareModal();
        }
      }

      const openPersonalFunc = () => {
        setTimeout(() => {
          openModalPersonal(true, {
            userInfo,
          });
        }, 10);
      };
      const refreshCacheGetData = () => {
        const getItem = window.localStorage.getItem('updateUserInfo');
        const newItem = JSON.parse(getItem);
        updataPersonlData.avatar = newItem?.avatar;
        updataPersonlData.realName = newItem?.realName;
      };
      refreshCacheGetData();
      const refreshPersonalFunc = (v) => {
        refreshPersonlData.avatar = v.avatar;
        refreshPersonlData.realName = v.realName;
      };
      const router = useRouter();
      const changePassword = () => {
        router.push('/system/changePassword');
      };

      const encryption = new AesEncryption(cacheCipher);
      const handleDecode = (string: string) => {
        return encryption.decryptByAES(string);
      };

      return {
        handleDecode,
        updataPersonlData,
        refreshPersonlData,
        refreshPersonalFunc,
        personalRef,
        registerPersonal,
        openPersonalFunc,
        prefixCls,
        t,
        getUserInfo,
        handleMenuClick,
        getShowDoc,
        register,
        getUseLockPage,
        hasPermission,
        registerModal,
      };
    },
  });
</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>
