<template>
  <div class="manage-header">
    <div class="header-left">
      <a-breadcrumb class="breadcrumb">
        <a-breadcrumb-item>青橙校园后台管理系统</a-breadcrumb-item>
        <a-breadcrumb-item class="routeName">{{ route.name }}</a-breadcrumb-item>
      </a-breadcrumb>
    </div>
    <div class="header-right">
<!--      <div class="bell">-->
<!--        <BellTwoTone/>-->
<!--      </div>-->
      <div class="name">
        {{ store.loginUser.userRole === "notLogin" ? '未登录' : store.loginUser.userRole }}
      </div>
      <div class="user">
        <a-dropdown>
          <a-avatar :size="40" class="user-avatar" :src="store.loginUser.avatarUrl"/>
          <template #overlay>
            <a-menu>
              <a-menu-item @click="logout">
                <LogoutOutlined />
                退出登录
              </a-menu-item>
              <a-menu-item  @click="showDrawer">
                <UserOutlined />
                个人中心
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </div>
    </div>
  </div>

  <a-drawer
      title="个人中心"
      :placement="placement"
      :closable="false"
      :open="open"
      @close="onClose"
      class="custom-class"
  >
    <a-avatar :size="64" :src="store.loginUser.avatarUrl"></a-avatar>

    <div class="message">
      <p class="firstmessage">用户名：{{ store.loginUser.username }}</p>
      <p>账号ID：{{ store.loginUser.username }}</p>
      <p>注册时间：2024-06-20</p>
      <p>账号权限：{{store.loginUser.userRole}}</p>
      <p>联系方式：{{store.loginUser.phone}}</p>
    </div>


  </a-drawer>
</template>

<script lang="ts" setup>
import {useRoute, useRouter} from "vue-router";
import {onBeforeMount} from 'vue'
import {userStore} from "../../store/userStore.ts";
import {UserOutlined,LogoutOutlined} from '@ant-design/icons-vue';
import myAxios from "../../api/myAxios.ts";
import {message} from "ant-design-vue";
import { ref } from 'vue';
import type { DrawerProps } from 'ant-design-vue';

const router = useRouter();
const route = useRoute();
const store = userStore()


const placement = ref<DrawerProps['placement']>('right');
const open = ref<boolean>(false);
const checkLoginStatus = () => {
  // 检查store中的登录状态
  if (store.loginUser.userRole === "notLogin") {
    console.log("未检测到登录状态，跳转到登录页");
    // 使用replace替换当前路由，禁止返回
    router.replace({ path: '/' });
  }
};



// 生命周期钩子：在组件挂载前检查登录状态
onBeforeMount(() => {
  checkLoginStatus();
});
const showDrawer = () => {
  open.value = true;
};

const onClose = () => {
  open.value = false;
};


/**
 * 登出
 */
const logout = async () => {
  try {
    const token = localStorage.getItem('token');

    // 严格遵循接口文档路径大小写
    const res: any = await myAxios.get(
        "/userInfo/logout",
        {
          headers: {
            Authorization:token,
          }
        }
    );

    console.log('登出响应:', res);

    if (res.code === 1) {

      localStorage.removeItem('token');
      sessionStorage.clear();
      store.$reset();


      await router.replace('/');
      message.success('登出成功');
    } else {

      message.error(`登出失败：${res.message || '未知错误'}`);
      console.error('业务逻辑错误:', res);
    }
  } catch (error) {
    console.error('请求异常:', error);
  }
}

</script>

<style scoped>
.manage-header {
  display: flex;
  height: 100%;
  justify-content: space-between;
  background: #fff;
  border-bottom: 2px solid #ccced7;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16);
}

.header-left {
  display: flex;
  margin-left: 20px;
}

.header-left .breadcrumb {
  color: black;
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
}
.routeName {
  font-size: 13px;
  padding-top: 10px;
  font-weight: 400;

}
.header-right {
  display: flex;
  margin-right: 30px;
}

.header-right .bell {
  display: flex;
  align-items: center;
  margin-right: 14px;
}

.header-right .name {
  display: flex;
  align-items: center;
  margin-right: 14px;
}

.header-right .user {
  display: flex;
  align-items: center;
}

.user-avatar {
  cursor: pointer;
  user-select: none;
}

.user-avatar:hover {
  transform: rotate(360deg);
}

.custom-class img{
  float: left;
}
.message {
  float: right;
  margin-right: 100px;
}
.firstmessage {
  margin-top: 0;
}
</style>