<template>
  <div class="edit-fill-line">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="120px"
    >
      <el-form-item prop="endNumber" label="起止题号：">
        <el-input-number
          style="width: 40px"
          v-model="modalForm.startNumber"
          :min="0"
          :max="999"
          :step="1"
          step-strictly
          :controls="false"
          @change="lineTypeChange"
        ></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"
          @change="lineTypeChange"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="lineSpacing" label="空位上下间距：">
        <el-input-number
          style="width: 125px"
          v-model.number="modalForm.lineSpacing"
          :min="20"
          :max="100"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="questionNumberPerLine" 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
          style="width: 125px"
          v-model.trim="modalForm.numberPre"
          :maxlength="6"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="空位排列方向：" required>
        <el-radio-group v-model="modalForm.questionDirection" size="small">
          <el-radio-button
            v-for="(val, key) in DIRECTION_TYPE"
            :key="key"
            :label="key"
            >{{ val }}</el-radio-button
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item label="小题空数类型：">
        <el-radio-group
          v-model="modalForm.questionLineType"
          size="small"
          @change="lineTypeChange"
        >
          <el-radio-button label="norm">标准</el-radio-button>
          <el-radio-button label="custom">自定义</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        v-if="modalForm.questionLineType === 'norm'"
        prop="lineNumberPerQuestion"
        label="每题空数："
      >
        <el-input-number
          style="width: 125px"
          v-model="modalForm.lineNumberPerQuestion"
          :min="1"
          :max="15"
          :step="1"
          step-strictly
          :controls="false"
          @change="lineTypeChange"
        ></el-input-number>
        <span class="el-input-tips">*指每一小题的空位数量</span>
      </el-form-item>
      <el-form-item v-else prop="questionLineNums" label="各小题空数：">
        <table class="table table-white table-narrow">
          <tr>
            <th>题号</th>
            <th>空数</th>
          </tr>
          <tr v-for="option in modalForm.questionLineNums" :key="option.no">
            <td>{{ option.no }}</td>
            <td>
              <el-input-number
                v-model="option.count"
                size="mini"
                :min="1"
                :max="50"
                :step="1"
                step-strictly
                :controls="false"
                style="width: 125px"
              ></el-input-number>
            </td>
          </tr>
        </table>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { DIRECTION_TYPE } from "../../../../enumerate";
import { deepCopy } from "../../../../plugins/utils";
import { getModel } from "./model";

const initModalForm = {
  id: "",
  topicName: "",
  startNumber: 1,
  endNumber: 2,
  questionsCount: 2,
  questionNumberPerLine: 1,
  lineNumberPerQuestion: 1,
  lineSpacing: 40,
  questionDirection: "horizontal",
  questionLineType: "norm",
  questionLineNums: [],
  numberPre: "",
};

export default {
  name: "edit-fill-line",
  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();
      }
    };
    const questionLineNumsValidater = (rule, value, callback) => {
      if (value.some((item) => !item.count)) {
        return callback(new Error("请输入每题空数"));
      }

      callback();
    };

    return {
      modalForm: { ...initModalForm },
      DIRECTION_TYPE,
      rules: {
        endNumber: [
          {
            required: true,
            message: "请输入起止题号",
            trigger: "change",
          },
          {
            type: "number",
            validator: numberRangeValidater,
            trigger: "change",
          },
        ],
        lineSpacing: [
          {
            required: true,
            type: "number",
            message: "请输入空位上下间距",
            trigger: "change",
          },
        ],
        questionNumberPerLine: [
          {
            required: true,
            type: "number",
            message: "请输入每行空数",
            trigger: "change",
          },
        ],
        lineNumberPerQuestion: [
          {
            required: true,
            type: "number",
            message: "请输入每题空数",
            trigger: "change",
          },
        ],
        questionLineNums: [
          {
            required: true,
            validator: questionLineNumsValidater,
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      this.modalForm = deepCopy(val);
      this.modalForm.endNumber =
        this.modalForm.startNumber + this.modalForm.questionsCount - 1;
    },
    lineTypeChange() {
      if (
        !this.modalForm.startNumber ||
        !this.modalForm.endNumber ||
        this.modalForm.startNumber > this.modalForm.endNumber
      )
        return;

      this.modalForm.questionsCount =
        this.modalForm.endNumber - this.modalForm.startNumber + 1;
      this.modalForm = getModel(this.modalForm);
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.$emit("modified", deepCopy(this.modalForm));
    },
  },
};
</script>
