<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="110px">
        <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-col :span="24">
            <el-form-item label="党政一把手" prop="manageAmt">
              <el-input
                type="number"
                v-model="form.manageAmt"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="分管副经理" prop="manageAssistantAmt">
              <el-input
                type="number"
                v-model="form.manageAssistantAmt"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="部室主任" prop="deptDirectorAmt">
              <el-input
                type="number"
                v-model="form.deptDirectorAmt"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="部室分管主任" prop="deptDirectorAssistantAmt">
              <el-input
                type="number"
                v-model="form.deptDirectorAssistantAmt"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="部室责任专责" prop="deptManageAmt">
              <el-input
                type="number"
                v-model="form.deptManageAmt"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="总负责人" prop="principAmt">
              <el-input
                type="number"
                v-model="form.principAmt"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="工作负责人" prop="workPrincipAmt">
              <el-input
                type="number"
                v-model="form.workPrincipAmt"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备主人" prop="ownerAmt">
              <el-input
                type="number"
                v-model="form.ownerAmt"
                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 {
  addExamineRoleDetail,
  updateExamineRoleDetail,
  dxamineRoleDetail,
} from "@/api/powerdistribution/maintenance";
export default {
  data() {
    return {
      // 遮罩层
      visible: false,
      title: "",
      form: {
        maxNum: "",
        minNum: "",
        targetId: "",
        manageAmt: "",
        manageAssistantAmt: "",
        deptDirectorAmt: "",
        deptDirectorAssistantAmt: "",
        deptManageAmt: "",
        principAmt: "",
        workPrincipAmt: "",
        ownerAmt: "",
      },
      // 表单校验
      rules: {
        maxNum: [
          { required: true, message: "最大时户数不能为空", trigger: "blur" },
        ],
        minNum: [
          { required: true, message: "最小时户数不能为空", trigger: "blur" },
        ],
        manageAmt: [
          { required: true, message: "党政一把手不能为空", trigger: "blur" },
        ],
        manageAssistantAmt: [
          { required: true, message: "分管副经理不能为空", trigger: "blur" },
        ],
        deptDirectorAmt: [
          { required: true, message: "部室主任不能为空", trigger: "blur" },
        ],
        deptDirectorAssistantAmt: [
          { required: true, message: "部室分管主任不能为空", trigger: "blur" },
        ],
        deptManageAmt: [
          { required: true, message: "部室责任专责不能为空", trigger: "blur" },
        ],
        principAmt: [
          { required: true, message: "总负责人不能为空", trigger: "blur" },
        ],
        workPrincipAmt: [
          { required: true, message: "工作负责人不能为空", trigger: "blur" },
        ],
        ownerAmt: [
          { 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 dxamineRoleDetail(data.id);
        this.form = { ..._data };
      } else {
        this.form.targetId = data.targetId;
        this.title = "新增";
      }
      this.visible = true;
    },
    handleSubmit() {
      if (this.loading) {
        return;
      }
      this.$refs.form.validate((v) => {
        if (!v) return;
        this.loading = true;
        const funMap = {
          编辑: updateExamineRoleDetail,
          新增: addExamineRoleDetail,
        };
        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: "",
        targetId: "",
        manageAmt: "",
        manageAssistantAmt: "",
        deptDirectorAmt: "",
        deptDirectorAssistantAmt: "",
        deptManageAmt: "",
        principAmt: "",
        workPrincipAmt: "",
        ownerAmt: "",
      };
      this.resetForm("form");
    },
  },
};
</script>
