<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="请输入SO NO."></el-input>
      </el-form-item>
      <el-form-item label="船公司类型">
        <supplierSelect v-model="bookingObj.shipCompanyType" :companyType="'9'" placeholder="请选择船公司类型" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
      <el-form-item label="驳船港">
        <dockSelect v-model="bookingObj.bargePortId" placeholder="请选择驳船港" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item label="大船港">
        <dockSelect v-model="bookingObj.bigPortId" placeholder="请选择大船港" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item label="目的港">
        <dockSelect v-model="bookingObj.destPortId" placeholder="请选择目的港" :allDocks="this.$attrs.allDocks" />
      </el-form-item>
      <el-form-item label="订舱公司">
        <supplierSelect v-model="bookingObj.spaceCompanyId" :companyType="'1'" placeholder="请选择订舱公司" :allSupplier="this.$attrs.allSupplier" />
      </el-form-item>
      <el-form-item label="预计驳船时间" prop="bargeTime">
        <el-date-picker type="date" placeholder="请选择日期" value-format="yyyy-MM-dd" v-model="bookingObj.bargeTime"></el-date-picker>
      </el-form-item>

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

      <el-row>
        <el-col :span="12">
          <el-form-item label="预计提单补料时间">
            <el-date-picker type="date" placeholder="请选择日期" value-format="yyyy-MM-dd" v-model="bookingObj.ladingBillTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通知人" label-width="80px">
            <userSelect v-model="bookingObj.ladingBillNotifyUser" placeholder="请选择通知人" :allUsers="this.$attrs.allUsers" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="预计报关时间">
            <el-date-picker type="date" placeholder="请选择日期" value-format="yyyy-MM-dd" v-model="bookingObj.customsTime"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="通知人" label-width="80px">
            <userSelect v-model="bookingObj.customsNotifyUser" placeholder="请选择通知人" :allUsers="this.$attrs.allUsers" />
          </el-form-item>
        </el-col>
      </el-row>

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

    </el-form>
    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit">提交</el-button>
      <el-button @click="cancel">关闭</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: {
        bargeTime: [{ required: true, message: "必填", trigger: "change" }],
        sailTime: [{ required: true, message: "必填", 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>
