import { DatePipe } from '@angular/common';
import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';

@Component({
  selector: 'byte-dialog-flight-scale',
  templateUrl: './dialog-flight-scale.component.html',
  styleUrls: ['./dialog-flight-scale.component.scss']
})
export class DialogFlightScaleComponent implements OnInit {

  constructor(
  public dialogRef: MatDialogRef<DialogFlightScaleComponent>,
  @Inject(MAT_DIALOG_DATA) public data: any) {}

  scaleForm: FormGroup = new FormGroup({
    airportOrigin: new FormControl(null, [Validators.required]),
    airportDestination: new FormControl(null, [Validators.required]),
    departureDate: new FormControl(null, [Validators.required]),
    arrivalDate: new FormControl(null, [Validators.required])
  });

  ngOnInit(): void {

  }

  onNoClick() {
    this.data.status = false;
    this.dialogRef.close({ status: false });
  }

  onSave() {
    const scale = {
      airportOriginId: this.scaleForm.value.airportOrigin,
      airportDestinationId: this.scaleForm.value.airportDestination,
      departureDate: new DatePipe('en-US').transform(this.scaleForm.get('departureDate').value, "yyyy-MM-dd'T'HH:mm:ss"),
      arrivalDate: new DatePipe('en-US').transform(this.scaleForm.get('arrivalDate').value, "yyyy-MM-dd'T'HH:mm:ss")
    }
    console.log(scale);
    this.dialogRef.close({status: true, scale: scale});
  }
}
