<template>
  <div class="question-preview">
    <div v-if="quesBody" class="question-body">
      <rich-text :text-json="quesBody"></rich-text>
    </div>
    <div v-if="quesOptions && quesOptions.length" class="question-options">
      <div
        v-for="option in quesOptions"
        :key="option.number"
        class="question-option"
      >
        <rich-text :text-json="option.optionBody"></rich-text>
      </div>
    </div>
    <div class="question-answer">
      <h4 class="question-answer-title">答案</h4>
      <div class="question-answer-body">
        <div v-if="IS_SELECTION">
          {{ quesAnswer | selectionAnswerFilter }}
        </div>
        <div v-else-if="IS_BOOL_ANSWER_QUESTION">
          {{ quesAnswer ? "对" : "错" }}
        </div>
        <div v-else-if="IS_FILL_BLANK_QUESTION || IS_TEXT_ANSWER_QUESTION">
          <rich-text
            v-for="(cont, cindex) in textQuesAnswer"
            :key="cindex"
            :text-json="cont"
          ></rich-text>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { deepCopy } from "@/plugins/utils";

export default {
  name: "QuestionBasePreview",
  filters: {
    selectionAnswerFilter(val) {
      return val.map((value) => String.fromCharCode(64 + value)).join("");
    },
  },
  props: {
    question: {
      type: Object,
      default() {
        return {};
      },
    },
    questionSerialno: {
      type: [String, Number],
      default: null,
    },
  },
  data() {
    return {
      quesBody: null,
      textQuesAnswer: [],
      quesAnswer: null,
      quesOptions: [],
    };
  },
  computed: {
    IS_FILL_BLANK_QUESTION() {
      return this.question.questionType === "FILL_BLANK_QUESTION";
    },
    IS_SELECTION() {
      return (
        this.question.questionType === "SINGLE_ANSWER_QUESTION" ||
        this.question.questionType === "MULTIPLE_ANSWER_QUESTION"
      );
    },
    IS_TEXT_ANSWER_QUESTION() {
      return this.question.questionType === "TEXT_ANSWER_QUESTION";
    },
    IS_BOOL_ANSWER_QUESTION() {
      return this.question.questionType === "BOOL_ANSWER_QUESTION";
    },
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      if (this.questionSerialno) {
        if (this.question.quesBody) {
          this.quesBody = deepCopy(this.question.quesBody);
          this.quesBody.sections[0].blocks.unshift({
            type: "text",
            value: this.questionSerialno + "、",
          });
        } else {
          this.quesBody = {
            sections: [
              {
                blocks: [
                  {
                    type: "text",
                    value: this.questionSerialno + "、",
                  },
                ],
              },
            ],
          };
        }
      }

      if (this.question.quesOptions && this.question.quesOptions.length) {
        const quesOptions = deepCopy(this.question.quesOptions);
        quesOptions.forEach((option) => {
          option.optionBody.sections[0].blocks.unshift({
            type: "text",
            value: String.fromCharCode(64 + option.number) + "、",
          });
        });
        this.quesOptions = quesOptions;
      }

      if (!this.question.quesAnswer) return;
      this.quesAnswer = JSON.parse(this.question.quesAnswer);

      if (this.IS_FILL_BLANK_QUESTION) {
        this.textQuesAnswer = this.quesAnswer.map((item) => {
          let nitem = deepCopy(item);
          nitem.sections[0].blocks.unshift({
            type: "text",
            value: `（${item.index + 1}）`,
          });
          return nitem;
        });
      } else if (this.IS_TEXT_ANSWER_QUESTION) {
        this.textQuesAnswer = this.quesAnswer;
      }
    },
  },
};
</script>
