<template>
  <div>
    <el-form ref="arrivalForm" :rules="rules" :model="arrivalObj" label-width="120px">
      <el-form-item label="预计到港时间">{{arrivalObj.expectedTime}}</el-form-item>
      <el-form-item label="实际到港时间">
        <el-date-picker type="datetime" placeholder="请选择日期" v-model="arrivalObj.actualTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="确认到港" prop="confirmTime">
        <el-date-picker type="datetime" placeholder="请选择日期" v-model="arrivalObj.confirmTime">
        </el-date-picker>
        <p class="message-area" v-show="showMsg">请确认是否有异常，如有异常请进行异常登记</p>
      </el-form-item>
      <el-form-item label="卸港时间">
        <el-date-picker type="date" placeholder="请选择日期" v-model="arrivalObj.dischargeTime"></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";
import dayjs from "dayjs";

/**
 * 到港
 */
export default {
  name: "arrival",
  components: {
    regError,
  },
  data() {
    return {
      // 到港对象
      arrivalObj: {
        expectedTime: "2022-07-18",
      },
      // 校验
      rules: {
        actualTime: [{ required: true, message: "必填", trigger: "change" }],
      },
      // 弹窗配置
      dialogVisible: false,
      // 提示消息
      showMsg: false,
    };
  },
  watch: {
    // 预计到港时间
    "arrivalObj.expectedTime"(val) {
      this.compareDate(val, this.arrivalObj.confirmTime);
    },
    // 确认到港时间
    "arrivalObj.confirmTime"(val) {
      this.compareDate(this.arrivalObj.expectedTime, val);
    },
  },
  methods: {
    // 时间比较
    compareDate(expectedTime, confirmTime) {
      this.showMsg = false;
      let date1 = null,
        date2 = null;
      if (expectedTime) date1 = dayjs(expectedTime);
      if (confirmTime) date2 = dayjs(confirmTime);
      if (date1 && date2) {
        const days = date2.date() - date1.date();
        if (days > 5) {
          this.showMsg = true;
        }
      }
    },
    // 异常登记
    exceptionReg() {
      this.dialogVisible = true;
    },
    /** 提交 */
    onSubmit() {
      this.$refs["arrivalForm"].validate((valid) => {
        if (valid) {
          alert("submit!");
        }
      });
    },
    /** 取消 */
    cancel() {
      this.$emit("closeDialog");
    },
  },
};
</script>

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