<template>
  <div>
    <el-form ref="unloadingForm" :model="unloadingObj" label-width="100px">
      <el-form-item label="网点">
        <dockSelect v-model="unloadingObj.ulOutletsId" placeholder="请选择网点" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item label="到仓时间">
        <el-date-picker type="datetime" placeholder="请选择日期" v-model="unloadingObj.ulWarehouseTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="卸柜时间">
        <el-date-picker type="datetime" placeholder="请选择日期" v-model="unloadingObj.ulBoxTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit(1)">保存</el-button>
      <el-button type="success" @click="onSubmit(2)">提交</el-button>
      <el-button @click="cancel">关闭</el-button>
      <el-button type="danger" @click="startUnloading" :disabled="isStartUnloading">开始卸柜</el-button>
    </el-row>

    <!-- 开始卸柜 -->
    <el-dialog title="开始卸柜" :visible.sync="dialogVisible" fullscreen :modal-append-to-body=false append-to-body>
      <startUnloading v-if="dialogVisible" v-bind="$attrs" @closeStart="closeStart" />
    </el-dialog>
  </div>
</template>

<script>
import startUnloading from "./startUnloading.vue";
import { unloadCreate } from "@/api/ecw/boxSea";
import { formatDateStr, serviceMsg } from "../../utils";
import dockSelect from "../common/dockSelect.vue";

/**
 * 卸柜
 */
export default {
  name: "unloading",
  inheritAttrs: false,
  components: { startUnloading, dockSelect },
  data() {
    return {
      // 清关对象
      unloadingObj: {},
      // 弹窗状态
      dialogVisible: false,
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["ulWarehouseTime", "ulBoxTime"]);
    this.unloadingObj = oldData;
  },
  methods: {
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["unloadingForm"].validate((valid) => {
        if (valid) {
          if (operateType === 2) {
            const { keyName } = this.$attrs.currNode;
            const ulStatus = this.$attrs.shipmentObj[keyName];
            if (ulStatus !== 185) {
              this.$message.error("请先通过卸柜审批");
              return;
            }
          }
          unloadCreate({
            ...this.unloadingObj,
            shipmentId: this.$attrs.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
    /* 关闭弹窗 */
    closeStart(type) {
      this.dialogVisible = false;
      if (type) this.cancel(type);
    },
    // 开始卸柜
    startUnloading() {
      this.dialogVisible = true;
    },
  },
  computed: {
    isStartUnloading() {
      const { currNode, shipmentObj } = this.$attrs;
      const status = shipmentObj[currNode.keyName];
      return status === 186 ? true : false;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
