<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>
      <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>Action 2</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";

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) => {
  ElMessage(`click on item ${command}`);
};
</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;

  .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;
  }
}
</style>
