<template>
  <div class="boolean-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 prop="quesAnswer" label="答案">
          <el-radio-group v-model="modalForm.quesAnswer" @change="answerChange">
            <el-radio
              v-for="option in answerOptions"
              :key="option.value"
              :label="option.value"
            >
              {{ option.label }}
            </el-radio>
          </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
        ref="QuestionInfoEdit"
        :question="modalForm"
        label-width="72px"
        use-course-property-cache
        @change="questionInfoChange"
      ></question-info-edit>
      <div class="ep-question-props-close" @click="unactiveQuestion">
        <i class="el-icon-error"></i>
      </div>
    </div>
  </div>
</template>

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

export default {
  name: "BooleanQuestion",
  components: { QuestionInfoEdit, QuestionInfoView },
  props: {
    question: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: {},
      answerOptions: [
        {
          value: "false",
          label: "错误",
        },
        {
          value: "true",
          label: "正确",
        },
      ],
    };
  },
  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);
      const validAnswer = ["true", "false"];
      this.modalForm.quesAnswer = validAnswer.includes(
        this.modalForm.quesAnswer
      )
        ? this.modalForm.quesAnswer
        : null;
    },
    answerChange() {
      this.$refs.modalFormComp.validateField(`answer`, () => {});
    },
    questionInfoChange(questionInfo) {
      this.modalForm = Object.assign({}, this.modalForm, questionInfo);
    },
    validate() {
      return this.$refs.modalFormComp.validate();
    },
    getData() {
      let data = Object.assign({}, this.question, this.modalForm);
      return data;
    },
    activeQuestion() {
      this.setCurQuestion(this.question);
    },
    unactiveQuestion() {
      this.setCurQuestion({});
    },
  },
};
</script>
