<script lang="ts" setup>
import {onMounted, ref} from "vue";
import {Search,Bell,Sunny} from "@element-plus/icons-vue";
import myAxios from "../../API/myAxios.ts";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import {userStore} from "../../stores/userStore.ts";
const router = useRouter();
const store = userStore();
const logout= async() => {
  const res:any = await myAxios.post("/user/logout", {
  })
  ElMessage({
    showClose: true,
    type: res.code === 0 ? 'success' : "error",
    message: res.code === 0 ? '退出成功' : `退出失败`,
  })
  if (res.code === 0 && res.data) {
    store.$reset();
    await router.replace('/login')
  }
}
const open = ref<boolean>(false);

const afterOpenChange = (bool: boolean) => {
  console.log('open', bool);
};

const showDrawer = () => {
  open.value = true;
};


onMounted(()=>{
  console.log(store.loginUser);
})
</script>


<template>
  <div class="boxLeft">
    <img src="../../images/logo.png" class="boxImg">
  </div>
  <div class="user">
    {{store.loginUser.userRole==='notLogin'?'未登录':store.loginUser.username}}
  </div>
  <div class="boxRight">
    <div class="icon1">
      <el-icon><Search /></el-icon>
    </div>
    <div class="icon2">
      <el-icon><Bell /></el-icon>
    </div>
    <div class="icon3">
      <el-icon><Sunny /></el-icon>
    </div>
    <el-dropdown>
      <a-avatar size="large" class="people">
        <template #icon><img :src="store.loginUser.avatarUrl" alt=""></template>
      </a-avatar>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="logout"><el-icon><Bell /></el-icon>退出登录</el-dropdown-item>
          <el-dropdown-item @click="showDrawer"><el-icon><Bell /></el-icon>个人中心</el-dropdown-item>
        </el-dropdown-menu>
      </template>

    </el-dropdown>

    <a-drawer
        v-model:open="open"
        class="custom-class"
        root-class-name="root-class-name"
        :root-style="{ color: 'blue' }"
        style="color: #646464"
        title="个人中心"
        placement="right"
        @after-open-change="afterOpenChange"
    >
      <el-avatar :size="64" :src="store.loginUser.avatarUrl"></el-avatar>

     <div class="message">
       <p class="firstmessage">用户名：{{ store.loginUser.username }}</p>
       <p>账号ID：{{ store.loginUser.username }}</p>
       <p>注册时间：2024-06-20</p>
       <p>账号权限：管理员</p>
     </div>
    </a-drawer>

  </div>
</template>


<style>
.boxLeft {
  width: 200px;
  height: 200px;
  float: left;
}
.user {
  padding-left: 1070px;
  display: inline-block;
  line-height: 50px;
}
.boxImg {
  width:200px;
}

.boxRight {
  float: right;
  width: 120px;
  height: 200px;
}

.icon1 {
  margin-top: 20px;
  display: inline-block;
}
.icon2 {
  margin-left: 10px;
  display: inline-block;
}
.icon3 {
  margin-left: 10px;
  display: inline-block;
}
.people {
  float: right;
  margin-top: 10px;
  margin-left: 10px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

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