<template>
  <div>
    <el-form ref="twoWayTakeoffForm" :rules="rules" :model="twoWayTakeoffObj" label-width="120px">
      <el-form-item :label="$t('预计起飞时间')">
        {{getTakeOffTime()}}
      </el-form-item>
      <el-form-item :label="$t('实际起飞时间')" prop="dtRealFlyTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="twoWayTakeoffObj.dtRealFlyTime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('实际头程时间')" prop="realHeadTravelTime">
        <el-date-picker type="datetime" :placeholder="$t('请选择日期')" v-model="twoWayTakeoffObj.realHeadTravelTime" 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">{{$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="dialogVisible = false" v-bind="$attrs" />
    </el-dialog>
  </div>
</template>

<script>
import regError from "../../regError";
import { takeoffCreate } from "@/api/ecw/boxAir";
import { formatDateStr, serviceMsg } from "../utils";

/**
 * 起飞
 */
export default {
  name: "twoWayTakeoff",
  inheritAttrs: false,
  components: {
    regError,
  },
  data() {
    return {
      // 清关对象
      twoWayTakeoffObj: {},
      // 校验
      rules: {
        dtRealFlyTime: [{ required: true, message: this.$t("必填"), trigger: "blur" }],
        realHeadTravelTime: [{ required: true, message: this.$t("必填"), trigger: "blur" }],
      },
      // 弹窗配置
      dialogVisible: false,
      // 提示消息
      showMsg: false,
    };
  },
  created() {
    const voName = this.$attrs.currNode.voName;
    let oldData = { ...this.$attrs.shipmentObj[voName] };
    oldData = formatDateStr(oldData, ["dtRealFlyTime"], "YYYY-MM-DD HH:mm:ss");
    oldData = formatDateStr(oldData, ["realHeadTravelTime"], "YYYY-MM-DD HH:mm:ss");
    this.twoWayTakeoffObj = oldData;
  },
  watch: {
  },
  methods: {
    // 异常登记
    exceptionReg() {
      this.dialogVisible = true;
    },
    /** 提交 */
    onSubmit(operateType) {
      this.$refs["twoWayTakeoffForm"].validate((valid) => {
        if (valid) {
          takeoffCreate({
            ...this.twoWayTakeoffObj,
            shipmentId: this.$attrs.shipmentObj.id,
            operateType,
          }).then((res) => {
            serviceMsg(res, this).then(() => {
              this.cancel("submit");
            });
          });
        }
      });
    },
    /** 取消 */
    cancel(type) {
      this.$emit("closeDialog", type);
    },

     // 预计开船时间
     getTakeOffTime() {
      return dayjs(this.$attrs.shipmentObj.bookAirInfo.flyTime).format(
        "YYYY-MM-DD HH:mm:ss"
      );
    },
  },
};
</script>

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