<template>
  <div class="question-view">
    <div class="question-body" v-html="question.body"></div>
    <div class="ops">
      <div class="stu-answer"> {{stuAnswer}}</div>
      <div class="score">({{question.questionScore}}分)</div>
    </div>
    <div v-for="(option, index) in question.options" :key="option.id" class="option" @click="toggleAnswer(index)">
      <input type="checkbox" name="question" value="optionName[index]" :checked="stuAnswer && stuAnswer.includes(optionName[index])" />
      <span style="padding: 0 10px;">{{optionName[index]}}: </span>
      <span class="question-options" v-html="option.content"></span>
    </div>
    <div class="reset">
      <i-button type="warning" size="large" @click="resetQuestion(examQuestion.id)">重置答案</i-button>
    </div>
  </div>
</template>

<script>
const optionName = ["A", "B", "C", "D", "E", "F"];
export default {
  name: "MultipleQuestionView",
  data() {
    return {
      optionName,
      stuAnswer: this.examQuestion.stuAnswer || ""
    };
  },
  props: {
    question: Object,
    examQuestion: Object
  },
  created: function() {
    window.addEventListener("keyup", this.keyup);
  },
  destroyed() {
    window.removeEventListener("keyup", this.keyup);
  },
  methods: {
    keyup(e) {
      // console.log(e);
      // console.log(document.activeElement.type);
      if (
        ["BODY", "A", "BUTTON", "DIV"].includes(
          document.activeElement.tagName
        ) ||
        (["INPUT"].includes(document.activeElement.tagName) &&
          ["checkbox", "radio"].includes(document.activeElement.type))
      ) {
        if (
          ["KeyA", "KeyB", "KeyC", "KeyD", "KeyE", "KeyF", "KeyG"]
            .slice(0, this.question.options.length)
            .includes(e.code)
        ) {
          if (this.stuAnswer.includes(e.code[3])) {
            this.stuAnswer = this.stuAnswer.replace(e.code[3], "");
          } else {
            this.stuAnswer = this.stuAnswer
              .concat(e.code[3])
              .split("")
              .sort()
              .join("");
          }
        }
      }
    },
    toggleAnswer: function(index) {
      if (this.stuAnswer.includes(optionName[index])) {
        this.stuAnswer = this.stuAnswer.replace(optionName[index], "");
      } else {
        this.stuAnswer = this.stuAnswer
          .concat(optionName[index])
          .split("")
          .sort()
          .join("");
      }
      this.answerQuestion();
    },
    answerQuestion: function() {
      this.$http.put("/api/exam_question/" + this.examQuestion.id, {
        stuAnswer: this.stuAnswer
      });
    },
    resetQuestion: function(examQuestionId) {
      this.stuAnswer = "";
      this.$http.put("/api/exam_question/" + examQuestionId, {
        stuAnswer: null
      });
    }
  },
  watch: {
    examQuestion: function() {
      this.stuAnswer = this.examQuestion.stuAnswer;
    }
  }
};
</script>

<style scoped>
.question-view {
  display: grid;
  grid-row-gap: 10px;
}

.question-body {
  font-size: 18px;
  margin-bottom: 10px;
}

.ops {
  display: flex;
  align-items: flex-end;
}

.stu-answer {
  width: 100px;
  border-bottom: 1px solid black;
  text-align: center;
  height: 20px;
}

.option {
  display: flex;
}
.question-options {
  text-align: left;
}
</style>
