<template>
  <div>
    <el-form ref="cusClearanceForm" :rules="rules" :model="cusClearanceObj" label-width="120px">
      <el-form-item label="清关代理">{{cusClearanceObj.agent}}</el-form-item>
      <el-form-item label="预计清关时间" prop="expectedTime">
        <el-date-picker type="date" placeholder="请选择日期" v-model="cusClearanceObj.expectedTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="清关时间" prop="confirmTime">
        <el-date-picker type="datetime" placeholder="请选择日期" v-model="cusClearanceObj.confirmTime"></el-date-picker>
        <p class="message-area" v-show="showMsg">清关时间与预计时间不符，如有异常请登记</p>
      </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: "cusClearance",
  components: {
    regError,
  },
  data() {
    return {
      // 清关对象
      cusClearanceObj: {
        agent: "test111",
      },
      // 校验
      rules: {
        expectedTime: [{ required: true, message: "必填", trigger: "change" }],
        confirmTime: [{ required: true, message: "必填", trigger: "change" }],
      },
      // 弹窗配置
      dialogVisible: false,
      // 提示消息
      showMsg: false,
    };
  },
  watch: {
    // 预计清关时间
    "cusClearanceObj.expectedTime"(val) {
      this.compareDate(val, this.cusClearanceObj.confirmTime);
    },
    // 清关时间
    "cusClearanceObj.confirmTime"(val) {
      this.compareDate(this.cusClearanceObj.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 && date2 !== date1) {
        this.showMsg = true;
      }
    },
    // 异常登记
    exceptionReg() {
      this.dialogVisible = true;
    },
    /** 提交 */
    onSubmit() {
      this.$refs["cusClearanceForm"].validate((valid) => {
        if (valid) {
          alert("submit!");
        }
      });
    },
    /** 取消 */
    cancel() {
      this.$emit("closeDialog");
    },
  },
};
</script>

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