<template>
  <div>
    <el-form ref="bookingForm" :rules="rules" :model="bookingObj" label-width="140px">
      <el-form-item label="SO NO.">
        <el-input v-model="bookingObj.sono" :placeholder="$t('请输入SO NO.')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('船公司类型')">
        <supplierSelect v-model="bookingObj.shipCompanyType" :companyType="'9'" :placeholder="$t('请选择船公司类型')" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
      <el-form-item :label="$t('驳船港')">
        <dockSelect v-model="bookingObj.bargePortId" :placeholder="$t('请选择驳船港')" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item :label="$t('大船港')">
        <dockSelect v-model="bookingObj.bigPortId" :placeholder="$t('请选择大船港')" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item :label="$t('目的港')">
        <dockSelect v-model="bookingObj.destPortId" :placeholder="$t('请选择目的港')" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item :label="$t('订舱公司')">
        <supplierSelect v-model="bookingObj.spaceCompanyId" :companyType="'1'" :placeholder="$t('请选择订舱公司')" :allSupplier="this.$attrs.allSupplier" />
      </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.bargeTime"></el-date-picker>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('预计开船时间')" prop="sailTime">
            <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="bookingObj.sailTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('通知人')" label-width="80px">
            <userSelect v-model="bookingObj.sailNotifyUser" :placeholder="$t('请选择通知人')" :allUsers="this.$attrs.allUsers" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('截补料时间')">
            <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="bookingObj.ladingBillTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('通知人')" label-width="80px">
            <userSelect v-model="bookingObj.ladingBillNotifyUser" :placeholder="$t('请选择通知人')" :allUsers="this.$attrs.allUsers" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('VGM提交时间')">
            <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="bookingObj.customsTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('通知人')" label-width="80px">
            <userSelect v-model="bookingObj.customsNotifyUser" :placeholder="$t('请选择通知人')" :allUsers="this.$attrs.allUsers" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('截关时间')">
            <el-date-picker type="datetime" :placeholder="$t('请选择日期')" value-format="yyyy-MM-dd HH:mm:ss" v-model="bookingObj.cutOffTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('通知人')" label-width="80px">
            <userSelect v-model="bookingObj.cutOffNotifyUser" :placeholder="$t('请选择通知人')" :allUsers="this.$attrs.allUsers" />
          </el-form-item>
        </el-col>
      </el-row>

    </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/boxSea";
import supplierSelect from "./common/supplierSelect.vue";
import dockSelect from "./common/dockSelect.vue";
import userSelect from "./common/userSelect.vue";
import { formatStringNumber, formatDateStr, serviceMsg } from "../utils";
/**
 * 订舱
 */
export default {
  name: "booking",
  inheritAttrs: false,
  components: {
    supplierSelect,
    dockSelect,
    userSelect,
  },
  props: {
    shipmentObj: Object,
  },
  data() {
    return {
      // 订舱对象
      bookingObj: {},
      // 校验
      rules: {
        sailTime: [{ required: true, message: this.$t("必填"), trigger: "change" }],
      },
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.shipmentObj[voName] };
    oldData = formatStringNumber(oldData, ["shipCompanyType", "spaceCompany"]);
    oldData = formatDateStr(oldData, [
      "sailTime",
      "bargeTime",
      "ladingBillTime",
      "customsTime",
      "cutOffTime",
    ]);
    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>
