<template>
  <div>
    <el-form ref="unloadingForm" :model="unloadingObj" label-width="100px">
      <el-form-item :label="$t('网点')">
        <el-select filterable v-model="unloadingObj.ulOutletsId" clearable :placeholder="$t('请选择')">
          <el-option v-for="node in nodes" :key="node.id" :label="$l(node, 'title')" :value="node.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('到仓时间')">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="unloadingObj.ulWarehouseTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('卸柜时间')">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" 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)">{{$t('保存')}}</el-button>
      <el-button type="success" @click="onSubmit(2)">{{$t('提交')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
      <el-button type="danger" @click="startUnloading" :disabled="isStartUnloading">{{$t('开始卸柜')}}</el-button>
    </el-row>

    <!-- 开始卸柜 -->
    <el-dialog :title="$t('开始卸柜')" :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 { getNodePage } from "@/api/ecw/node";

/**
 * 卸柜
 */
export default {
  name: "unloading",
  inheritAttrs: false,
  components: { startUnloading },
  data() {
    return {
      // 清关对象
      unloadingObj: {},
      // 弹窗状态
      dialogVisible: false,
      nodes: [],
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["ulWarehouseTime", "ulBoxTime"]);
    this.unloadingObj = oldData;
    // 进入卸柜,仓库给了默认值为0,并且底下显示无匹配数据。应该默认为空,让手动去选择
    if(this.unloadingObj.ulOutletsId === 0) this.unloadingObj.ulOutletsId = ''
    getNodePage({ pageNo: 1, pageSize: 1000, status: 0 }).then((res) => {
      const { data } = res;
      this.nodes = data?.list ?? [];
    });
  },
  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(this.$t("请先通过卸柜审批"));
              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>