<template>
  <div style="display: inline-block">
    <el-button type="success" @click="handleOpen(3)">通过</el-button>
    <el-button type="danger" @click="handleOpen(4)">驳回</el-button>
    <el-dialog ref="formDialog" append-to-body width="550px" :visible.sync="visible" :close-on-click-modal="false"
      @close="handleClose">
      <el-form :model="auditParams" ref="form" label-width="100px">
        <Input type="textarea" prop="remark" label="审批意见" :required="auditParams.status === 4" width="350px" :rows="4"
          :maxlength="100" />
      </el-form>
      <div style="clear: both;" slot="footer">
        <el-button :loading="loading" v-if="auditParams.status === 3" type="success" @click="handleAudit">通过</el-button>
        <el-button :loading="loading" v-else type="danger" @click="handleAudit">驳回</el-button>
        <el-button class="back button" @click="handleClose">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { audit } from "@/api/audit/flow";

export default {
  name: "AuditDialog",
  props: {
    businessId: {
      type: String,
      required: true,
    },
    auditFlow: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      visible: false,
      loading: false,
      auditParams: {},
    };
  },
  methods: {
    handleAudit() {
      this.$refs["form"].validate().then((valid) => {
        if (valid) {
          this.loading = true;
          const { id, auditFlow } = this.$route.query;
          audit({
            businessId: Number(id),
            auditFlow: Number(auditFlow),
            ...this.auditParams
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success("操作成功");
              this.handleClose();
            }
          })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    handleOpen(status) {
      this.auditParams.status = status
      this.visible = true;
    },
    handleClose() {
      this.auditParams = {}
      this.visible = false;
    },
  },
};
</script>
