<template>
  <div class="edit-explain">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="100px"
    >
      <!-- <el-form-item prop="topicNo" label="大题序号：">
        <el-input-number
          v-model="modalForm.topicNo"
          :min="1"
          :max="99"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item> -->
      <el-form-item prop="topicName" label="题目名称：">
        <card-name-edit v-model="modalForm"></card-name-edit>
      </el-form-item>
      <el-form-item prop="startEnd" label="起止题号：">
        <el-input-number
          style="width: 40px"
          v-model="modalForm.startNumber"
          :min="0"
          :max="999"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
        <span class="el-input-split"></span>
        <el-input-number
          style="width: 40px"
          v-model="modalForm.endNumber"
          :min="0"
          :max="999"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import CardNameEdit from "../../components/common/CardNameEdit.vue";

const initModalForm = {
  id: "",
  topicNo: null,
  topicName: "",
  nameFontSize: "14px",
  nameFontWeight: 400,
  startNumber: 1,
  endNumber: 4,
  questionsCount: 4,
};

export default {
  name: "edit-explain",
  components: { CardNameEdit },
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    const numberRangeValidater = (rule, value, callback) => {
      if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
        return callback(new Error("请输入起止题号"));
      }
      if (this.modalForm.startNumber > this.modalForm.endNumber) {
        callback(new Error("开始题号不能大于结束题号"));
      } else {
        callback();
      }
    };
    return {
      modalForm: { ...initModalForm },
      rules: {
        topicNo: [
          {
            required: true,
            message: "请输入大题序号",
            trigger: "change",
          },
        ],
        topicName: [
          {
            required: true,
            message: "请输入题目名称",
            trigger: "change",
          },
        ],
        endNumber: [
          {
            required: true,
            message: "请输入起止题号",
            trigger: "change",
          },
          {
            type: "number",
            validator: numberRangeValidater,
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      const valInfo = val.parent || val;
      this.modalForm = { ...valInfo };
      this.modalForm.endNumber =
        this.modalForm.startNumber + this.modalForm.questionsCount - 1;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.modalForm.questionsCount =
        this.modalForm.endNumber - this.modalForm.startNumber + 1;
      this.modalForm.topicName = this.modalForm.topicName.trim();
      this.$emit("modified", this.modalForm);
    },
  },
};
</script>
