<template>
  <div class="topbar">
    <div class="topbar_title">
      <img src="~@/assets/img/logo.png" />
    </div>
    <div class="topbar_user">
      <el-dropdown @command="handleCommand" trigger="hover">
        <div class="avatar-wrapper">
          <div class="user_avatar">
            <el-icon><UserFilled /></el-icon>
          </div>
          <span>{{ userInfo.name }}</span>
          <el-icon><ArrowDown /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item divided command="logout">
              <span>退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
  <topTag />
</template>

<script setup lang="ts">
import topTag from "./topTag.vue";

import { ref } from "vue";
import { useStore } from "vuex";
import { ElMessageBox } from "element-plus";
import { UserFilled, ArrowDown } from "@element-plus/icons-vue";

const store = useStore();
const userInfo: any = ref("");

store.dispatch("GetUserInfo").then((res: any) => {
  userInfo.value = res;
});

const handleCommand = () => {
  ElMessageBox.confirm("即将退出登录，是否确定？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    store.dispatch("LogOut").then(() => {
      // 刷新登录页面，避免多次弹框
      window.location.reload();
    });
  });
};
</script>

<style lang="scss" scoped>
.topbar {
  position: relative;
  height: 90px;
  width: 100%;
  display: flex;
  align-items: center;
  background-image: url(~@/assets/img/top-bj.png);
  background-size: 100% 100%;
  box-sizing: border-box;
  &_title {
    flex: 1;
    img {
      height: 40px;
    }
  }
  &_search {
    .el-input {
      width: 280px;
      margin-right: 30px;
    }
  }
  &_user {
    min-width: 180px;
    .avatar-wrapper {
      color: #000;
      font-weight: bolder;
      font-size: 16px;
      display: flex;
      align-items: center;
      .user_avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        text-align: center;
        line-height: 40px;
        margin-right: 5px;
        .el-icon {
          margin: 0;
          color: #020066;
          font-size: 30px;
        }
      }
      .el-icon {
        margin-left: 10px;
      }
    }
  }
}
</style>
