<template>
  <div class="text-answer-question">
    <div
      :class="['ep-question-content', { 'is-active': isActive }]"
      @click="activeQuestion"
    >
      <div class="ep-question-title">
        <span class="ep-question-title-number">{{ question.number }}.</span>
        <rich-text :text-json="question.body"></rich-text>
      </div>
      <question-info-view
        v-if="!isActive"
        :question="getData()"
      ></question-info-view>
    </div>
    <div v-if="isActive" class="ep-question-props">
      <el-form ref="modalFormComp" :model="modalForm" label-width="72px">
        <el-form-item label="答案">
          <v-editor
            v-model="quesAnswer"
            :enable-audio="false"
            @change="answerChange"
          ></v-editor>
        </el-form-item>
        <el-form-item label="答案解析">
          <v-editor
            v-model="modalForm.answerAnalysis"
            :enable-audio="false"
          ></v-editor>
        </el-form-item>
      </el-form>
      <question-info-edit
        ref="QuestionInfoEdit"
        :question="modalForm"
        label-width="72px"
        use-course-property-cache
        @change="questionInfoChange"
      ></question-info-edit>
    </div>
  </div>
</template>

<script>
import { getInitQuestionModel } from "./model";
import QuestionInfoEdit from "../QuestionInfoEdit.vue";
import QuestionInfoView from "./QuestionInfoView.vue";
import { mapState, mapMutations } from "vuex";
import { objTypeOf } from "@/plugins/utils";
import { isAnEmptyRichText } from "@/utils/utils";

export default {
  name: "TextAnswerQuestion",
  components: { QuestionInfoEdit, QuestionInfoView },
  props: {
    question: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: {},
      quesAnswer: null,
    };
  },
  computed: {
    ...mapState("import-edit", ["curQuestion"]),
    isActive() {
      return this.curQuestion.id === this.question.id;
    },
  },
  created() {
    this.initData();
  },
  methods: {
    ...mapMutations("import-edit", ["setCurQuestion"]),
    initData() {
      this.modalForm = this.$objAssign(getInitQuestionModel(), this.question);
      this.initAnswer(this.question.quesAnswer);
      this.quesAnswer = this.modalForm.quesAnswer[0] || null;
    },
    initAnswer(answer) {
      let quesAnswer = [];
      if (answer) {
        try {
          quesAnswer = JSON.parse(answer);
        } catch (err) {
          console.log(`answer error：${answer}`);
        }
      }
      if (this.checkAnswerFormatValid(quesAnswer)) {
        this.modalForm.quesAnswer = quesAnswer.map((item) =>
          isAnEmptyRichText(item) ? null : item
        );
      } else {
        this.modalForm.quesAnswer = [];
      }
    },
    checkAnswerFormatValid(answer) {
      return objTypeOf(answer) !== "array";
    },
    answerChange() {
      this.modalForm.quesAnswer = [
        {
          index: 1,
          ...this.quesAnswer,
        },
      ];
    },
    questionInfoChange(questionInfo) {
      this.modalForm = Object.assign({}, this.modalForm, questionInfo);
    },
    validate() {
      return Promise.resolve(true);
    },
    getData() {
      let data = Object.assign({}, this.question, this.modalForm);
      data.quesAnswer = JSON.stringify(data.quesAnswer);
      return data;
    },
    activeQuestion() {
      this.setCurQuestion(this.question);
    },
  },
};
</script>
