<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>
      <question-answer
        class="question-answer-body"
        :data="question"
      ></question-answer>
    </div>
  </div>
</template>

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

export default {
  name: "QuestionBasePreview",
  components: { QuestionAnswer },
  props: {
    question: {
      type: Object,
      default() {
        return {};
      },
    },
    questionSerialno: {
      type: [String, Number],
      default: null,
    },
  },
  data() {
    return {
      quesBody: null,
      textQuesAnswer: [],
      quesAnswer: null,
      quesOptions: [],
    };
  },
  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;
      }
    },
  },
};
</script>
