<template>
  <div
    v-if="markStore.currentTask"
    class="mark-board-track is-mouse-board"
    :class="[
      markStore.isTrackMode && markStore.isScoreBoardCollapsed
        ? 'hide'
        : 'show',
    ]"
  >
    <div class="board-mode">
      <a-select
        v-model:value="normalMode"
        style="width: 120px"
        @change="normalModeChange"
      >
        <a-select-option value="keyboard">键盘给分</a-select-option>
        <a-select-option value="mouse">鼠标给分</a-select-option>
      </a-select>
    </div>
    <div class="board-header">
      <div class="board-header-left">
        <div class="board-header-info">
          <img src="@/assets/icons/icon-star.svg" /><span>总分</span>
        </div>
        <div class="board-header-score">
          <transition-group name="score-number-animation" tag="span">
            <span :key="markStore.currentTask.markResult?.markerScore || 0">{{
              markStore.currentTask.markResult?.markerScore
            }}</span>
          </transition-group>
        </div>
      </div>
      <qm-button
        v-if="props.isCheckAnswer && !hasModifyScore"
        class="board-header-submit"
        size="medium"
        type="primary"
        @click="checkSubmit"
      >
        复核
      </qm-button>
      <qm-button
        v-else
        class="board-header-submit"
        size="medium"
        type="primary"
        @click="submit"
      >
        提交
      </qm-button>
    </div>

    <div
      v-if="markStore.currentTask && markStore.currentTask.questionList"
      class="board-questions"
      :style="{
        flexGrow: 2,
      }"
    >
      <template
        v-for="(question, index) in markStore.currentTask.questionList"
        :key="index"
      >
        <div class="board-question-full-box">
          <div
            :id="'bq-' + question.mainNumber + '-' + question.subNumber"
            :class="[
              'board-question',
              {
                'is-current': isCurrentQuestion(question),
                'is-disabled': isDisabledQuestion(question),
              },
            ]"
            @click="chooseQuestion(question)"
          >
            <div class="question-info">
              <div class="question-title" :title="question.title">
                {{ question.title }}
              </div>
              <div class="question-no">
                {{ question.mainNumber }}-{{ question.subNumber }}
              </div>
            </div>
            <div v-if="question.problem" class="question-score is-problem">
              问题卷
            </div>
            <div
              v-else-if="isArbitrated(question)"
              class="question-score is-problem"
            >
              {{ getArbitratedStatusName(question) }}
            </div>
            <div v-else-if="!question.selfMark" class="question-score">
              已评
            </div>
            <div v-else class="board-scores">
              <div
                v-for="(s, i) in questionScoreSteps(question)"
                :key="i"
                :class="[
                  'board-score',
                  { 'is-current': isCurrentScore(question, s) },
                ]"
                @click="chooseScore(question, s)"
              >
                {{ s }}
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>

    <div
      v-if="
        markStore.currentQuestion?.problem &&
        markStore.currentQuestion?.selfMark
      "
      class="board-footer"
    >
      <!-- 非回评下自己标记的问题卷才能取消 -->
      <qm-button class="board-clear" @click="cancelProblem">
        取消问题卷
      </qm-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { Question } from "@/types";
import { useMarkStore } from "@/store";
import useAutoChooseFirstQuestion from "../composables/useAutoChooseFirstQuestion";
import useQuestion from "../composables/useQuestion";
import useMakeTrack from "../composables/useMakeTrack";

const markStore = useMarkStore();
const { isDisabledQuestion, isArbitrated, getArbitratedStatusName } =
  useQuestion();
const { chooseQuestion } = useAutoChooseFirstQuestion();
const { makeCommonTrack } = useMakeTrack();

const emit = defineEmits([
  "submit",
  "allZeroSubmit",
  "unselectiveSubmit",
  "checkSubmit",
]);
const props = defineProps<{ isCheckAnswer?: boolean }>();

const hasModifyScore = ref(false);

// 切换给分模式
const normalMode = ref(markStore.setting.uiSetting["normal.mode"] || "mouse");
function normalModeChange(value: string) {
  markStore.setting.uiSetting["normal.mode"] = value;
}
function isCurrentQuestion(question: Question) {
  return (
    markStore.currentQuestion?.mainNumber === question.mainNumber &&
    markStore.currentQuestion?.subNumber === question.subNumber
  );
}
function chooseScore(question: Question, score: number) {
  if (isDisabledQuestion(question)) return;
  // 只要修改了分值，就当做已修改
  hasModifyScore.value = true;

  makeCommonTrack(question, score);
}
function isCurrentScore(question: Question, score: number) {
  const { __index } = question;
  const questionScore =
    markStore.currentTask &&
    markStore.currentTask.markResult &&
    markStore.currentTask.markResult.scoreList[__index];
  return questionScore === score;
}

function questionScoreSteps(question: Question) {
  if (!question) return [];

  const remainScore = Math.round(question.maxScore * 100) / 100;
  const steps = [];
  for (
    let i = 0;
    i <= remainScore;
    i = Math.round(i * 100 + question.intervalScore * 100) / 100
  ) {
    steps.push(i);
  }
  if (
    Math.round(remainScore * 100) % Math.round(question.intervalScore * 100) !==
    0
  ) {
    steps.push(remainScore);
  }

  return steps;
}

function cancelProblem() {
  if (!markStore.currentQuestion) return;

  markStore.currentQuestion.problem = false;
  markStore.currentQuestion.problemType = null;
  markStore.currentQuestion.problemRemark = "";
}

function submit() {
  emit("submit");
}

function checkSubmit() {
  emit("checkSubmit");
}

const buttonHeightForSelective = $computed(() =>
  markStore.setting.selective && markStore.setting.enableAllZero
    ? "36px"
    : "76px"
);
</script>

<style scoped>
.mark-board-track-container {
  max-width: 290px;
  min-width: 290px;
  padding: 20px;
  max-height: calc(100vh - 56px - 0px);
  overflow: auto;
  z-index: 1001;
  transition: margin-right 0.5s;
  color: var(--app-small-header-text-color);
}
.mark-board-track-container.show {
  margin-right: 0;
}
.mark-board-track-container.hide {
  margin-right: -290px;
}

.top-container {
  background-color: var(--app-container-bg-color);
}
.total-score {
  color: var(--app-main-text-color);
  font-size: 32px;
}
.question {
  min-width: 80px;
}
.single-score {
  position: relative;
  width: 32px;
  height: 32px;
  font-size: var(--app-secondary-font-size);
  display: grid;
  place-content: center;
  background-color: var(--app-container-bg-color);

  border-radius: 30px;
}
.current-score {
  background-color: var(--app-score-color);
  color: white;
}
.all-zero-unselective-button {
  height: v-bind(buttonHeightForSelective);
  border-radius: 10px;
  padding: 7px;
  background-color: #4db9ff;
  border: none;
}
</style>
