<template>
  <div class="edit-fill-number">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="100px"
    >
      <el-form-item prop="name" label="名称：">
        <el-input
          v-model="modalForm.name"
          placeholder="请输入名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="numberCount" label="号码数量：">
        <el-input-number
          v-model="modalForm.numberCount"
          :max="20"
          :min="1"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { objAssign } from "../../plugins/utils";

const initModalForm = {
  id: "",
  name: "",
  numberCount: 13,
};

export default {
  name: "edit-fill-number",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    const numberValidater = (rule, value, callback) => {
      if (!this.modalForm.numberCount) {
        return callback(new Error("请输入号码数量"));
      }

      callback();
    };
    return {
      modalForm: { ...initModalForm },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "change" }],
        numberCount: [
          {
            required: true,
            validator: numberValidater,
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      this.modalForm = objAssign(initModalForm, val);
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

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