<template>
  <div class="commonHeader">
    <div style="width: 155px">
      <svg-icon
        v-if="routeMeta.back"
        icon-class="back"
        size="48"
        @click="commonBack"
      />
      <!-- <svg-icon v-else icon-class="LOGO" style="height: 48px; width: 155px" /> -->
    </div>
    <div class="middle-title">
      {{ routeMeta.title }}
    </div>
    <div v-if="routeMeta.back"></div>
    <div v-else>
      <el-space>
        <div>
          <svg-icon
            class="activeNotice"
            icon-class="lingdang"
            size="48"
            @click="messageStatus = !messageStatus"
          />
        </div>
        <div class="task"></div>
        <div>
          <div class="name">{{ userStore.user.username }}</div>
          <div class="work">{{ userStore.user.station }}</div>
        </div>
        <el-dropdown
          ref="dropdown1"
          trigger="contextmenu"
          @command="handleCommand"
        >
          <img
            v-if="userStore.user.avatar"
            :src="baseUrl + userStore.user.avatar"
            fit="cover"
            style="
              width: 48px;
              height: 48px;
              border-radius: 24px;
              border: 1px solid #ccc;
            "
            @click="showClick"
          />
          <svg-icon
            v-else
            icon-class="head"
            size="48"
            style="
              width: 48px;
              height: 48px;
              border-radius: 24px;
              border: 1px solid #ccc;
            "
            @click="showClick"
          />
          <template #dropdown>
            <el-dropdown-menu style="width: 150px">
              <el-dropdown-item command="change">修改密码</el-dropdown-item>
              <el-dropdown-item command="b">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-space>
    </div>
  </div>
</template>

<script lang="ts" setup>
// import dayjs from "dayjs";
import type { DropdownInstance } from "element-plus";
import { logoutApi } from "@/api/auth";
import { useUserStore } from "@/store";

const userStore = useUserStore();

const baseUrl = import.meta.env.VITE_APP_UPLOAD_URL;

const router = useRouter();
const route = useRoute();
const routeMeta = computed(() => {
  return route.meta;
});

const dropdown1 = ref<DropdownInstance>();

const processCount = ref(50);
const messageStatus = ref(false);

const showClick = () => {
  if (!dropdown1.value) return;
  dropdown1.value.handleOpen();
};

const commonBack = (itemValue) => {
  router.back();
};

const handleCommand = (command: string | number | object) => {
  if (command === "b") {
    logoutApi().then(() => {
      localStorage.setItem("token", "");

      router.replace("/login");
      userStore.$reset();
    });
  }
  if (command === "change") {
    router.push({
      name: "changePassword",
      query: {
        redirect: route.fullPath,
        id: userStore.user.userId,
      },
    });
  }
};
</script>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item) {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
  font-size: 24px;
  color: rgba(0, 0, 0, 0.9);
}

.commonHeader {
  height: $navbar-height;
  width: 100%;
  background-color: #409eff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  //border: 1px solid red;

  .middle-title {
    font-weight: 500;
    font-size: 38px;
    color: rgba(0, 0, 0, 0.9);
    line-height: 24px;
    text-align: center;
  }

  .date {
    font-weight: 500;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.6);
    line-height: 14px;
    text-align: center;
  }

  .time {
    font-weight: 500;
    font-size: 20px;
    color: rgba(0, 0, 0, 0.9);
    line-height: 23px;
  }

  .head {
    width: 48px;
    height: 48px;
    border-radius: 24px;
  }

  .name {
    font-weight: 500;
    font-size: 20px;
    color: rgba(0, 0, 0, 0.9);
    line-height: 14px;
    text-align: right;
  }

  .work {
    font-weight: 500;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.6);
    line-height: 14px;
    text-align: right;
    margin-top: 5px;
  }

  .task {
    padding-top: 5px;
    margin-right: 10px;
  }

  // .activeNotice {
  //   animation: swing 0.15s infinite alternate ease-in-out;
  // }

  // @keyframes swing {
  //   0% {
  //     transform: rotate(-45deg);
  //   }

  //   100% {
  //     transform: rotate(45deg);
  //   }
  // }

  .process {
    font-weight: 500;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.6);
    line-height: 14px;
    text-align: right;
    margin-top: 8px;
  }
}
</style>
