<template>
  <div>
    <el-form ref="cabinetForm" :rules="rules" :model="cabinetObj" label-width="80px">
      <el-form-item :label="$t('到仓时间')">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="cabinetObj.ldInWarehouseTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('仓库')">
        <el-select v-model="cabinetObj.ldWarehouseType" :placeholder="$t('请选择仓库')" filterable>
          <el-option v-for="warehouse in $attrs.warehouseList" :key="warehouse.id" :label="$l(warehouse,'title')" :value="warehouse.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('装柜时间')" prop="ldBoxTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="cabinetObj.ldBoxTime" 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="cabinetObj.ldOutWarehouseTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('装柜图片')">
        <ImageUpload :limit="1" :isShowTip=false v-model="cabinetObj.ldPictures" />
      </el-form-item>
    </el-form>

    <!-- 开始装柜 -->
    <el-dialog :title="$t('开始装柜')" :visible.sync="dialogVisible" fullscreen :modal-append-to-body=false append-to-body>
      <startPacking v-bind="$attrs" v-if="dialogVisible" v-on="$listeners" @closeDialog1="closeDialog1" />
    </el-dialog>

    <!-- 操作 -->
    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit(1)">{{$t('保存')}}</el-button>
      <el-button type="success" @click="onSubmit(2)" :disabled="isSeal">{{$t('封柜')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
      <el-button type="danger" @click="startCabinet" :disabled="isStartCabinet">{{$t('开始装柜')}}</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: this.$t("必填"), trigger: "change" },
        ],
      },
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, [
      "ldInWarehouseTime",
      "ldBoxTime",
      "ldOutWarehouseTime",
    ]);

    let pictures = oldData.ldPictures;
    if (oldData.ldPictures) {
      pictures = JSON.parse(oldData.ldPictures);
      if (Array.isArray(pictures)) {
        pictures = pictures.map((item) => item.url).join(",");
      }
    }

    this.cabinetObj = {
      ...oldData,
      ldWarehouseType:
        oldData.ldWarehouseType === 0 ? undefined : oldData.ldWarehouseType,
      ldPictures: pictures,
    };
  },
  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(this.$t("请先进行装柜->审批->确认封柜"));
              return;
            }
          }
          const { ldPictures } = this.cabinetObj;
          let pictures = ldPictures?.split(",") ?? [];
          let newPictures = [];
          for (const item of pictures) {
            if (item) {
              newPictures.push({
                type: "image",
                url: item,
              });
            }
          }

          cabinetCreate({
            shipmentId: this.$attrs.shipmentObj.id,
            ...this.cabinetObj,
            ldPictures: newPictures.length ? JSON.stringify(newPictures) : "",
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    closeDialog1(type) {
      this.dialogVisible = false;
      if (type) this.cancel(type);
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
    /** 开始装柜 */
    startCabinet() {
      this.dialogVisible = true;
    },
  },
  computed: {
    isStartCabinet() {
      const { currNode, shipmentObj } = this.$attrs;
      const status = shipmentObj[currNode.keyName];
      return [46, 47].includes(status) ? true : false;
    },
    isSeal() {
      const { currNode, shipmentObj } = this.$attrs;
      const status = shipmentObj[currNode.keyName];
      return status === 47 ? true : false;
    },
  },
};
</script>

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