<template>
  <div
    v-if="store.currentTask"
    :style="{ display: store.MarkBoardTrackCollapse ? 'none' : 'block' }"
    class="mark-board-track-container"
  >
    <div>
      <h1 class="tw-text-3xl tw-text-center">试卷总分：{{ markerScore }}</h1>
    </div>

    <div v-if="groups">
      <template v-for="(groupNumber, index) in groups" :key="index">
        <div class="tw-mb-4">
          <div
            class="
              tw-flex tw-justify-between tw-place-items-center
              hover:tw-bg-gray-200
            "
            style="border-bottom: 1px solid grey"
            @mouseover="addFocusTrack(groupNumber, undefined, undefined)"
            @mouseleave="removeFocusTrack"
          >
            分组 {{ groupNumber }}
            <input
              class="tw-my-auto"
              title="打回"
              type="checkbox"
              @click="groupClicked(groupNumber)"
              :checked="groupChecked(groupNumber)"
            />
          </div>
          <div v-if="questions">
            <template v-for="(question, index) in questions" :key="index">
              <div
                v-if="question.groupNumber === groupNumber"
                class="
                  question
                  tw-flex tw-place-items-center tw-mb-1 tw-ml-2
                  hover:tw-bg-gray-200
                "
                @mouseover="
                  addFocusTrack(
                    undefined,
                    question.mainNumber,
                    question.subNumber
                  )
                "
                @mouseleave="removeFocusTrack"
              >
                <span class="tw-flex-1">
                  {{ question.title }} {{ question.mainNumber }}-{{
                    question.subNumber
                  }}
                </span>
                <span class="tw-flex-1 tw-text-center">
                  {{ question.score || 0 }}
                </span>
                <input
                  title="打回"
                  type="checkbox"
                  @change="questionCheckChanged(question)"
                  :checked="questionChecked(question)"
                />
              </div>
            </template>
          </div>
        </div>
      </template>
    </div>

    <div class="tw-flex tw-justify-center">
      <qm-button
        type="primary"
        v-if="
          store.currentTask.inspectTime && store.currentTask.inspectTime > 0
        "
        @click="reject"
      >
        打回
      </qm-button>
      <qm-button
        v-else-if="checkedQuestions.length === 0"
        @click="inspect"
        type="primary"
      >
        复核
      </qm-button>
      <qm-button v-else @click="reject" type="primary">打回</qm-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Question } from "@/types";
import { message } from "ant-design-vue";
import { computed, reactive, watch } from "vue";
import { store } from "../store";

const emit = defineEmits(["inspect", "reject"]);
let checkedQuestions = reactive([] as Array<Question>);

watch(
  () => store.currentTask,
  () => {
    checkedQuestions.splice(0);
  }
);
const groups = computed(() => {
  const gs = store.currentTask?.questionList.map((q) => q.groupNumber);
  return [...new Set(gs)].sort((a, b) => a - b);
});

const questions = computed(() => {
  const qs = store.currentTask?.questionList;
  return qs;
});

const markerScore = computed(
  () =>
    (questions.value
      ?.map((q) => Math.round((q.score || 0) * 100))
      .reduce((acc, s) => acc + s) || 0) / 100
);

function addToCheckedQuestion(question: Question) {
  checkedQuestions.push(question);
}
function removeCheckedQuestion(question: Question) {
  const idx = checkedQuestions.indexOf(question);
  checkedQuestions.splice(idx, 1);
}
function groupChecked(groupNumber: number) {
  return (
    checkedQuestions.filter((q) => q.groupNumber === groupNumber).length ===
    questions.value?.filter((q) => q.groupNumber === groupNumber).length
  );
}

function questionChecked(question: Question) {
  return checkedQuestions.includes(question);
}

function questionCheckChanged(question: Question) {
  const checked = questionChecked(question);
  if (checked) {
    removeCheckedQuestion(question);
  } else {
    addToCheckedQuestion(question);
  }
}

function groupClicked(groupNumber: number) {
  if (groupChecked(groupNumber)) {
    checkedQuestions
      .filter((q) => q.groupNumber === groupNumber)
      .forEach((q) => {
        const idx = checkedQuestions.indexOf(q);
        checkedQuestions.splice(idx, 1);
      });
  } else {
    questions.value
      ?.filter((q) => q.groupNumber === groupNumber)
      .forEach((q) => {
        if (!questionChecked(q)) checkedQuestions.push(q);
      });
  }
}

function addFocusTrack(
  groupNumber: number | undefined,
  mainNumber: number | undefined,
  subNumber: string | undefined
) {
  store.focusTracks.splice(0);

  if (groupNumber) {
    questions.value
      ?.filter((q) => q.groupNumber === groupNumber)
      ?.map((q) => q.trackList)
      .reduce((acc, ts) => acc.concat(ts))
      .forEach((t) => {
        store.focusTracks.push(t);
      });
  } else {
    questions.value
      ?.map((q) => q.trackList)
      .reduce((acc, ts) => acc.concat(ts))
      .filter((t) => {
        if (mainNumber) {
          return t.mainNumber === mainNumber && t.subNumber === subNumber;
        } else {
          return false;
        }
      })
      .forEach((t) => {
        store.focusTracks.push(t);
      });
  }
  // console.log(store.focusTracks);
}

function removeFocusTrack() {
  store.focusTracks.splice(0);
}

function reject() {
  if (checkedQuestions.length === 0) {
    message.warn({ content: "请先选择试题。" });
    return;
  }
  emit("reject", checkedQuestions);
}

function inspect() {
  emit("inspect");
}
</script>

<style scoped>
.mark-board-track-container {
  max-width: 250px;
  min-width: 250px;
  border-left: 1px solid grey;
  padding-left: 6px;
  padding-right: 6px;
  max-height: calc(100vh - 41px);
  overflow: auto;
}
.question {
  min-width: 100px;
  border-bottom: 1px dotted grey;
}
</style>
