import { Component, OnInit } from '@angular/core';
import { Pasajero } from '../../../pasajeros/interfaces/pasajeros.inteface';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ReservasService } from '../../services/reservas.service';
import { PasajerosService } from '../../../pasajeros/services/pasajeros.service';
import { EscalasService } from '../../../vuelos/services/escalas.service';

@Component({
  selector: 'app-detalles',
  templateUrl: './detalles.component.html',
  styleUrls: ['./detalles.component.css']
})
export class DetallesComponent implements OnInit {

  id: number = 0;
  pasajero: Pasajero | undefined;
  escalasVuelo: any[] = [];

  reservaForm: FormGroup = this.formBuilder.group({
    diaReservacion: [ '' ],
    estado: [ ''],
    pasajeroId: [ '' ],
    idVuelo: [ '' ],
  })

  constructor(
    private formBuilder: FormBuilder,
    private activateRoute: ActivatedRoute,
    private router: Router,
    private reservasService: ReservasService,
    private pasajeroService: PasajerosService,
    private escalasService: EscalasService
  ){}  

  ngOnInit(): void {
    this.id = this.activateRoute.snapshot.params['id'];
    this.llenarFormulario();  
  }

  private llenarFormulario(){
    this.reservasService.getReserva(this.id).subscribe(
      res => {
        this.getEscalasVuelo();
        this.getPasajero(res.pasajeroId);
        this.reservaForm.patchValue(res);
      }
    )
  }

  getPasajero(id:number){
    this.pasajeroService.getPasajero(id).subscribe(
      res => {
        this.pasajero = res;
      }
    )
  }

  getEscalasVuelo(){
    this.reservasService.getReserva(this.id).subscribe(
      res => {
        this.escalasVuelo = res.escalas;
      }
    )
  }
  
}
