<template>
  <el-dropdown trigger="click">
    <div class="userInfo">  
      <div class="icon">
        <Icon><Avatar /></Icon>
      </div>
      <div class="username">
        {{ username }}
      </div>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="openDialog('infoRef')">
          <el-icon><User /></el-icon>{{ $t("header.personalData") }}
        </el-dropdown-item>
        <el-dropdown-item @click="openDialog('passwordRef')">
          <el-icon><Edit /></el-icon>{{ $t("header.changePassword") }}
        </el-dropdown-item>
        <el-dropdown-item @click="changeMode">
          <el-icon><Edit /></el-icon>{{ $t("header.changeMode") }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <div class="avatar">
    <img src="@/assets/icons/out_login.svg" alt="avatar" @click="logout" />
  </div>
  <!-- infoDialog -->
  <InfoDialog ref="infoRef"></InfoDialog>
  <!-- passwordDialog -->
  <PasswordDialog ref="passwordRef"></PasswordDialog>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { LOGIN_URL } from "@/config";
import { useRouter } from "vue-router";
import { logoutApi } from "@/api/modules/login";
import { useUserStore } from "@/stores/modules/user";
import { ElMessageBox, ElMessage } from "element-plus";
import InfoDialog from "./InfoDialog.vue";
import PasswordDialog from "./PasswordDialog.vue";
import { computed } from "vue";
import { Avatar } from "@element-plus/icons-vue";
import AssemblySize from "./components/AssemblySize.vue";
import Language from "./components/Language.vue";
import SearchMenu from "./components/SearchMenu.vue";
import ThemeSetting from "./components/ThemeSetting.vue";
import Message from "./components/Message.vue";
import Fullscreen from "./components/Fullscreen.vue";
import { useAuthStore } from "@/stores/modules/auth";
const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);
const router = useRouter();
const authStore = useAuthStore();
// 退出登录
const logout = () => {
  ElMessageBox.confirm("您是否确认退出登录?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    // 1.执行退出登录接口
    await logoutApi();
    // 2.清除 Token
    userStore.setToken("");
    // 3.重定向到登陆页
    router.replace(LOGIN_URL);
    //重置菜单/导航栏权限
    authStore.resetAuthStore();
    ElMessage.success("退出登录成功！");
  });
};

// 打开修改密码和个人信息弹窗
const infoRef = ref<InstanceType<typeof InfoDialog> | null>(null);
const passwordRef = ref<InstanceType<typeof PasswordDialog> | null>(null);
const openDialog = (ref: string) => {
  if (ref == "infoRef") infoRef.value?.openDialog();
  if (ref == "passwordRef") passwordRef.value?.openDialog();
};
//模式切换
const changeMode = () => {
  authStore.changeModel();
};
</script>

<style scoped lang="scss">
.userInfo {
  min-width: 80px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  cursor: pointer;
  .icon {
    width: 18px;
    height: 18px;
    color: #fff !important;
  }
  .username {
    color: #fff;
    font-size: 16px;
    margin-left: 10px;
  }
}

.avatar {
  width: 40px;
  height: 40px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  cursor: pointer;
  // border-radius: 50%;
  img {
    // width: 100%;
    // height: 100%;
  }
}
</style>
