<template>
  <div class="approval-form">
    <!-- 审批表单 -->
    <el-form
      size="mini"
      :model="dialogForm"
      prop="remark"
      :rules="rules"
      ref="form"
    >
      <el-form-item label="审批意见：">
        <el-radio-group
          v-model="checkStatus"
          size="mini"
          style="margin-bottom: 20px"
        >
          <el-radio :label="1" border>同意</el-radio>
          <el-radio :label="-2" border>驳回</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="驳回意见：" prop="remark" v-if="checkStatus == -2">
        <el-input
          type="textarea"
          placeholder="驳回意见"
          v-model="dialogForm.remark"
          size="mini"
          :rows="4"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="mini"
          style="margin: 0 10px 0; width: calc((100% - 20px) / 2)"
          type="primary"
          icon="el-icon-check"
          @click.native="handleCheck"
          >确定</el-button
        >
        <el-button
          size="mini"
          style="margin: 0; width: calc((100% - 20px) / 2)"
          type="primary"
          icon="el-icon-close"
          @click.native="handleCancel"
          plain
          >取消</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { putFlowCheck } from "@/api/approval";
export default {
  props: {
    // 流程id
    flowId: {
      type: String,
      default: "",
    },
  },
  inject: ["handleClosePreviewDialog", "loadData"],
  data() {
    return {
      checkStatus: 1, //1同意 -2驳回
      dialogForm: {
        remark: "", //驳回意见
      },
      rules: {
        remark: [
          {
            required: true,
            message: "请输入驳回意见",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleCheck() {
      const { checkStatus } = this;
      if (checkStatus == -2) {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            const { remark } = this.dialogForm;
            this.submit(checkStatus, remark);
          }
        });
      } else {
        this.submit(checkStatus);
      }
    },

    async submit(checkStatus, remark) {
      const params = {
        checkStatus,
        remark,
        id: this.flowId,
      };
      if (!remark) {
        params.remark = "";
      }
      let res = await putFlowCheck(params);
      if (res.code == 0) {
        this.$message.success("操作成功！");
        // 关闭弹窗
        this.handleClosePreviewDialog();
        // 重新加载审批列表
        this.loadData();
        // 重置数据
        this.$refs.form.resetFields();
      }
    },
  },
};
</script>
