<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 { store } from "@/features/mark/store";
import MarkHeader from "./MarkHeader.vue";
import { useRoute } from "vue-router";
import MarkBody from "./MarkBody.vue";
import type { MarkStore, Task } from "@/types";
import { message } from "ant-design-vue";
import { getSingleLibraryTask } from "@/api/libraryTrackPage";

const route = useRoute();
let libraryId = route.query.libraryId;

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.task) {
    store.setting.fileServer = res.data.fileServer;
    store.setting.splitConfig = res.data.splitConfig;
    store.setting.groupNumber = res.data.groupNumber;
    store.setting.uiSetting = {
      "answer.paper.scale": 1,
      "score.board.collapse": false,
    } as MarkStore["setting"]["uiSetting"];

    let task = res.data.task as Task;

    task.sliceUrls = task.sliceUrls.map(
      (s: string) => store.setting.fileServer + s
    );

    store.currentTask = task;
    if (store.currentTask)
      store.setting.subject = store.currentTask
        .subject as MarkStore["setting"]["subject"];
  } else {
    store.message = res.data.message;
  }
}

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

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

async function getSingleStuTask() {
  return getSingleLibraryTask(libraryId as string);
}

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

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