<template>
  <el-dialog
    class="matrix-set-dialog"
    :visible.sync="modalIsShow"
    top="10vh"
    width="260px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form ref="modalFormComp" :model="modalForm" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item prop="rowCount">
            <el-input-number
              v-model="modalForm.rowCount"
              size="small"
              :min="0"
              :max="20"
              :step="1"
              step-strictly
              :controls="false"
              style="width: 100%; text-align: center"
              placeholder="行数"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="columnCount">
            <el-input-number
              v-model="modalForm.columnCount"
              size="small"
              :min="0"
              :max="20"
              :step="1"
              step-strictly
              :controls="false"
              style="width: 100%; text-align: center"
              placeholder="列数"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button size="small" type="primary" @click="submit">确认</el-button>
      <el-button size="small" @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
const initModalForm = {
  columnCount: undefined,
  rowCount: undefined,
};

export default {
  name: "modify-field",
  data() {
    return {
      modalIsShow: false,
      modalForm: {},
      rules: {
        columnCount: [
          {
            required: true,
            message: "请输入",
            trigger: "change",
          },
        ],
        rowCount: [
          {
            required: true,
            message: "请输入",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = { ...initModalForm };
      this.$nextTick(() => {
        this.$refs.modalFormComp.clearValidate();
      });
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      this.$emit("confirm", this.modalForm);
      this.cancel();
    },
  },
};
</script>

<style>
.matrix-set-dialog .el-dialog__header {
  display: none;
}
.matrix-set-dialog .el-dialog__body {
  padding: 20px 20px 10px;
}
</style>
