<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 v-if="routeMeta.back && routeMeta.title" class="middle-title">
      {{ routeMeta.title }}
    </div>
    <div v-else>
      <div class="date">{{ date }}</div>
      <div class="time">{{ time }}</div>
    </div>
    <div>
      <el-space>
        <div class="task">
          <el-progress
            :percentage="processCount"
            :show-text="false"
            :stroke-width="10"
          />
          <div class="process">任务进度: {{ processCount }}%</div>
        </div>
        <div>
          <div class="name">name</div>
          <div class="work">gongwei</div>
        </div>

        <el-dropdown
          ref="dropdown1"
          trigger="contextmenu"
          @command="handleCommand"
        >
          <img v-if="headUrl" :src="headUrl" alt="" @click="showClick" />
          <svg-icon v-else icon-class="head" size="48" @click="showClick" />
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="a">Action 1</el-dropdown-item>
              <el-dropdown-item command="b" divided>退出登录</el-dropdown-item>
              <el-dropdown-item command="c" divided>Action 3</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";

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

const dropdown1 = ref<DropdownInstance>();

const date = dayjs().format("YYYY-MM-DD");
const time = ref(dayjs().format("HH:mm:ss"));

const processCount = ref(50);

const headUrl = ref("");

let timer: any = -1;

onMounted(() => {
  timer = setInterval(() => {
    time.value = dayjs().format("HH:mm:ss");
  }, 1000);
});

onBeforeUnmount(() => {
  if (timer) {
    clearInterval(timer);
  }
});

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", "");
      location.reload();
    });
  }
};
</script>

<style lang="scss" scoped>
.commonHeader {
  height: $navbar-height;
  width: 100%;
  background-color: #f1f3f5;
  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;
  }

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

:deep(.el-dropdown__popper .el-dropdown__list) {
  width: 292px !important;
  background: #f1f3f5;
  box-shadow: 0px 0px 29px 4px rgba(0, 0, 0, 0.25);
  border-radius: 30px;
  overflow: hidden;
}

//:deep(.el-dropdown-menu__item) {
//  width: 200px;
//  height: 80px;
//  font-weight: 500;
//  font-size: 24px;
//  color: rgba(0, 0, 0, 0.9);
//  text-align: center;
//}
</style>
