<!-- 选择题 -->
<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="输入描述"
        />

        <UploadAudio
          v-show="isEnable(data.property.is_enable_listening)"
          :file-id="data.file_id_list?.[0]"
          @upload="upload"
          @deleteFile="deleteFile"
        />
      </div>

      <div class="content">
        <ul>
          <li v-for="(item, i) in data.option_list" :key="i" class="content-item">
            <span class="question-number" @dblclick="changeOptionType(data)">
              {{ computedQuestionNumber(i, data.option_number_show_mode) }}.
            </span>
            <div class="option-content">
              <span :class="['checkbox', { active: isAnswer(item.mark) }]" @click="selectAnswer(item.mark)"></span>
              <RichText v-model="item.content" placeholder="输入内容" :inline="true" />
            </div>
            <SvgIcon icon-class="delete" class="delete pointer" @click="deleteOption(i)" />
          </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 selectTypeList"
            :key="value"
            v-model="data.property.select_type"
            :label="value"
            @input="changeSelectType"
          >
            {{ 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_listening"
            :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"
            :disabled="scoreTypeList[1].value === value && data.property.select_type === selectTypeList[0].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, scoreTypeList, changeOptionType } from '@/views/exercise_questions/data/common';
import { getSelectData, getOption } from '@/views/exercise_questions/data/select';

export default {
  name: 'SelectQuestion',
  components: {
    UploadAudio,
  },
  mixins: [QuestionMixin],
  data() {
    return {
      selectTypeList,
      changeOptionType,
      data: getSelectData(),
    };
  },
  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));
      }
    },
    changeSelectType(val) {
      if (val === selectTypeList[0].value && this.data.answer.answer_list.length > 1) {
        this.data.answer.answer_list = [this.data.answer.answer_list[0]];
      }
      // 当多选题切换到单选题时，分值类型切换为总分
      if (val === selectTypeList[0].value && this.data.property.score_type === scoreTypeList[1].value) {
        this.data.property.score_type = scoreTypeList[0].value;
      }
    },
    isAnswer(mark) {
      return this.data.answer.answer_list.indexOf(mark) !== -1;
    },
    selectAnswer(mark) {
      let index = this.data.answer.answer_list.indexOf(mark);
      if (this.data.property.select_type === selectTypeList[0].value) {
        this.data.answer.answer_list = [mark];
      }
      if (this.data.property.select_type === selectTypeList[1].value) {
        if (index === -1) {
          this.data.answer.answer_list.push(mark);
        } else {
          this.data.answer.answer_list.splice(index, 1);
        }
      }
    },
    addOption() {
      this.data.option_list.push(getOption());
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  .option-content {
    .checkbox {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: 16px;
      height: 16px;
      margin-right: 8px;
      cursor: pointer;
      border: 1px solid #333;
      border-radius: 50%;

      &.active {
        &::before {
          display: inline-block;
          width: 6px;
          height: 6px;
          content: '';
          background-color: #333;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>
