<template>
  <el-dialog
    class="edit-fill-question 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="topicNo" label="大题题号：">
        <el-input-number
          style="width:125px;"
          v-model.number="modalForm.topicNo"
          :min="1"
          :max="20"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <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="endNumber" 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="modalForm.startNumber"
          :max="100"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="optionCount" label="选项个数：">
        <el-input-number
          style="width:125px;"
          v-model="modalForm.optionCount"
          :min="2"
          :max="22"
          :step="1"
          step-strictly
          :controls="false"
          :disabled="modalForm.isFill"
        ></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-checkbox
          v-model="modalForm.isFill"
          @change="selectTypeChange"
          :disabled="modalForm.isMultiply"
          >判断题</el-checkbox
        >
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="modalForm.isMultiply" :disabled="modalForm.isFill"
          >多选</el-checkbox
        >
      </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: 5,
  questionsCount: 10,
  optionCount: 5,
  isFill: false,
  isMultiply: false
};

export default {
  name: "edit-fill-question",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      }
    },
    topicNos: {
      type: Array,
      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();
      }
    };

    const topicNoValidater = (rule, value, callback) => {
      if (!this.instance.topicNo) {
        // 新增题目
        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: {
        topicName: [
          {
            required: true,
            message: "请输入题目名称",
            trigger: "change"
          }
        ],
        topicNo: [
          {
            required: true,
            message: "请输入大题题号",
            trigger: "change"
          },
          {
            type: "number",
            validator: topicNoValidater,
            trigger: "change"
          }
        ],
        endNumber: [
          {
            required: true,
            message: "请输入起止题号",
            trigger: "change"
          },
          {
            type: "number",
            validator: numberRangeValidater,
            trigger: "change"
          }
        ],
        optionCount: [
          {
            required: true,
            type: "number",
            message: "请输入选项个数",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    initData(val) {
      const valInfo = val.parent || val;
      this.modalForm = { ...valInfo };
      this.modalForm.endNumber =
        this.modalForm.startNumber + this.modalForm.questionsCount - 1;
    },
    selectTypeChange(val) {
      if (val) {
        this.modalForm.optionCount = 2;
        this.modalForm.isMultiply = false;
      }
    },
    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>
