<template>
  <div class="fill-blank-question">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item prop="quesBody" label="题干">
        <v-editor
          v-model="modalForm.quesBody"
          enable-answer-point
          @answer-point-changed="answerPointsChange"
          @change="quesBodyChange"
        ></v-editor>
      </el-form-item>
      <el-form-item label="答案"></el-form-item>
      <el-form-item
        v-for="(answer, oindex) in modalForm.quesAnswer"
        :key="oindex"
        :prop="`quesAnswer.${oindex}.body`"
        :rules="answerRule"
      >
        <div class="question-edit-option">
          <div class="option-check">（{{ oindex + 1 }}）</div>
          <div class="option-body">
            <v-editor v-model="answer.body"></v-editor>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="答案解析">
        <v-editor v-model="modalForm.comment"></v-editor>
      </el-form-item>
    </el-form>
    <question-info-edit
      ref="QuestionInfoEdit"
      :question="modalForm"
      @change="questionInfoChange"
    ></question-info-edit>
  </div>
</template>

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

export default {
  name: "FillBlankQuestion",
  components: { QuestionInfoEdit },
  props: {
    question: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: {},
      rules: {
        quesBody: [
          {
            validator: (rule, value, callback) => {
              if (!value || isAnEmptyRichText(value)) {
                return callback(new Error(`请输入题干`));
              }
              callback();
            },
            trigger: "change",
          },
        ],
      },
      answerRule: {
        validator: (rule, value, callback) => {
          if (!value || !value.length) {
            return callback(new Error(`请输入答案内容`));
          }
          callback();
        },
        trigger: "change",
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      this.modalForm = this.$objAssign(
        getInitQuestionModel("FILL_BLANK_QUESTION"),
        this.question
      );
      this.modalForm.quesAnswer = this.modalForm.quesAnswer.map((item) => {
        return {
          body: item,
        };
      });
    },
    answerPointsChange(answerPointsCount) {
      console.log(answerPointsCount);
      let curAnswerPointsCount = this.modalForm.quesAnswer.length;
      if (curAnswerPointsCount === answerPointsCount) return;

      if (curAnswerPointsCount > answerPointsCount) {
        this.modalForm.quesAnswer = this.modalForm.quesAnswer.slice(
          0,
          answerPointsCount
        );
      } else {
        for (let i = curAnswerPointsCount; i < answerPointsCount; i++) {
          this.modalForm.quesAnswer.push({
            body: null,
          });
        }
      }
    },
    quesBodyChange() {
      this.$refs.modalFormComp.validateField(`quesBody`, () => {});
    },
    questionInfoChange(questionInfo) {
      this.modalForm = Object.assign({}, this.modalForm, questionInfo);
    },
    validate() {
      return this.$refs.modalFormComp.validate();
    },
    getData() {
      let data = { ...this.modalForm };
      data.quesAnswer = this.modalForm.quesAnswer.map((item, index) => {
        return {
          ...item.body,
          index,
        };
      });
      return data;
    },
  },
};
</script>
