<template>
  <div class="edit-fill-field">
    <el-form
      ref="modalFormComp"
      :key="modalForm.id"
      :model="modalForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item prop="fieldCountPerLine" label="每行名词数：">
        <el-input-number
          v-model="modalForm.fieldCountPerLine"
          style="width: 125px"
          :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 prop="lineSpacing" label="空位上下间距：">
        <el-input-number
          v-model.number="modalForm.lineSpacing"
          style="width: 125px"
          :min="20"
          :max="100"
          :step="1"
          step-strictly
          :controls="false"
        ></el-input-number>
      </el-form-item>
      <el-form-item prop="fields" label="名词：">
        <el-input
          v-model="modalForm.fields"
          placeholder="请输入名词项"
          type="textarea"
          style="width: 100%"
          @change="fieldsChange"
        ></el-input>
        <span class="el-input-tips">*多个名词用逗号隔开，支持中英文逗号</span>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="modalForm.nameIsJustify"
          >名词名是否两端对齐</el-checkbox
        >
      </el-form-item>
    </el-form>
  </div>
</template>

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

const initModalForm = {
  id: "",
  fieldCountPerLine: 1,
  lineSpacing: 30,
  nameIsJustify: false,
  fields: "",
};

export default {
  name: "EditFillField",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      modalForm: { ...initModalForm },
      fieldList: [],
      rules: {
        fields: [
          {
            required: true,
            message: "请输入名词",
            trigger: "change",
          },
        ],
        lineSpacing: [
          {
            required: true,
            type: "number",
            message: "请输入空位上下间距",
            trigger: "change",
          },
        ],
        fieldCountPerLine: [
          {
            required: true,
            type: "number",
            message: "请输入每行名词数",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initData(this.instance);
  },
  methods: {
    initData(val) {
      this.modalForm = { ...val };
    },
    fieldsChange() {
      this.modalForm.fields = this.modalForm.fields.replace(/\\n/g, "");
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

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