<template>
  <div class="edit-fill-field">
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="120px"
    >
      <el-form-item prop="fieldCountPerLine" label="每行变量数：">
        <el-input-number
          style="width: 125px"
          v-model="modalForm.fieldCountPerLine"
          :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
          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="fields" label="变量：">
        <el-select
          v-model="modalForm.fields"
          placeholder="请选择"
          multiple
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="item in fieldList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="modalForm.nameIsJustify"
          >变量名是否两端对齐</el-checkbox
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { deepCopy, objAssign } from "../../plugins/utils";
import { mapState } from "vuex";

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

export default {
  name: "edit-fill-field",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    ...mapState("free", ["cardConfig"]),
  },
  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.fieldList = [
        ...this.cardConfig.requiredFields,
        ...this.cardConfig.extendFields,
      ].filter((item) => item.enable);

      this.modalForm = deepCopy(val);
      this.modalForm.fields = val.fields.map((item) => item.code);
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      const data = deepCopy(this.modalForm);
      data.fields = this.fieldList.filter((item) =>
        this.modalForm.fields.includes(item.code)
      );
      const model = objAssign(this.instance, data);

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