<template>
  <div
    v-if="store.currentTask"
    class="mark-board-track is-mouse-board"
    :class="[store.isScoreBoardCollapsed ? 'hide' : 'show']"
  >
    <div class="board-mode">
      <a-dropdown>
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="toggleKeyMouse">
              鼠标给分
            </a-menu-item>
          </a-menu>
        </template>
        <a-button>
          键盘给分
          <DownOutlined style="display: inline-flex" />
        </a-button>
      </a-dropdown>
    </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="store.currentTask.markResult?.markerScore || 0">{{
              store.currentTask.markResult?.markerScore
            }}</span>
          </transition-group>
        </div>
      </div>
      <qm-button
        class="board-header-submit"
        size="medium"
        type="primary"
        @click="submit"
      >
        提交
      </qm-button>
    </div>

    <div
      v-if="store.currentTask && store.currentTask.questionList"
      class="board-questions"
      :style="{
        flexGrow: 2,
      }"
    >
      <template
        v-for="(question, index) in store.currentTask.questionList"
        :key="index"
      >
        <div class="board-question-full-box">
          <div
            :id="'bq-' + question.mainNumber + '-' + question.subNumber"
            :class="['board-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 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>
</template>

<script setup lang="ts">
import type { Question } from "@/types";
import { store } from "@/store/store";
import { keyMouse } from "./use/keyboardAndMouse";
import { DownOutlined } from "@ant-design/icons-vue";

const emit = defineEmits(["submit", "allZeroSubmit", "unselectiveSubmit"]);
const { toggleKeyMouse } = keyMouse();

function chooseScore(question: Question, score: number) {
  store.currentQuestion = question;
  const { __index } = store.currentQuestion;
  store.currentTask &&
    (store.currentTask.markResult.scoreList[__index] = score);
}
function isCurrentScore(question: Question, score: number) {
  const { __index } = question;
  const questionScore =
    store.currentTask && store.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 submit() {
  emit("submit");
}

const buttonHeightForSelective = $computed(() =>
  store.setting.selective && store.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>
