<!-- 听读训练 -->
<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">
        <label class="title-little">内容</label>
        <ul>
          <li v-for="(item, i) in data.option_list" :key="i" class="content-item repeat-option">
            <span class="question-number" @dblclick="changeOptionType(data)">
              {{ computedQuestionNumber(i, data.option_number_show_mode) }}.
            </span>
            <div class="option-content">
              <RichText v-model="item.content" placeholder="输入内容" :inline="true" />
            </div>
            <UploadAudio
              :key="item.audio_file_id || i"
              :file-id="item.audio_file_id"
              :item-index="i"
              @upload="uploads"
              @deleteFile="deleteFiles"
            />
            <SvgIcon icon-class="delete" class="delete pointer" @click="deleteOption(i, item.audio_file_id)" />
          </li>
        </ul>
      </div>

      <div class="footer">
        <span class="add-option" @click="addOption">
          <SvgIcon icon-class="add-circle" size="14" /> <span>增加选项</span>
        </span>
      </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 v-model="data.property.score" type="number" />
        </el-form-item>
      </el-form>
    </template>
  </QuestionBase>
</template>

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

import { selectTypeList, changeOptionType } from '@/views/exercise_questions/data/common';
import { repeatData, getOption } from '@/views/exercise_questions/data/repeat';

export default {
  name: 'RepeatQuestion',
  components: {
    UploadAudio,
  },
  mixins: [QuestionMixin],
  data() {
    return {
      selectTypeList,
      changeOptionType,
      data: JSON.parse(JSON.stringify(repeatData)),
    };
  },
  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());
    },
    uploads(file_id, index) {
      this.data.option_list[index].audio_file_id = file_id;
      this.data.file_id_list.push(file_id);
    },
    deleteFiles(file_id, itemIndex) {
      this.data.option_list[itemIndex].audio_file_id = '';
      this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
    },
    // 删除小题
    deleteOption(i, file_id) {
      this.data.option_list.splice(i, 1);
      this.data.file_id_list.splice(this.data.file_id_list.indexOf(file_id), 1);
    },
  },
};
</script>

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

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