<!-- 听读训练 -->
<template>
  <QuestionBase>
    <template #content>
      <div class="stem">
        <RichText v-model="data.stem" :font-size="18" placeholder="输入题干" />

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

      <div class="content">
        <ul>
          <li v-for="(item, i) in data.option_list" :key="i" class="content-item repeat-option">
            <div v-for="(items, indexs) in item" :key="indexs" class="option-content">
              <el-input v-model="items.content" placeholder="输入内容" @blur="items.content = items.content.trim()" />
            </div>
          </li>
        </ul>
      </div>
      <template v-if="isEnable(data.property.is_enable_reference_answer)">
        <span class="reference-title">参考答案：</span>
        <RichText v-model="data.reference_answer" :font-size="14" placeholder="输入参考答案" />
      </template>

      <div v-if="isEnable(data.property.is_enable_analysis)" class="analysis">
        <div class="analysis-title">解析：</div>
        <RichText v-model="data.analysis" :is-border="true" :font-size="14" placeholder="输入解析" />
      </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_reference_answer"
            :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_analysis"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>

        <el-form-item label="分值">
          <el-radio
            v-for="{ value, label } in scoreTypeList"
            :key="value"
            v-model="data.property.score_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item>
          <el-input-number
            v-model="data.property.score"
            :min="0"
            :step="data.property.score_type === scoreTypeList[0].value ? 1 : 0.1"
          />
        </el-form-item>
        <el-form-item label="行数">
          <el-input-number
            v-model="data.property.row_count"
            :min="1"
            :step="1"
            class="word-num-input"
            :precision="0"
            @change="handleChangeRows('row')"
          />
        </el-form-item>
        <el-form-item label="列数">
          <el-input-number
            v-model="data.property.column_count"
            :min="1"
            :step="1"
            class="word-num-input"
            :precision="0"
            @change="handleChangeRows('column')"
          />
        </el-form-item>
      </el-form>
    </template>
  </QuestionBase>
</template>

<script>
import QuestionMixin from '../common/QuestionMixin.js';
import { getRandomNumber } from '@/utils/index';

import { selectTypeList, changeOptionType } from '@/views/exercise_questions/data/common';
import {
  analysisRecognitionReplaceAnswerData,
  replaceAnswerData,
  getOption,
} from '@/views/exercise_questions/data/replaceAnswer';

export default {
  name: 'ReplaceAnswertQuestion',
  components: {},
  mixins: [QuestionMixin],
  data() {
    return {
      selectTypeList,
      changeOptionType,
      data: JSON.parse(JSON.stringify(replaceAnswerData)),
    };
  },
  methods: {
    /**
     * 智能识别
     * @param {String} text 识别数据
     */
    recognition(text) {
      let arr = this.recognitionCommon(text);
      let obj = analysisRecognitionReplaceAnswerData(arr);
      this.recognitionCommonSetObj(obj);
    },
    addOption() {
      this.data.option_list.push(getOption());
    },
    // 修改行数
    handleChangeRows(type) {
      let row_count = this.data.property.row_count;
      let column_count = this.data.property.column_count;
      if (type === 'row') {
        if (this.data.option_list.length >= row_count) {
          this.data.option_list = this.data.option_list.splice(0, row_count);
        } else {
          let length = row_count - this.data.option_list.length;
          for (let i = 0; i < length; i++) {
            let table_item = [];
            for (let j = 0; j < column_count; j++) {
              table_item.push({ content: '', mark: getRandomNumber() });
            }
            this.data.option_list.push(table_item);
          }
        }
      } else if (this.data.option_list[0].length >= column_count) {
        this.data.option_list.forEach((item, index) => {
          this.data.option_list[index] = item.splice(0, column_count);
        });
      } else {
        let length = column_count - this.data.option_list[0].length;
        for (let i = 0; i < row_count; i++) {
          for (let j = 0; j < length; j++) {
            this.data.option_list[i].push({ content: '', mark: getRandomNumber() });
          }
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  margin-bottom: 8px;
}

.reference-title {
  display: block;
  margin-top: 8px;
  font-size: 14px;
  color: #000;
}

.repeat-option {
  :deep .upload-wrapper {
    margin-top: 0;
  }

  :deep .file-name {
    width: 205px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
