<template>
  <div class="match-question">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item v-if="IS_PARAGRAPH_MATCHING" prop="quesBody" label="题干">
        <v-editor
          v-model="modalForm.quesBody"
          @change="quesBodyChange"
        ></v-editor>
      </el-form-item>
      <el-form-item prop="quesAnswer" label="答案">
        <el-radio-group v-model="quesAnswer" @change="answerChange">
          <el-radio-button
            v-for="option in parentQuestion.quesOptions"
            :key="option.number"
            :label="option.number"
          >
            {{ (option.number - 1) | optionOrderWordFilter }}
          </el-radio-button>
        </el-radio-group>
      </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
      v-if="canEditQuestionInfo"
      ref="QuestionInfoEdit"
      :question="modalForm"
      @change="questionInfoChange"
    ></question-info-edit>
  </div>
</template>

<script>
import { isAnEmptyRichText } from "@/utils/utils";
import QuestionInfoEdit from "../QuestionInfoEdit.vue";
import { getMatchQuestionModel } from "./questionModel";

export default {
  name: "MatchQuestion",
  components: { QuestionInfoEdit },
  props: {
    question: {
      type: Object,
      default() {
        return {};
      },
    },
    parentQuestion: {
      type: Object,
      default() {
        return {
          quesOptions: [],
          questionType: "",
        };
      },
    },
    canEditQuestionInfo: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      modalForm: {},
      quesAnswer: null,
      rules: {
        quesBody: [
          {
            validator: (rule, value, callback) => {
              if (!value || isAnEmptyRichText(value)) {
                return callback(new Error(`请输入题干`));
              }
              callback();
            },
            trigger: "change",
          },
        ],
        quesAnswer: [
          {
            validator: (rule, value, callback) => {
              if (!value || !value.length) {
                return callback(new Error(`请设置答案`));
              }
              callback();
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  computed: {
    IS_PARAGRAPH_MATCHING() {
      return this.parentQuestion.questionType === "PARAGRAPH_MATCHING";
    },
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      this.modalForm = this.$objAssign(getMatchQuestionModel(), this.question);
      this.modalForm.courseId = this.parentQuestion.courseId;
      this.modalForm.quesAnswer = this.question.quesAnswer
        ? JSON.parse(this.question.quesAnswer)
        : [];
      this.quesAnswer = this.modalForm.quesAnswer[0] || null;
    },
    quesBodyChange() {
      this.$refs.modalFormComp.validateField(`quesBody`, () => {});
    },
    answerChange() {
      this.modalForm.quesAnswer =
        this.quesAnswer || this.quesAnswer === 0 ? [this.quesAnswer] : [];
      this.$refs.modalFormComp.validateField(`quesAnswer`, () => {});
    },
    optionChange() {
      const optionCount = this.parentQuestion.quesOptions.length;
      if (!this.quesAnswer || this.quesAnswer <= optionCount) return;

      this.quesAnswer = null;
      this.answerChange();
    },
    questionInfoChange(questionInfo) {
      this.modalForm = Object.assign({}, this.modalForm, questionInfo);
    },
    validate() {
      return this.$refs.modalFormComp.validate();
    },
    getData() {
      let data = { ...this.modalForm };
      data.quesAnswer = JSON.stringify(data.quesAnswer);
      return data;
    },
  },
};
</script>
