<template>
  <div>
    <el-form ref="reviewForm" :model="reviewObj" label-width="120px">
      <el-form-item :label="$t('申请理由')">
        <el-input v-model="reviewObj.applyReason" type="textarea" rows="2" :placeholder="$t('请输入申请理由')" :disabled="isReview"></el-input>
      </el-form-item>
    </el-form>
    <el-row class="operate-button">
      <el-button type="success" @click="onSubmit" v-show="!isReview">{{$t('发起申请')}}</el-button>
      <el-button type="primary" @click="jumpReviewDetail" v-show="isReview">{{$t('审核中')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</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;
    const {
      preInstallBackInfo,
      cabinetBackInfo,
      cabinetUnloadBackApprovalInfo,
    } = shipmentObj;
    if (currNode.type === "preinstall") {
      this.isReview = preInstallBackInfo ? true : false;
      if (preInstallBackInfo && preInstallBackInfo.approvalStatus !== 1) {
        this.isReview = false;
      }
      this.bpmProcessId = preInstallBackInfo?.bpmProcessId;
    }
    if (currNode.type === "cabinet") {
      this.isReview = cabinetBackInfo ? true : false;
      if (cabinetBackInfo && cabinetBackInfo.approvalStatus !== 1) {
        this.isReview = false;
      }
      this.bpmProcessId = cabinetBackInfo?.bpmProcessId;
    }
    if (currNode.type === "unloading") {
      this.isReview = cabinetUnloadBackApprovalInfo ? true : false;
      if (
        cabinetUnloadBackApprovalInfo &&
        cabinetUnloadBackApprovalInfo.approvalStatus !== 1
      ) {
        this.isReview = false;
      }
      this.bpmProcessId = 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;
          let approvalType = 4; // 预装反审
          if (currNode.type === "cabinet") approvalType = 9; // 装柜反审
          if (currNode.type === "unloading") approvalType = 7; // 卸柜反审核

          approvalCreate({
            shipmentId: shipmentObj.id,
            ...this.reviewObj,
            approvalStatus: 0,
            approvalType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
  },
};
</script>
