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

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

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

    costForm: FormGroup = new FormGroup({
      departureDate: new FormControl(null, [Validators.required]),
      arrivalDate: new FormControl(null, [Validators.required]),
      price: new FormControl(null, [Validators.required])
    });

    ngOnInit(): void {
    }

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

    onSave() {
      const cost = {
        departureDate: new DatePipe('en-US').transform(this.costForm.get('departureDate').value, "yyyy-MM-dd'T'HH:mm:ss"),
        arrivalDate: new DatePipe('en-US').transform(this.costForm.get('arrivalDate').value, "yyyy-MM-dd'T'HH:mm:ss"),
        amount: this.costForm.value.price,
        isNew: true
      }
      this.dialogRef.close({status: true, cost: cost});
    }

}
