<!-- 听读训练 -->
<template>
  <QuestionBase>
    <template #content>
      <div class="stem">
        <el-input
          v-if="data.property.stem_type === stemTypeList[0].value"
          v-model="data.stem"
          rows="3"
          resize="none"
          type="textarea"
          placeholder="输入题干"
        />

        <RichText v-if="data.property.stem_type === stemTypeList[1].value" v-model="data.stem" placeholder="输入题干" />

        <el-input
          v-show="isEnable(data.property.is_enable_description)"
          v-model="data.description"
          rows="3"
          resize="none"
          type="textarea"
          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>

    <template #property>
      <el-form :model="data.property">
        <el-form-item label="题干">
          <el-radio
            v-for="{ value, label } in stemTypeList"
            :key="value"
            v-model="data.property.stem_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="题号">
          <el-input v-model="data.property.question_number" />
        </el-form-item>
        <el-form-item label-width="45px">
          <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-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 scoreTypeList"
            :key="value"
            v-model="data.property.score_type"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label-width="45px">
          <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"
          />
        </el-form-item>
      </el-form>
    </template>
  </QuestionBase>
</template>

<script>
import QuestionMixin from '../common/QuestionMixin.js';

import { selectTypeList, changeOptionType } from '@/views/exercise_questions/data/common';
import { 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 = text
        .split(/[\r\n]/)
        .map((item) => item.trim())
        .filter((item) => item);

      if (arr.length > 0) {
        this.data.stem = arr[0];
        this.data.option_list = arr.slice(1).map((content) => getOption(content));
      }
    },
    addOption() {
      this.data.option_list.push(getOption());
    },
    // 修改行数
    handleChangeRows(val) {
      if (this.data.option_list.length >= val) {
        this.data.option_list = this.data.option_list.splice(0, val);
      } else {
        let length = val - this.data.option_list.length;
        for (let i = 0; i < length; i++) {
          this.addOption();
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.repeat-option {
  :deep .upload-wrapper {
    margin-top: 0;
  }

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