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

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

    <el-row class="operate-button">
      <el-button type="primary">保存</el-button>
      <el-button type="success" @click="onSubmit">提交</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" />
    </el-dialog>
  </div>
</template>

<script>
import regError from "./regError";

/**
 * 起运
 */
export default {
  name: "departure",
  components: { regError },
  data() {
    return {
      // 起运对象
      departureObj: {},
      // 校验
      rules: {
        arrivalTime: [{ required: true, message: "必填", trigger: "change" }],
      },
      // 弹窗配置
      dialogVisible: false,
      // 提示消息
      showMsg: false,
    };
  },
  watch: {
    // 实际开船时间
    "departureObj.actualTime"(val) {
      this.compareDate(val, this.departureObj.arrivalTime);
    },
    // 预计到港时间
    "departureObj.arrivalTime"(val) {
      this.compareDate(this.departureObj.actualTime, val);
    },
  },
  methods: {
    // 时间比较
    compareDate(actualTime, arrivalTime) {
      this.showMsg = false;
      let date1 = null,
        date2 = null;
      if (actualTime) date1 = new Date(actualTime);
      if (arrivalTime) date2 = new Date(arrivalTime);
      if (date1 && date2 && date1 > date2) {
        this.showMsg = true;
      }
    },
    // 异常登记
    exceptionReg() {
      this.dialogVisible = true;
    },
    /** 提交 */
    onSubmit() {
      this.$refs["departureForm"].validate((valid) => {
        if (valid) {
          alert("submit!");
        }
      });
    },
    /** 取消 */
    cancel() {
      this.$emit("closeDialog");
    },
  },
};
</script>

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