<template>
  <div class="app-departure">
    <el-form ref="departureForm" :rules="rules" :model="departureObj" label-width="120px">
      <el-form-item :label="$t('预计开船时间')">
        {{getSailingTime()}}
      </el-form-item>
      <el-form-item :label="$t('实际开船时间')" prop="dtRealShipTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="departureObj.dtRealShipTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        <p class="message-area">{{$t('订单状态与短信通知，将到指定时间更新与发送')}}</p>
        <p class="message-area" v-show="showMsg">{{$t('实际开船实际与预计时间不符')}}</p>
      </el-form-item>

      <el-form-item :label="$t('预期到港时间')" prop="dtEstArrivalTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="departureObj.dtEstArrivalTime" 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(1)">{{$t('保存')}}</el-button>
      <el-button type="success" @click="onSubmit(2)">{{$t('提交')}}</el-button>
      <el-button @click="cancel">{{$t('关闭')}}</el-button>
      <el-button type="primary" @click="exceptionReg" :disabled="!isShowError">{{$t('异常登记')}}</el-button>
    </el-row>

    <!-- 对话框 -->
    <el-dialog custom-class="shipping-dialog" :title="$t('票异常')" :visible.sync="dialogVisible" width="700px" :modal-append-to-body=false append-to-body destroy-on-close>
      <regError @closeDialog="regCloseDialog" v-bind="$attrs" />
    </el-dialog>
  </div>
</template>

<script>
import regError from "../../regError";
import { shippingCreate } from "@/api/ecw/boxSea";
import dayjs from "dayjs";
import { formatDateStr, serviceMsg } from "../utils";

/**
 * 起运
 */
export default {
  name: "departure",
  inheritAttrs: false,
  components: { regError },
  data() {
    return {
      // 起运对象
      departureObj: {},
      // 校验
      rules: {
        dtEstArrivalTime: [
          { required: true, message: this.$t("必填"), trigger: "change" },
        ],
        dtRealShipTime: [
          { required: true, message: this.$t("必填"), trigger: "change" },
        ]
      },
      // 弹窗配置
      dialogVisible: false,
      // 提示消息
      showMsg: false,
      // 异常登记
      isShowError: false,
      inspectionTimeShipping: this.getDictDatas(
        this.DICT_TYPE.BOX_INSPECTION_TIME_SHIPPING
      )[0].value,
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["dtRealShipTime"], "YYYY-MM-DD HH:mm:ss");
    oldData = formatDateStr(oldData, ["dtEstArrivalTime"]);
    this.departureObj = oldData;
  },
  watch: {
    // 实际开船时间
    "departureObj.dtRealShipTime"(val) {
      this.compareDate(val, this.getSailingTime());
      this.compareDate1(val, this.getSailingTime());
    },
  },
  methods: {
    regCloseDialog(type) {
      this.dialogVisible = false;
      if (type === "error") {
        this.$emit("getBoxInfo");
      }
    },
    // 时间比较
    compareDate(dtRealShipTime, sailTime) {
      this.showMsg = false;
      let date1 = null,
        date2 = null;
      if (dtRealShipTime) date1 = dayjs(dtRealShipTime);
      if (sailTime) date2 = dayjs(sailTime);
      if (date1 && date2 && date1.isAfter(date2)) {
        this.showMsg = true;
      }
    },
    compareDate1(dtRealShipTime, sailTime) {
      this.isShowError = false;
      let date1 = null,
        date2 = null;
      if (dtRealShipTime) date1 = dayjs(dtRealShipTime);
      if (sailTime) date2 = dayjs(sailTime);
      if (date1 && date2) {
        const delayDay = date1.diff(date2, "day");
        if (delayDay >= this.inspectionTimeShipping) this.isShowError = true;
      }
    },
    // 异常登记
    exceptionReg() {
      this.dialogVisible = true;
    },
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["departureForm"].validate((valid) => {
        if (valid) {
          shippingCreate({
            ...this.departureObj,
            shipmentId: this.$attrs.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },
    // 预计开船时间
    getSailingTime() {
      return dayjs(this.$attrs.shipmentObj.bookSeaInfo.sailTime).format(
        "YYYY-MM-DD HH:mm:ss"
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.app-departure {
  .message-area {
    margin: 0;
    color: red;
  }
}
</style>
