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

      <el-form-item label="预期到港时间" prop="dtEstArrivalTime">
        <el-date-picker type="date" placeholder="请选择日期" v-model="departureObj.dtEstArrivalTime" value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row class="operate-button">
      <el-button type="primary" @click="onSubmit(1)">保存</el-button>
      <el-button type="success" @click="onSubmit(2)">提交</el-button>
      <el-button @click="cancel">关闭</el-button>
      <el-button type="primary" @click="exceptionReg" :disabled="!showMsg">异常登记</el-button>
    </el-row>

    <!-- 对话框 -->
    <el-dialog custom-class="shipping-dialog" title="票异常" :visible.sync="dialogVisible" width="700px" :modal-append-to-body=false append-to-body destroy-on-close>
      <regError @closeDialog="dialogVisible = false" 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: "必填", trigger: "change" },
        ],
      },
      // 弹窗配置
      dialogVisible: false,
      // 提示消息
      showMsg: false,
    };
  },
  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());
    },
  },
  methods: {
    // 时间比较
    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;
      }
    },
    // 异常登记
    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"
      );
    },
  },
};
</script>

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