<template>
  <div>
    <el-form ref="reviewForm" :model="reviewObj" label-width="120px">
      <el-form-item label="申请理由">
        <el-input v-model="reviewObj.applyReason" type="textarea" rows="2" placeholder="请输入申请理由" :disabled="isReview"></el-input>
      </el-form-item>
    </el-form>
    <el-row class="operate-button">
      <el-button type="success" @click="onSubmit" :disabled="isReview">发起申请</el-button>
      <el-button type="primary" @click="jumpReviewDetail" :disabled="!isReview">审核中</el-button>
      <el-button @click="cancel">关闭</el-button>
    </el-row>
  </div>
</template>

<script>
import { approvalCreate } from "@/api/ecw/boxSea";
import { serviceMsg, toReviewDetail } from "../utils";

/**
 * 反审
 */
export default {
  name: "review",
  inheritAttrs: false,
  data() {
    return {
      // 反审对象
      reviewObj: {},
      isReview: false,
      bpmProcessId: "",
    };
  },
  created() {
    const { currNode, shipmentObj } = this.$attrs;
    if (currNode.type === "preinstall") {
      this.isReview = shipmentObj["preInstallBackInfo"] ? true : false;
      this.bpmProcessId = shipmentObj["preInstallBackInfo"]?.bpmProcessId;
    }
    if (currNode.type === "unloading") {
      this.isReview = shipmentObj["cabinetUnloadBackApprovalInfo"]
        ? true
        : false;
      this.bpmProcessId =
        shipmentObj["cabinetUnloadBackApprovalInfo"]?.bpmProcessId;
    }
  },
  methods: {
    jumpReviewDetail() {
      toReviewDetail.apply(this, [this.bpmProcessId]);
      this.$emit("closeDialog");
    },
    /** 提交 */
    onSubmit() {
      this.$refs["reviewForm"].validate((valid) => {
        if (valid) {
          const { currNode, shipmentObj } = this.$attrs;
          approvalCreate({
            shipmentId: shipmentObj.id,
            ...this.reviewObj,
            approvalStatus: 0,
            approvalType: currNode.type === "preinstall" ? 4 : 7, // 4预装反审 7卸柜反审核
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
  },
};
</script>