<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="date" placeholder="请选择日期" v-model="unloadingObj.ulWarehouseTime" value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="卸柜时间">
        <el-date-picker type="date" placeholder="请选择日期" v-model="unloadingObj.ulBoxTime" value-format="yyyy-MM-dd"></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">开始卸柜</el-button>
    </el-row>

    <!-- 开始卸柜 -->
    <el-dialog title="开始卸柜" :visible.sync="dialogVisible" fullscreen :modal-append-to-body=false append-to-body destroy-on-close>
      <startUnloading />
    </el-dialog>
  </div>
</template>

<script>
import startUnloading from "./startUnloading.vue";
import { unloadCreate, serviceMsg } from "@/api/ecw/boxSea";
import { formatDateStr } 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 !== 145) {
              this.$message.warning("请先通过卸柜审批");
              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);
    },
    // 开始卸柜
    startUnloading() {
      this.dialogVisible = true;
    },
  },
};
</script>

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