<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">
            <div v-for="(li, j) in item" :key="li.mark" class="item-cell">
              <el-input
                v-if="isEnable(isEnableManualModify) && j === 0"
                v-model="li.custom_number"
                class="manual-modify"
              />
              <span
                v-else-if="!isEnable(isEnableManualModify) && j === 0"
                class="question-number"
                title="双击切换序号类型"
                @dblclick="changeOptionType(data)"
              >
                {{ computedQuestionNumber(i, data.option_number_show_mode) }}
              </span>
              <RichText v-model="li.content" placeholder="输入内容" :inline="true" />
              <span v-if="data.property.column_number > j + 1" class="horizontal-line"></span>
              <SvgIcon
                v-if="data.property.column_number === j + 1"
                icon-class="delete"
                class="delete pointer"
                @click="deleteOption(i)"
              />
            </div>
          </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 columnNumberList"
            :key="value"
            v-model="data.property.column_number"
            :label="value"
            @input="changeColumnNumber"
          >
            {{ 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 { changeOptionType } from '@/views/exercise_questions/data/common';
import {
  analysisRecognitionMatchingData,
  columnNumberList,
  getOption,
  getMatchingDataTemplate,
} from '@/views/exercise_questions/data/matching';

export default {
  name: 'MatchingQuestion',
  mixins: [QuestionMixin],
  props: {
    isEnableManualModify: {
      type: String,
      default: 'false',
    },
  },
  data() {
    return {
      changeOptionType,
      columnNumberList,
      data: getMatchingDataTemplate(),
    };
  },
  watch: {
    'data.option_list': {
      handler(val) {
        if (!val) {
          return;
        }
        this.data.answer.answer_list = val.map((item) => {
          return item.map(({ mark }) => mark);
        });
      },
      deep: true,
    },
    isEnableManualModify: {
      handler(val) {
        if (val === 'false') return;
        this.data.option_list = this.data.option_list.map((item, i) => {
          return item.map((li, j) => {
            li.custom_number = `${j === 0 ? i + 1 : ''}`;
            return li;
          });
        });
      },
    },
  },
  methods: {
    /**
     * 智能识别
     * @param {String} text 识别数据
     */
    recognition(text) {
      let arr = this.recognitionCommon(text);
      let obj = analysisRecognitionMatchingData(arr);
      this.recognitionCommonSetObj(obj);
    },
    addOption() {
      let newOption = [];
      for (let i = 0; i < this.data.property.column_number; i++) {
        newOption.push(getOption());
      }
      this.data.option_list.push(newOption);
    },
    /**
     * 修改列数后，修改选项列表
     * @param {Number} val 列数
     */
    changeColumnNumber(val) {
      this.data.option_list = this.data.option_list.map((item) => {
        let len = item.length;
        if (len > val) {
          item.splice(val);
        } else if (len < val) {
          for (let i = 0; i < val - len; i++) {
            item.push(getOption());
          }
        }
        return item;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  &-item {
    .item-cell {
      display: flex;
      flex: 1;
      column-gap: 4px;
      align-items: center;

      .manual-modify {
        width: 55px;

        :deep .el-input__inner {
          height: 36px;
        }
      }

      .rich-wrapper {
        flex: 1;
        min-width: 100px;
        min-height: 32px;
        background-color: $fill-color;

        :deep .rich-text {
          &.mce-content-body {
            padding-top: 4px;
            padding-left: 12px;
          }

          &:not(.mce-edit-focus) {
            p {
              margin: 0;
            }
          }

          &.mce-content-body[data-mce-placeholder]:not(.mce-visualblocks)::before {
            top: 6px;
            left: 12px;
          }
        }
      }
    }
  }
}
</style>
