<template>
  <div
    class="
      tw-flex tw-gap-4 tw-justify-between tw-items-center
      header-container
      tw-px-1
    "
    v-if="store.setting"
  >
    <div>
      {{
        `${store.setting.subject.code ?? ""}-${
          store.setting.subject.name ?? ""
        }`
      }}
    </div>
    <div class="tw-flex tw-gap-1">
      <div>
        编号<span class="highlight-text">{{
          store.currentTask?.secretNumber
        }}</span>
      </div>
    </div>
    <ul v-if="!isSingleStudent" class="tw-flex tw-gap-2 tw-mb-0">
      <li>
        待复核<span class="highlight-text">{{ store.status.totalCount }}</span>
      </li>
    </ul>
    <ul class="tw-flex tw-gap-2 tw-mb-0">
      <li @click="upScale" title="放大">
        <ZoomInOutlined
          class="icon-font icon-font-size-20 tw-cursor-pointer"
          :style="{
            color: greaterThanOneScale ? 'red' : 'white',
          }"
        />
      </li>
      <li @click="downScale" title="缩小">
        <ZoomOutOutlined
          class="icon-font icon-font-size-20 tw-cursor-pointer"
          :style="{
            color: lessThanOneScale ? 'red' : 'white',
          }"
        />
      </li>
      <li @click="normalScale" title="适应">
        <FullscreenOutlined
          class="icon-font icon-font-size-20 tw-cursor-pointer"
        />
      </li>
    </ul>
    <div @click="toggleHistory" v-if="!isSingleStudent" title="回看">
      <SnippetsOutlined
        class="icon-font icon-font-size-20 tw-cursor-pointer"
        :style="{
          color: store.historyOpen ? 'red' : 'white',
        }"
      />
    </div>
    <div class="tw-flex tw-place-items-center">
      <UserOutlined class="icon-font icon-with-text" />{{
        store.setting.userName
      }}
    </div>
    <div
      class="tw-flex tw-place-items-center tw-cursor-pointer"
      @click="closeWindow"
    >
      <PoweroffOutlined class="icon-font icon-with-text" />关闭
    </div>
  </div>
</template>

<script setup lang="ts">
import { clearInspectedTask } from "@/api/inspectPage";
import { computed, onMounted, ref } from "vue";
import { store } from "../store";
import {
  ZoomInOutlined,
  ZoomOutOutlined,
  FullscreenOutlined,
  SnippetsOutlined,
  UserOutlined,
  PoweroffOutlined,
} from "@ant-design/icons-vue";
import { useRoute } from "vue-router";

const route = useRoute();
let isSingleStudent = ref(false);
isSingleStudent.value = !!route.query.studentId;
const { studentId, subjectCode } = route.query as {
  studentId: string;
  subjectCode: string;
};

const upScale = () => {
  const s = store.setting.uiSetting["answer.paper.scale"];
  if (s < 3)
    store.setting.uiSetting["answer.paper.scale"] = +(s + 0.2).toFixed(1);
};
const downScale = () => {
  const s = store.setting.uiSetting["answer.paper.scale"];
  if (s > 0.2)
    store.setting.uiSetting["answer.paper.scale"] = +(s - 0.2).toFixed(1);
};
const normalScale = () => {
  store.setting.uiSetting["answer.paper.scale"] = 1;
};
const toggleHistory = () => {
  store.historyOpen = !store.historyOpen;
};
const greaterThanOneScale = computed(() => {
  return store.setting.uiSetting["answer.paper.scale"] > 1;
});
const lessThanOneScale = computed(() => {
  return store.setting.uiSetting["answer.paper.scale"] < 1;
});

async function updateClearTask() {
  await clearInspectedTask(studentId, subjectCode);
}

const closeWindow = async () => {
  await updateClearTask();
  window.close();
};

onMounted(() => {
  // 不确定是否一定能在关闭页面时调用
  window.addEventListener("beforeunload", () => {
    updateClearTask();
  });
});
</script>

<style scoped>
.header-container {
  position: relative;
  font-size: 16px;
  height: 40px;

  background-color: #5d6d7d;
  color: white;
}
.highlight-text {
  color: #ffe400;
}
.icon-font {
  display: block;
}
.icon-font-size-20 {
  font-size: 20px;
}
.icon-with-text {
  font-size: 18px;
  line-height: 18px;
}
</style>
