<template>
  <el-dropdown trigger="click">
    <div class="avatar">
      <img :src="userStore.avatar" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="toProfile()">
          <el-icon><User /></el-icon>{{ $t('header.personalCenter') }}
        </el-dropdown-item>
        <el-dropdown-item divided @click="openDrawer()">
          <el-icon><Files /></el-icon>导入导出
        </el-dropdown-item>
        <el-dropdown-item divided @click="logout">
          <el-icon><SwitchButton /></el-icon>{{ $t('header.logout') }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <InfoDialog ref="infoRef" />
  <IEDrawer ref="drawerRef" />
</template>

<script setup lang="ts">
import { LOGIN_URL } from '@/config'
import { useUserStore } from '@/stores/modules/user'
import { ElMessageBox, ElMessage } from 'element-plus'
import InfoDialog from './InfoDialog.vue'
import IEDrawer from '@/views/import-export/index.vue'
const router = useRouter()
const userStore = useUserStore()

const toProfile = () => {
  router.push('/system/user/profile')
}
// 退出登录
const logout = () => {
  ElMessageBox.confirm('您是否确认退出登录?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // 1.执行退出登录接口
    await userStore.logOut()

    // 3.重定向到登陆页
    router.replace(LOGIN_URL)
    ElMessage.success('退出登录成功！')
  })
}

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof IEDrawer> | null>(null)
const openDrawer = () => {
  drawerRef.value?.acceptParams()
}
</script>

<style scoped lang="scss">
.avatar {
  width: 40px;
  height: 40px;
  overflow: hidden;
  cursor: pointer;
  border-radius: 50%;
  img {
    width: 100%;
    height: 100%;
  }
}

// .avatar-dft {
//   width: 40px;
//   height: 40px;
//   overflow: hidden;
//   cursor: pointer;
//   border-radius: 50%;
//   img {
//     width: 100%;
//     height: 100%;
//     color: var(--color);
//     filter: drop-shadow(var(--el-menu-active-color) 100px 0);
//     fill: currentColor;
//     transform: translateX(-100px);
//   }
// }
</style>
