<template>
  <div>
    <el-form ref="airCheckoutForm" :model="airCheckoutObj" :rules="rules" label-width="100px">

      <el-form-item :label="$t('')" prop="deliverType">
        <el-radio-group v-model="airShipmentObj.deliverType" :disabled="true">
          <el-radio v-for="item in deliverTypes" :key="item.value" :label="item.value">{{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <div v-show="airShipmentObj.deliverType === '1'">

          <el-form-item :label="$t('送货时间')" >
                <el-date-picker type="datetime" :disabled="true" :placeholder="$t('请选择日期')" v-model="airShipmentObj.deliverTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>

          <el-form-item :label="$t('送货地址')">
              <el-input v-model="airShipmentObj.deliverAddress" :disabled="true" :placeholder="$t('请输入送货地址')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('车牌')">
              <el-input v-model="airShipmentObj.licensePlate" :disabled="true" :placeholder="$t('请输入车牌')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('司机')">
              <el-input v-model="airShipmentObj.driver" :disabled="true" :placeholder="$t('请输入司机')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('司机联系方式')">
              <el-input v-model="airShipmentObj.driverPhone" :disabled="true" :placeholder="$t('请输入司机联系方式')"></el-input>
          </el-form-item>

      </div>

      <div v-show="airShipmentObj.deliverType === '2'">

          <el-form-item :label="$t('自提时间')">
                <el-date-picker type="datetime" :disabled="true" :placeholder="$t('请选择日期')" v-model="airShipmentObj.deliverTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-form-item>

          <el-form-item :label="$t('自提地址')">
              <el-input v-model="airShipmentObj.deliverAddress" :disabled="true" :placeholder="$t('请输入自提地址')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('车牌')">
              <el-input v-model="airShipmentObj.licensePlate" :disabled="true" :placeholder="$t('请输入车牌')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('司机')">
              <el-input v-model="airShipmentObj.driver" :disabled="true" :placeholder="$t('请输入司机')"></el-input>
          </el-form-item>

          <el-form-item :label="$t('司机联系方式')">
              <el-input v-model="airShipmentObj.driverPhone" :disabled="true" :placeholder="$t('请输入司机联系方式')"></el-input>
          </el-form-item>

      </div>

      <el-form-item :label="$t('出仓日期')" prop="checkoutTime">
          <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="airCheckoutObj.checkoutTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>

      <el-form-item :label="$t('计划箱数')" prop="planNum">
        <el-input-number v-model="airCheckoutObj.planNum" :controls="false" :precision="0" disabled />
      </el-form-item>

      <el-form-item :label="$t('出仓箱数')" prop="realNum">
        <el-input-number v-model="airCheckoutObj.realNum" :controls="false" :precision="0" disabled />
      </el-form-item>

      <el-form-item :label="$t('出仓影像')">
        <ImageUpload :limit="1" :isShowTip=false v-model="airCheckoutObj.checkoutFiles" @input="saveImage" />
      </el-form-item>

      <el-form-item :label="$t('备注')">
        <el-input type="textarea" :rows ="6" v-model="airCheckoutObj.remark" :placeholder="$t('备注')"></el-input>
      </el-form-item>


    </el-form>

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

    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit(1)">{{$t('保存')}}</el-button>
      <el-button :disabled="$attrs.shipmentObj.approvaling" type="success" @click="onSubmit(2)">{{$t('提交')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
      <el-button type="danger" @click="startCheckout">{{$t('确认出仓')}}</el-button>
    </el-row>
  </div>
</template>

<script>
import { getbox } from "@/api/ecw/box";
import { airCheckoutCreate } from "@/api/ecw/boxSea";
import startCheckout from "./checkout/startCheckout.vue";
import userSelect from "./common/userSelect.vue";
import ImageUpload from "@/components/ImageUpload";
import { constantDict, formatDateStr, formatNumberString, serviceMsg } from "../utils";

/**
 * 驳船
 */
export default {
  name: "checkout",
  inheritAttrs: false,
  components: { userSelect, ImageUpload, startCheckout },
  data() {
    return {
      // 空运出仓对象
      airCheckoutObj: {},

      // 空运出货对象
      airShipmentObj: {},
      // 送货方式
      deliverTypes: constantDict.deliverType,
      // 校验
      rules: {
        //deliverType: [{ required: true, message: this.$t("必填"), trigger: "change" }],
        checkoutTime: [{ required: true, message: this.$t("必填"), trigger: "change" }]
      },
      dialogVisible: false
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["checkoutTime"]);
    this.airCheckoutObj = oldData;
    this.$set(this.airCheckoutObj, 'planNum', this.$attrs.shipmentObj.planNum)
    this.$set(this.airCheckoutObj, 'realNum', this.$attrs.shipmentObj.realNum)

    let shipmentdData = { ...this.$attrs.shipmentObj["boxAirShipmentBackVO"] };
    shipmentdData = formatDateStr(shipmentdData, ["deliverTime"]);
    shipmentdData = formatNumberString(shipmentdData, ["deliverType"]);
    this.airShipmentObj = shipmentdData;
  },
  methods: {
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["airCheckoutForm"].validate((valid) => {
        if (valid) {
          if(this.airCheckoutObj.planNum != this.airCheckoutObj.realNum){
            this.$message.error(this.$t("出仓件数与计划出仓件数不一致，请检查"));
            return;
          }
          airCheckoutCreate({
            ...this.airCheckoutObj,
            shipmentId: this.$attrs.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    reload() {
      getbox(this.$attrs.shipmentObj.id).then((res)=>{
        this.$set(this.airCheckoutObj, 'planNum', res.data.planNum)
        this.$set(this.airCheckoutObj, 'realNum', res.data.realNum)
      })
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
    /** 开始出仓 */
    startCheckout() {
      this.dialogVisible = true;
    },
    closeDialog1() {
      this.dialogVisible = false;
    },
    //自动保存图片
    saveImage() {
      const { ldPictures } = this.airCheckoutObj;
      let pictures = ldPictures?.split(",") ?? [];
      let newPictures = [];
      for (const item of pictures) {
        if (item) {
          newPictures.push({
            type: "image",
            url: item,
          });
        }
      }
      airCheckoutCreate({
        ...this.airCheckoutObj,
            shipmentId: this.$attrs.shipmentObj.id,
        operateType: 1,
      }).then(()=>{
        this.$emit("getBoxInfo");
      })
    }
  },
};
</script>

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