<template>
  <QuestionBase>
    <template #content>
      <div class="stem">
        <RichText v-model="data.stem" :font-size="18" :wordlimit-num="5000" placeholder="输入题干" />

        <RichText
          v-if="isEnable(data.property.is_enable_description)"
          v-model="data.description"
          placeholder="输入提示"
        />
      </div>

      <div class="content">
        <RichText v-model="data.article" :is-border="true" :height="130" placeholder="输入文章" :is-has-space="true" />
      </div>

      <div v-for="(item, i) in data.question_list" :key="i">
        <QuestionHeader
          :question-type-option="questionTypeOption"
          :is-child="true"
          :type="exerciseTypeList[item.type]"
          @updateCurQuestionType="updateCurQuestionType(i, $event)"
          @deleteQuestion="deleteQuestion(i)"
          @recognition="recognitionChildren(i, $event)"
        />
        <KeepAlive>
          <component
            :is="exerciseComponents[item.type]"
            ref="exercise"
            :is-child="true"
            :is-change.sync="change"
            :question-id="item.id"
            :is-enable-manual-modify="data.property.is_enable_manual_modify"
            @updatePreviewData="updatePreviewData(i, $event)"
            @loaded="loaded"
          />
        </KeepAlive>
      </div>

      <div class="footer">
        <span class="add-option" @click="addQuestion">
          <SvgIcon icon-class="add-circle" size="14" /><span>增加配题</span>
        </span>
      </div>
    </template>

    <template #property>
      <el-form :model="data.property" label-width="72px" label-position="left">
        <el-form-item label="题号">
          <el-input v-model="data.property.question_number" />
        </el-form-item>
        <el-form-item>
          <el-radio
            v-for="{ value, label } in questionNumberTypeList"
            :key="value"
            v-model="data.other.question_number_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="题干题号">
          <el-select v-model="data.property.stem_question_number_font_size">
            <el-option v-for="item in fontSizeList" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="提示">
          <el-radio
            v-for="{ value, label } in switchOption"
            :key="value"
            v-model="data.property.is_enable_description"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="手动修改">
          <el-radio
            v-for="{ value, label } in switchOption"
            :key="value"
            v-model="data.property.is_enable_manual_modify"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
      </el-form>
    </template>
  </QuestionBase>
</template>

<script>
import {
  readData,
  questionTypeOption,
  exerciseTypeList,
  analysisRecognitionReadData,
} from '@/views/exercise_questions/data/read';
import { AddQuestionToExercise, DeleteQuestion } from '@/api/exercise';
import { questionDataList } from '@/views/exercise_questions/data/questionType';

import QuestionMixin from '../common/QuestionMixin.js';
import QuestionHeader from '@/views/exercise_questions/create/components/common/QuestionHeader.vue';
import SelectQuestion from '@/views/exercise_questions/create/components/exercises/SelectQuestion.vue';
import JudgeQuestion from '@/views/exercise_questions/create/components/exercises/JudgeQuestion.vue';
import MatchingQuestion from '@/views/exercise_questions/create/components/exercises/MatchingQuestion.vue';
import FillQuestion from '@/views/exercise_questions/create/components/exercises/FillQuestion.vue';
import EssayQuestion from './EssayQuestion.vue';

export default {
  name: 'ReadQuestion',
  components: {
    QuestionHeader,
  },
  mixins: [QuestionMixin],
  inject: ['exercise_id'],
  data() {
    return {
      change: false, // 是否修改了题目类型
      questionTypeOption,
      exerciseTypeList,
      childPreviewData: [], // 子题目预览数据
      loaded_number: 0, // 已加载的题目数量
      data: JSON.parse(JSON.stringify(readData)),
      exerciseComponents: {
        select: SelectQuestion,
        judge: JudgeQuestion,
        matching: MatchingQuestion,
        fill: FillQuestion,
        essay_question: EssayQuestion,
      },
    };
  },
  methods: {
    // 添加子题目
    addQuestion() {
      AddQuestionToExercise({
        exercise_id: this.exercise_id,
        is_child_question: 'true',
        parent_question_id: this.questionId,
        type: 'select',
        additional_type: 'single',
        content: questionDataList['select'],
      }).then(({ status, question_id }) => {
        if (status !== 1) return;
        this.data.question_list.push({ id: question_id, type: 'select', additional_type: 'single' });
      });
    },

    /**
     * 删除题目
     * @param {Number} i 题目索引
     */
    deleteQuestion(i) {
      DeleteQuestion({ question_id: this.data.question_list[i].id }).then(({ status }) => {
        if (status !== 1) return;
        this.data.question_list.splice(i, 1);
        this.childPreviewData.splice(i, 1);
      });
    },

    /**
     * 智能识别
     * @param {String} text 识别数据
     */
    recognition(text) {
      let arr = this.recognitionCommon(text);
      let obj = analysisRecognitionReadData(arr);
      this.recognitionCommonSetObj(obj);
    },
    /**
     * 智能识别
     * @param {Number} i 题目索引
     * @param {String} text 识别数据
     */
    recognitionChildren(i, text) {
      this.$refs.exercise[i]?.recognition(text);
    },

    /**
     * 更新当前题目类型
     * @param {Number} i 题目索引
     * @param {Array} arr 选中的题目类型
     */
    updateCurQuestionType(i, arr) {
      this.change = true;
      let type = arr[arr.length - 1];
      this.data.question_list[i].type = type;
      this.data.question_list[i].additional_type = type === 'select' ? 'single' : '';
    },

    /**
     * 更新预览数据
     * @param {number} i 题目索引
     * @param {object} data 预览数据
     */
    updatePreviewData(i, data) {
      this.$set(this.childPreviewData, i, data);
    },

    loaded() {
      this.loaded_number += 1;
      if (this.loaded_number === this.data.question_list.length) {
        this.updateLoading(false);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.question-type {
  margin-top: 16px;
}
</style>
