<!-- 排序题 -->
<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"
            :class="['content-item-' + data.property.layout_type]"
          >
            <span
              v-if="data.property.layout_type === 'horizontal'"
              class="question-number"
              title="双击切换序号类型"
              @dblclick="changeOptionType(data)"
            >
              {{ computedQuestionNumber(i, data.option_number_show_mode) }}
            </span>
            <div class="option-box">
              <div v-for="(items, indexs) in item.data_list" :key="indexs" class="option-content">
                <RichText v-model="items.content" placeholder="输入内容" :inline="true" />
                <i class="el-icon-circle-close" @click="deleteOptionItem(i, indexs)"></i>
              </div>
            </div>
            <SvgIcon
              v-if="data.property.layout_type === 'horizontal'"
              icon-class="add-circle"
              size="16"
              @click="addOptionItem(i)"
            />
            <SvgIcon
              v-if="data.property.layout_type === 'horizontal'"
              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>

      <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-select v-model="data.property.option_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_analysis"
            :label="value"
          >
            {{ label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="排序">
          <el-radio
            v-for="{ value, label } in sortTypeList"
            :key="value"
            v-model="data.property.layout_type"
            :label="value"
            @change="changeType"
          >
            {{ 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>
    </template>
  </QuestionBase>
</template>

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

import {
  analysisRecognitionSortData,
  getOption,
  sortTypeList,
  getSortDataTemplate,
} from '@/views/exercise_questions/data/sort';
import { changeOptionType } from '@/views/exercise_questions/data/common';
import { getRandomNumber } from '@/utils/index';

export default {
  name: 'SortQuestion',
  components: {},
  mixins: [QuestionMixin],
  data() {
    return {
      changeOptionType,
      sortTypeList,
      data: getSortDataTemplate(),
    };
  },
  watch: {
    'data.option_list': {
      handler(val) {
        if (!val) return;
        this.data.answer.answer_list = val?.map((item) => {
          let value_list = item.data_list?.map((items) => {
            return items.mark;
          });
          let answer_item = {
            mark: item.mark,
            value_list,
          };
          return answer_item;
        });
      },
      deep: true,
    },
  },
  methods: {
    addOption() {
      if (this.data.property.layout_type === 'vertical') {
        this.data.option_list[0].data_list.push(getOption());
      } else {
        this.data.option_list.push({
          mark: getRandomNumber(),
          data_list: [getOption(), getOption(), getOption(), getOption(), getOption(), getOption()],
        });
      }
    },
    addOptionItem(i) {
      this.data.option_list[i].data_list.push(getOption());
    },
    /**
     * 智能识别
     * @param {String} text 识别数据
     */
    recognition(text) {
      let arr = this.recognitionCommon(text);
      let obj = analysisRecognitionSortData(arr);
      this.recognitionCommonSetObj(obj);
    },
    changeType(item) {
      if (item === 'vertical') {
        this.data.option_list = this.data.option_list.splice(0, 1);
      }
    },
    // 删除小题里的选项
    deleteOptionItem(i, index) {
      this.data.option_list[i].data_list.splice(index, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
.content-item {
  flex-wrap: wrap;
  gap: 4px 4px;
}

.content-item-horizontal {
  .option-box {
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    gap: 4px 4px;
  }

  .option-content {
    min-width: 110px;
    max-width: 110px;
    word-break: break-word;
  }
}

.question-container .question-content .content > ul .content-item-vertical {
  display: block;

  .option-content {
    width: 100%;
    margin-bottom: 4px;
  }
}
</style>
