<template>
  <div
    v-if="store.setting.uiSetting['score.board.collapse']"
    @mouseover="handleMouseOverWithBoard"
  >
    <div
      class="tw-self-start tw-cursor-pointer tw-pb-10"
      @mouseover.stop="() => true"
    >
      <a-switch
        v-model:checked="store.setting.uiSetting['score.board.collapse']"
      />
    </div>
  </div>
  <div
    v-if="store.currentTask"
    class="mark-board-track-container"
    :class="store.setting.uiSetting['score.board.collapse'] && 'hide-board'"
    @mouseover="handleMouseOverWithBoard"
    @mouseout="handleMouseOutWithBoard"
  >
    <div class="tw-my-2 tw-flex">
      <div
        class="tw-self-start tw-cursor-pointer hover:tw-bg-gray-200 tw-mr-20"
      >
        <a-switch
          v-model:checked="store.setting.uiSetting['score.board.collapse']"
        />
      </div>
      <a-dropdown class="tw-self-end">
        <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 }}
      </h1>
    </div>
    <div class="tw-flex tw-place-content-center tw-mb-2">
      <a-popconfirm
        v-if="store.setting.enableAllZero"
        title="确定给全零分？"
        ok-text="确定"
        cancel-text="取消"
        @confirm="$emit('allZeroSubmit')"
      >
        <a-button
          type="primary"
          shape="round"
          size="large"
          style="margin-right: 20px !important"
        >
          全零分
        </a-button>
      </a-popconfirm>

      <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 questionScoreSteps(question)"
                :key="i"
                @click="chooseScore(question, s)"
                class="single-score"
                :class="isCurrentScore(question, s) && 'current-score'"
              >
                {{ s }}
              </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", "allZeroSubmit"],
  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 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;
    }

    const collapseSideBar = () => {
      store.setting.uiSetting["score.board.collapse"] =
        !!!store.setting.uiSetting["score.board.collapse"];
    };

    const handleMouseOverWithBoard = () => {
      if (store.setting.uiSetting["score.board.collapse"]) {
        const container = document.querySelector(".mark-board-track-container");
        if (container) {
          container.classList.add("show-board");
        }
      }
    };

    const handleMouseOutWithBoard = () => {
      if (store.setting.uiSetting["score.board.collapse"]) {
        const container = document.querySelector(".mark-board-track-container");
        if (container) {
          container.classList.remove("show-board");
        }
      }
    };

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

    return {
      store,
      collapseSideBar,
      handleMouseOverWithBoard,
      handleMouseOutWithBoard,
      toggleKeyMouse,
      questionScoreSteps,
      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);
  overflow: scroll;
  z-index: 1001;
}
.hide-board {
  display: none;
}
.show-board {
  position: absolute;
  background-color: white;
  right: 0px;
  display: block;
}
.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>
