<template>
  <div>
    <el-form ref="cabinetForm" :rules="rules" :model="cabinetObj" label-width="80px">
      <el-form-item label="到仓时间">
        <el-date-picker type="date" placeholder="请选择日期" v-model="cabinetObj.ldInWarehouseTime" value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="仓库">
        <el-select v-model="cabinetObj.ldWarehouseType" placeholder="请选择仓库" filterable>
          <el-option v-for="warehouse in $attrs.warehouseList" :key="warehouse.id" :label="warehouse.titleZh" :value="warehouse.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="装柜时间" prop="ldBoxTime">
        <el-date-picker type="date" placeholder="请选择日期" v-model="cabinetObj.ldBoxTime" value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="出仓时间">
        <el-date-picker type="date" placeholder="请选择日期" v-model="cabinetObj.ldOutWarehouseTime" value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="装柜图片">
        <ImageUpload :limit="1" :isShowTip=false v-model="cabinetObj.ldPictures" />
      </el-form-item>
    </el-form>

    <!-- 开始装柜 -->
    <el-dialog title="开始装柜" :visible.sync="dialogVisible" fullscreen :modal-append-to-body=false append-to-body>
      <startPacking v-bind="$attrs" v-if="dialogVisible" />
    </el-dialog>

    <!-- 操作 -->
    <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="startCabinet" :disabled="isStartCabinet">开始装柜</el-button>
    </el-row>
  </div>
</template>

<script>
import startPacking from "./startPacking.vue";
import { cabinetCreate } from "@/api/ecw/boxSea";
import { formatDateStr, serviceMsg } from "../../utils";
import ImageUpload from "@/components/ImageUpload";

/**
 * 装柜
 */
export default {
  name: "cabinet",
  inheritAttrs: false,
  components: { startPacking, ImageUpload },
  data() {
    return {
      // 弹窗
      dialogVisible: false,
      // 装柜对象
      cabinetObj: {},
      // 校验
      rules: {
        ldBoxTime: [{ required: true, message: "必填", trigger: "change" }],
      },
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, [
      "ldInWarehouseTime",
      "ldBoxTime",
      "ldOutWarehouseTime",
    ]);
    this.cabinetObj = oldData;
  },
  methods: {
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["cabinetForm"].validate((valid) => {
        if (valid) {
          if (operateType === 2) {
            const { currNode, shipmentObj } = this.$attrs;
            const status = shipmentObj[currNode.keyName];
            if (status !== 46) {
              this.$message.error("请先进行装柜->审批->确认封柜");
              return;
            }
          }
          cabinetCreate({
            shipmentId: this.$attrs.shipmentObj.id,
            ...this.cabinetObj,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
    /** 开始装柜 */
    startCabinet() {
      this.dialogVisible = true;
    },
  },
  computed: {
    isStartCabinet() {
      const { currNode, shipmentObj } = this.$attrs;
      const status = shipmentObj[currNode.keyName];
      return status === 46 ? true : false;
    },
  },
};
</script>

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