<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">
              <span v-if="j === 0" class="question-number">
                {{ i + 1 }}
              </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>
    </template>

    <template #property>
      <el-form :model="data.property">
        <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-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-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 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 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>
    </template>
  </QuestionBase>
</template>

<script>
import RichText from '@/components/common/RichText.vue';
import QuestionMixin from '../common/QuestionMixin.js';

import { columnNumberList, getOption, getMatchingDataTemplate } from '@/views/exercise_questions/data/matching';

export default {
  name: 'MatchingQuestion',
  components: { RichText },
  mixins: [QuestionMixin],
  data() {
    return {
      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,
    },
  },
  methods: {
    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;

      .rich-wrapper {
        flex: 1;
        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>
