<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="装柜图片">
        <el-upload action="" :limit="1" :file-list="cabinetObj.ldPictures">
          <el-button size="small" type="primary">选择文件</el-button>
        </el-upload>
      </el-form-item>
    </el-form>

    <!-- 开始装柜 -->
    <el-dialog title="开始装柜" :visible.sync="dialogVisible" fullscreen :modal-append-to-body=false append-to-body destroy-on-close>
      <startPacking />
    </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">开始装柜</el-button>
    </el-row>
  </div>
</template>

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

/**
 * 装柜
 */
export default {
  name: "cabinet",
  inheritAttrs: false,
  components: { startPacking },
  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) {
          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;
    },
  },
};
</script>

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