<template>
  <div v-if="store.currentTask" class="mark-board-track-container">
    <div class="tw-my-2 tw-flex tw-place-content-center">
      <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>
      <h1 class="tw-text-3xl tw-text-center">
        总分：{{ store.currentMarkResult?.markerScore || 0 }}
      </h1>
    </div>
    <div class="tw-flex tw-place-content-center tw-mb-2">
      <qm-button type="primary" shape="round" size="large" @click="submit">
        提交
      </qm-button>
    </div>

    <div v-if="store.currentTask && store.currentTask.questionList">
      <template
        v-for="(question, index) in store.currentTask?.questionList"
        :key="index"
      >
        <div class="question tw-rounded tw-p-1 tw-mb-2">
          <div>
            <div>
              {{ question.title }} {{ question.mainNumber }}-{{
                question.subNumber
              }}
            </div>
            <div class="tw-flex tw-flex-wrap tw-gap-1">
              <div
                v-for="(s, i) in Math.round(question.maxScore * 100 + 100) /
                100"
                :key="i"
                @click="chooseScore(question, s - 1)"
                class="single-score"
                :class="isCurrentScore(question, s - 1) && 'current-score'"
              >
                {{ s - 1 }}
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script lang="ts">
import { Question } from "@/types";
import { isNumber } from "lodash";
import { defineComponent, watch } from "vue";
import { store } from "./store";
import { keyMouse } from "./use/keyboardAndMouse";
import { message } from "ant-design-vue";
import { DownOutlined } from "@ant-design/icons-vue";

export default defineComponent({
  name: "MarkBoardMouse",
  emits: ["submit"],
  components: { DownOutlined },
  setup(props, { emit }) {
    const { toggleKeyMouse } = keyMouse();

    function chooseScore(question: Question, score: number) {
      store.currentQuestion = question;
      store.currentQuestion.score = score;
    }
    function isCurrentScore(question: Question, score: number) {
      return question.score === score;
    }

    function submit() {
      const errors: any = [];
      store.currentTask?.questionList.forEach((question, index) => {
        if (!isNumber(question.score)) {
          errors.push({ question, index, error: "没有赋分不能提交" });
        }
      });
      if (errors.length === 0) {
        emit("submit");
      } else {
        console.log(errors);
        message.error({
          content: errors
            .map((e: any) => `${e.index + 1}、${e.error}`)
            .join("\n"),
          duration: 10,
        });
      }
    }

    return {
      store,
      toggleKeyMouse,
      chooseScore,
      isCurrentScore,
      submit,
    };
  },
});
</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);
}
.question {
  min-width: 80px;
  border: 1px solid grey;
}
.single-score {
  width: 29px;
  height: 29px;
  display: grid;
  place-content: center;

  border: 1px solid black;
  border-radius: 5px;
}
.current-score {
  border: 1px solid yellowgreen;
  background-color: lightblue;
}
</style>
