<template>
  <el-dialog
    class="edit-fill-line edit-dialog"
    :visible.sync="dialogIsShow"
    title="填空题"
    top="10vh"
    width="640px"
    :modal="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="opened"
    @close="closed"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="100px"
    >
      <el-form-item prop="topicName" label="题目名称：">
        <el-input
          v-model.trim="modalForm.topicName"
          size=""
          placeholder="请输入题目名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="startEnd" label="起止题号：">
        <el-input-number
          style="width:40px;"
          v-model="modalForm.startNumber"
          :min="0"
          :max="100"
          :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="100"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="每行空数：">
        <el-input-number
          style="width:125px;"
          v-model="modalForm.questionNumberPerLine"
          :min="1"
          :max="10"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
        <span class="el-input-tips">*指一行显示填空题数量</span>
      </el-form-item>
      <el-form-item label="每空行数：">
        <el-input-number
          style="width:125px;"
          v-model="modalForm.lineNumberPerQuestion"
          :min="1"
          :max="15"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
        <span class="el-input-tips">*指每一题显示几行</span>
      </el-form-item>
      <el-form-item label="题号前缀：">
        <el-input
          style="width:125px;"
          v-model.trim="modalForm.numberPre"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="submit">确认</el-button>
      <el-button @click="cancel" plain>取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
const initModalForm = {
  id: "",
  topicNo: null,
  topicName: "",
  startNumber: 1,
  endNumber: 2,
  questionsCount: 2,
  questionNumberPerLine: 1,
  lineNumberPerQuestion: 1,
  numberPre: ""
};

export default {
  name: "edit-fill-line",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    },
    topicNos: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    const numberValidater = (rule, value, callback) => {
      if (this.modalForm.startNumber < this.modalForm.endNumber) {
        callback(new Error("开始序号不能小于结束序号"));
      } else {
        callback();
      }
    };

    const topicNoValidater = (rule, value, callback) => {
      if (this.instance.topicNo === 0) {
        // 新增题目
        if (this.topicNos.includes(value)) {
          callback(new Error("当前大题题号已经存在，请重新输入"));
        } else {
          callback();
        }
      } else {
        // 修改题目
        if (value !== this.instance.topicNo && this.topicNos.includes(value)) {
          callback(new Error("当前大题题号已经存在，请重新输入"));
        } else {
          callback();
        }
      }
    };

    return {
      dialogIsShow: false,
      modalForm: { ...initModalForm },
      rules: {
        topicNo: [
          {
            validate: topicNoValidater,
            trigger: "change"
          }
        ],
        startEnd: [
          {
            validate: numberValidater,
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      this.modalForm = { ...val };
      this.modalForm.endNumber =
        this.modalForm.startNumber + this.modalForm.questionsCount - 1;
    },
    opened() {
      this.initData(this.instance);
    },
    closed() {
      this.$emit("closed");
    },
    cancel() {
      this.dialogIsShow = false;
    },
    open() {
      this.dialogIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

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