<template>
  <div class="shipping-update-error">
    <div class="message-title">{{headerTitle}}</div>
    <el-form ref="arrivalForm" :rules="rules" :model="airArrivalInfo" label-width="120px">
      <el-form-item :label="$t('实际二程起飞时间')" prop="actSecondTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="airArrivalInfo.actSecondTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('预计到港时间')" prop="estTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="airArrivalInfo.estTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('实际到港时间')" prop="actTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="airArrivalInfo.actTime" 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">{{$t('确定')}}</el-button>
      <el-button @click="cancel">{{$t('取消')}}</el-button>
    </el-row>
  </div>
</template>

<script>
import { updateOrderArrival, updateAllOrderArrival } from "@/api/ecw/boxAir";

export default {
  name: "updateError",
  inheritAttrs: false,
  components: {},
  props: {
    shipmentObj: Object,
    arrivalInfo: Object,
  },
  data() {
    return {
      // 到港对象
      airArrivalInfo: {},
      // 校验
      rules: {
        actSecondTime: [
          { required: true, message: this.$t("必填"), trigger: "change" },
        ],
        actTime: [
          { required: true, message: this.$t("必填"), trigger: "change" },
        ]
      },
      headerTitle: "",
    };
  },
  watch: {
    arrivalInfo: {
      handler: function (val) {
        const { orderList, type } = val;

        if (type === "selected") {
          this.headerTitle = this.$t(
            "确定给{selfNo}下的{orderNos}更新状态吗？",
            {
              selfNo: this.shipmentObj.selfNo,
              orderNos: orderList.map((item) => item.orderNo).join(",") ?? "",
            }
          );
        } else {
          this.headerTitle = this.$t(
            `确定给{selfNo}下的${
              type === "all" ? this.$t("所有订单") : this.$t("所选订单")
            }更新状态吗？`,
            {
              selfNo: this.shipmentObj.selfNo,
            }
          );
        }
      },
      immediate: true,
    },
  },
  methods: {
    onSubmit() {
      this.$refs["arrivalForm"].validate((valid) => {
        if (valid) {
          if(this.arrivalInfo.type == 'selected'){
            let param = {
              shipmentId: this.shipmentObj.id,
              orderIdList: this.arrivalInfo.orderList
            }
            updateOrderArrival({...this.airArrivalInfo, ...param}).then(()=>{
              this.$message.success("成功");
              this.cancel()
            })
          }else{
            let param = {
              shipmentId: this.shipmentObj.id
            }
            updateAllOrderArrival({...this.airArrivalInfo, ...param}).then(()=>{
              this.$message.success("成功");
              this.cancel()
            })
          }
        }
      });
    },
    cancel() {
      this.$emit("closeDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
.shipping-update-error {
  .message-title {
    text-align: center;
    font-size: 20px;
    margin: 0 20px 10px;
  }
}
</style>
