<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>
          <svg-icon
            class="activeNotice"
            style="margin-bottom: 5px"
            icon-class="add"
            size="38"
            @click="open"
          />
          <svg-icon
            class="activeNotice"
            icon-class="lingdang"
            size="48"
            @click="messageStatus = !messageStatus"
          />
        </div>
        <div class="task">
          <el-progress
            :percentage="processCount"
            :show-text="false"
            :stroke-width="10"
          />
        </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"-->
          <!--            :alt="userStore.user.avatar"-->
          <!--            :src="userStore.user.avatar"-->
          <!--            object-fit="cover"-->
          <!--            style="width: 48px; height: 48px; border-radius: 24px"-->
          <!--            @click="showClick"-->
          <!--          />-->
          <svg-icon icon-class="head" size="48" @click="showClick" />
          <template #dropdown>
            <el-dropdown-menu style="width: 150px">
              <!--              <el-dropdown-item command="a">Action 1</el-dropdown-item>-->
              <el-dropdown-item command="b">退出登录</el-dropdown-item>
              <!--              <el-dropdown-item command="c" divided>Action 3</el-dropdown-item>-->
              <el-dropdown-item command="c"
                >{{ notice ? "关闭" : "打开" }}触摸提示
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-space>
    </div>
    <MessageBox v-model="messageStatus" />
    <AddMsg ref="addMsgRef" />
  </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";
import { emitter, EventsNames } from "@/utils/common";

const userStore = useUserStore();
const addMsgRef = ref(null);
const open = () => {
  addMsgRef.value.open();
};
const router = useRouter();
const route = useRoute();
const routeMeta = computed(() => {
  return route.meta;
});
const notice = ref(true);
const dropdown1 = ref<DropdownInstance>();

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

const processCount = ref(50);
const messageStatus = ref(false);
const addMessageStatus = ref(false);
const headUrl = ref("");

let timer: any = -1;

onMounted(() => {
  //获取触摸提示
  //@ts-ignore
  if (localStorage.getItem("notice") == true) {
    notice.value = true;
  } else {
    notice.value = false;
  }
  //@ts-ignore
  localStorage.setItem("notice", notice.value);
  userStore.user.notice = notice.value;
  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();
      router.replace("/login");
    });
  }
  if (command === "c") {
    notice.value = !notice.value;
    localStorage.setItem("notice", notice.value);
    userStore.user.notice = notice.value;
    ElMessage.success("设置成功！");
  }
};
</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: #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;
  }

  // .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>
