<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="80px">
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="编码名称" prop="roleName">
              <el-input v-model="form.roleName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="编码值" prop="roleCode">
              <el-input v-model="form.roleCode" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="roleDes">
              <el-input v-model="form.roleDes" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" 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 {
  getRiskRoleInfo,
  updateRiskRole,
  addRiskRole,
} from "@/api/secure/pdm-risk-role";
export default {
  data() {
    return {
      // 遮罩层
      visible: false,
      title: "",
      form: {
        roleName: "",
        roleCode: "",
        roleDes: "",
        remark: "",
      },
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: "编码名称不能为空", trigger: "blur" },
        ],
        roleCode: [
          { required: true, message: "编码值不能为空", trigger: "blur" },
        ],
        roleDes: [{ 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 getRiskRoleInfo(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 = {
          修改: updateRiskRole,
          新增: addRiskRole,
        };
        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 = {
        roleName: "",
        roleCode: "",
        roleDes: "",
        remark: "",
      };
      this.resetForm("form");
    },
  },
};
</script>
