<template>
  <div class="appNav">
    <div class="rowBC">
      <div class="appNav-left">
        <el-avatar class="appNav-avatar" :size="25" :src="imgUrl" @error="errorHandler">
          <img src="../../../assets/home/pc_err.png" />
        </el-avatar>
        <span class="appNav-name">深圳机场货运一个码系统-管理端</span>
      </div>
      <div class="appNav-right">
        <el-avatar class="appNav-avatar" :size="25" :src="imgUrl" @error="errorHandler">
          <img src="../../../assets/home/pc_err.png" />
        </el-avatar>
        <el-dropdown trigger="click" size="default">
          <div class="avatar-wrapper">
            <span class="name">{{name}}</span>
            <ArrowDownBold style="width: 1.2em; height: 1.3em;" />
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <router-link to="/">
                <el-dropdown-item>首页</el-dropdown-item>
              </router-link>
              <el-dropdown-item divided @click="loginOut">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { ArrowDownBold } from "@element-plus/icons-vue";
import { useUserStore } from "@/store/user";
import imgUrl from "../../../assets/home/pic_logo.png";
const store = useUserStore();
const name = store.$state.username;
const errorHandler = () => true;
/*
 * 退出登录
 * */
const router = useRouter();
const route = useRoute();
const loginOut = async () => {
  const userStore = useUserStore();
  const result: any = await userStore.logout();
  if (result.code == 0) {
    ElMessage({
      message: "退出成功",
      type: "success",
      duration: 2 * 1000,
      onClose: () => {
        useUserStore().resetState();
        router.push(`/login`);
        location.reload();
      },
    });
  } else {
    ElMessage.error(result.message);
  }
};
</script>

<style lang="scss" scoped>
.appNav {
  height: 48px;
  line-height: 48px;
  background: #ac014d;
  padding: 0 42px 0 16px;
  &-avatar {
    margin-top: 11.5px;
  }
  &-name {
    font-size: 14px;
    font-weight: 400;
    color: #ffffff;
    margin-left: 13px;
    top: -5px;
    position: relative;
  }
  .avatar-wrapper {
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #ffffff;
    cursor: pointer;
    margin-top: 15px;
    .name {
      margin: 0px 10px;
      position: relative;
      top: -3px;
    }
  }
}
</style>
