<template>
  <div class="topbar">
    <div class="topbar_title">
      <img src="~@/assets/img/top_title.png" />
    </div>
    <div class="topbar_search">
      <el-input placeholder="查询" v-model="topSearch" :suffix-icon="Search" clearable />
    </div>
    <div class="topbar_help">
      <el-icon color="#767DEA"><QuestionFilled /></el-icon>
      帮助
    </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>{{ userId }}</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>
</template>

<script setup lang="ts">
import { nextTick, ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessageBox, ElMessage } from "element-plus";
import { Search, QuestionFilled, UserFilled, ArrowDown } from "@element-plus/icons-vue";

import cache from "@/utils/cache";

const userId = cache.session.get("userId");
const topSearch = ref("");
const router = useRouter();
const handleCommand = () => {
  ElMessageBox.confirm("即将退出登录，是否确定？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      ElMessage({
        type: "success",
        message: "成功退出登录"
      });
      router.push("/login");
    })
    .catch(() => console.log("取消"));
};
</script>

<style lang="scss" scoped>
.topbar {
  width: calc(100vw - 200px);
  height: 90px;
  position: fixed;
  right: 0;
  top: 0;
  display: flex;
  align-items: center;
  background-color: #020066;
  box-sizing: border-box;
  &_title {
    flex: 1;
    img {
      height: 40px;
    }
  }
  &_search {
    .el-input {
      width: 280px;
      margin-right: 30px;
    }
  }
  &_help {
    display: flex;
    align-items: center;
    font-size: 16px;
    margin-right: 30px;
    color: #fff;
    .el-icon {
      font-size: 18px;
    }
  }
  &_user {
    width: 140px;
    .avatar-wrapper {
      color: #fff;
      font-size: 16px;
      display: flex;
      align-items: center;
      .user_avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background-color: #fff;
        text-align: center;
        line-height: 40px;
        margin-right: 5px;
        .el-icon {
          margin: 0;
          color: #020066;
          font-size: 30px;
        }
      }
      .el-icon {
        margin-left: 10px;
      }
    }
  }
}
</style>
