<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>
      <span v-if="voKey=='preInstallBackInfo'" style="color: red;margin-left: 120px;">{{$t('请注意,预装反审后,全部提单需重新制作')}}</span>
    </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 plain type="primary" @click="canclAudit" v-show="isReview">{{$t('取消审核')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
    </el-row>
  </div>
</template>

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

/**
 * 反审
 */
export default {
  name: "review",
  inheritAttrs: false,
  data() {
    return {
      // 反审对象
      reviewObj: {},
      isReview: false,
      bpmProcessId: "",
      voKey: "",
    };
  },
  created() {
    const { currNode, shipmentObj } = this.$attrs;

    let voKey = "";
    switch (currNode.type) {
      case "preinstall":
        voKey = "preInstallBackInfo";
        break;
      case "cabinet":
        voKey = "cabinetBackInfo";
        break;
      case "unloading":
        voKey = "cabinetUnloadBackApprovalInfo";
        break;
    }

    if (voKey) {
      this.isReview = shipmentObj[voKey] ? true : false;
      if (shipmentObj[voKey] && shipmentObj[voKey].approvalStatus !== 1) {
        this.isReview = false;
      }
      if (shipmentObj[voKey]) {
        this.bpmProcessId = shipmentObj[voKey].bpmProcessId;
      }
      if (this.isReview) {
        this.$set(
          this.reviewObj,
          "applyReason",
          shipmentObj[voKey].applyReason
        );
      }
    }
    this.voKey = voKey;
  },
  methods: {
    /* 取消审核 */
    canclAudit() {
      const { shipmentObj } = this.$attrs;

      approvalCancel({
        applyReason: this.$t("取消反审核"),
        id: shipmentObj[this.voKey].id,
        shipmentId: shipmentObj.id,
      }).then((res) => {
        serviceMsg(res, this).then(() => {
          this.$emit("closeDialog", "submit");
        });
      });
    },
    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>