<template>
  <div>
    <el-form ref="bookingForm" :rules="rules" :model="bookingObj" label-width="140px">
      <el-form-item :label="$t('提单号')">
        <el-input v-model="bookingObj.blNo" :placeholder="$t('请输入提单号')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('起运港')">
        <dockSelect v-model="bookingObj.departurePortId" :placeholder="$t('请选择起运港')" portType="2" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item :label="$t('目的港')">
        <dockSelect v-model="bookingObj.destPortId" :placeholder="$t('请选择目的港')" portType="2" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item :label="$t('Shipper')">
        <supplierSelect v-model="bookingObj.shipperId" :companyType="'5'" :placeholder="$t('请选择Shipper')" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
      <el-form-item :label="$t('航空公司')">
        <supplierSelect v-model="bookingObj.airlineCompanyId" :companyType="'7'" :placeholder="$t('请选择航空公司')" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
      <el-form-item :label="$t('预计起飞时间')" prop="flyTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="bookingObj.flyTime"></el-date-picker>
      </el-form-item>

      <el-form-item :label="$t('截关时间')">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="bookingObj.closingTime"></el-date-picker>
      </el-form-item>

      <el-form-item :label="$t('截补料时间')">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="bookingObj.replenishmentTime"></el-date-picker>
      </el-form-item>

      <el-form-item :label="$t('航程')" prop="voyage">
        <el-radio-group v-model="bookingObj.voyage">
          <el-radio v-for="item in voyageData" :key="item.value" :label="item.value">{{item.label}}</el-radio>
        </el-radio-group>
      </el-form-item>

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

      <el-form-item :label="$t('出货渠道')">
        <el-select v-model="bookingObj.airlineTypeId" :placeholder="$t('请选择出货渠道')">
          <el-option v-for="item in channelList" :label="$l(item, 'name')" :value="item.channelId" :key="item.channelId"></el-option>
        </el-select>
      </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 { booking } from "@/api/ecw/boxAir";
import supplierSelect from "./common/supplierSelect.vue";
import dockSelect from "./common/dockSelect.vue";
import userSelect from "./common/userSelect.vue";
import { formatStringNumber, formatDateStr, serviceMsg, constantDict } from "../utils";
import { getChannelList } from '@/api/ecw/channel'
/**
 * 订舱
 */
export default {
  name: "booking",
  inheritAttrs: false,
  components: {
    supplierSelect,
    dockSelect,
    userSelect,
  },
  props: {
    shipmentObj: Object,
  },
  data() {
    return {
      // 订舱对象
      bookingObj: {},
      channelList: [],
      voyageData: constantDict.voyageData,
      // 校验
      rules: {
        flyTime: [{ required: true, message: this.$t("必填"), trigger: "change" }],
        voyage: [{ required: true, message: this.$t("必填"), trigger: "change" }],
      },
    };
  },
  created() {
    getChannelList().then((res) => (this.channelList = res.data))
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.shipmentObj[voName] };
    oldData = formatStringNumber(oldData, ["shipCompanyType", "spaceCompany"]);
    oldData = formatDateStr(oldData, [
      "flyTime",
      "closingTime",
      "replenishmentTime",
      "deliveryTime"
    ]);
    this.bookingObj = oldData;
  },
  methods: {
    /** 提交 */
    onSubmit() {
      this.$refs["bookingForm"].validate((valid) => {
        if (valid) {
          booking({
            shipmentId: this.shipmentObj.id,
            ...this.bookingObj,
            operateType: 2,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
  },
};
</script>
