<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    @close="cancelForm"
    width="500px"
    append-to-body
  >
    <div class="demo-drawer__content" style="padding: 0 20px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="最大时户数" prop="maxNum">
              <el-input
                type="number"
                v-model="form.maxNum"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="最小时户数" prop="minNum">
              <el-input
                type="number"
                v-model="form.minNum"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="demo-drawer__footer" style="text-align: right">
        <el-button @click="cancelForm">取 消 </el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">{{
          loading ? "提交中 ..." : "确 定"
        }}</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {
  addExamineRoleTarget,
  updateExamineRoleTarget,
  examineRoleTarget,
} from "@/api/powerdistribution/maintenance";
export default {
  data() {
    return {
      // 遮罩层
      visible: false,
      title: "",
      form: {
        maxNum: "",
        minNum: "",
      },
      // 表单校验
      rules: {
        maxNum: [
          { required: true, message: "最大时户数不能为空", trigger: "blur" },
        ],
        minNum: [
          { required: true, message: "最小时户数不能为空", trigger: "blur" },
        ],
      },
      loading: false,
    };
  },
  methods: {
    // 显示弹框
    async openModel(data, title) {
      this.title = title;
      if (data && data.id) {
        this.title = "编辑";
        const { data: _data } = await examineRoleTarget(data.id);
        this.form = { ..._data };
      } else {
        this.title = "新增";
      }
      this.visible = true;
    },
    handleSubmit() {
      if (this.loading) {
        return;
      }
      this.$refs.form.validate((v) => {
        if (!v) return;
        this.loading = true;
        const funMap = {
          编辑: updateExamineRoleTarget,
          新增: addExamineRoleTarget,
        };
        funMap[this.title](this.form).then((response) => {
          this.$modal.msgSuccess("操作成功");
          this.loading = false;
          this.visible = false;
          this.resetForm("form");
          this.$emit("refresh");
        });
      });
    },
    cancelForm() {
      this.loading = false;
      this.visible = false;
      this.form = {
        maxNum: "",
        minNum: "",
      };
      this.resetForm("form");
    },
  },
};
</script>
./UpdateModel.vue
