<template>
  <div class="my-container">
    <mark-header />
    <div class="tw-flex tw-gap-1">
      <mark-body @error="renderError" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import { isScanImage, store } from "../store";
import MarkHeader from "./MarkHeader.vue";
import { useRoute } from "vue-router";
import MarkBody from "../inspect/MarkBody.vue";
import type { Task } from "@/types";
import { message } from "ant-design-vue";
import { getSingleStudentTask } from "@/api/studentTrackPage";
import { getInspectedSetting } from "@/api/inspectPage";
import { getPaper } from "@/api/jsonMark";

const route = useRoute();
let studentId = route.query.studentId;
let subjectCode = route.query.subjectCode as string;

async function updateSetting() {
  const settingRes = await getInspectedSetting(subjectCode);
  store.setting.examType = settingRes.data.examType;
  store.setting.fileServer = settingRes.data.fileServer;
  store.setting.subject = settingRes.data.subject;
  store.setting.userName = settingRes.data.userName;
  store.setting.uiSetting = {
    "answer.paper.scale": 1,
    "score.board.collapse": false,
  };
  store.setting.splitConfig = settingRes.data.splitConfig;
  if (store.setting.subject?.paperUrl) {
    store.setting.subject.paperUrl =
      store.setting.fileServer + store.setting.subject?.paperUrl;

    if (!isScanImage()) {
      await getPaper(store);
    }
  }
}

async function updateTask() {
  // const mkey = "fetch_task_key";
  message.info({ content: "获取任务中...", duration: 2 });
  let res = await getSingleStuTask();
  // message.success({ content: "获取成功", key: mkey });

  if (res.data.sliceUrls) {
    store.setting.fileServer = res.data.fileServer;
    store.setting.uiSetting = {
      "answer.paper.scale": 1,
      "score.board.collapse": false,
    };

    let task = {} as Task;
    task.examNumber = res.data.examNumber;

    task.sliceUrls = res.data.sliceUrls.map(
      (s: string) => store.setting.fileServer + s
    );
    // 目前api并没有区分score和tag
    task.questionList = [
      // @ts-ignore
      // { trackList: res.data.tagList.filter((q) => !q.tagName) },
    ];
    // @ts-ignore
    task.specialTagList = res.data.tagList.filter((q) => q.tagName);

    store.currentTask = task;
  } else {
    store.message = res.data.message;
  }
}

async function fetchTask() {
  await updateTask();
}

onMounted(async () => {
  await updateSetting();
  await fetchTask();
});

async function getSingleStuTask() {
  return getSingleStudentTask(studentId as string);
}

const renderError = () => {
  store.currentTask = undefined;
  store.message = "加载失败，请重新加载。";
};
</script>

<style scoped>
.my-container {
  width: 100%;
}
</style>
