import { ClassGetter } from '@angular/compiler/src/output/output_ast';
import { Component, HostListener, Inject, OnInit, ViewContainerRef } from '@angular/core';
import {FormGroup,FormBuilder, Validators, FormControl} from '@angular/forms';
import {MatDialogRef,MAT_DIALOG_DATA} from '@angular/material/dialog';
import {TranslateService} from '@ngx-translate/core';
import { NotificationService, NotificationType, InitCommonsService } from '@xdf/commons';
//import {TouchedErrorStateMatcher} from 'src/app/shared/directives/error-matcher/error-matcher';
import { ValidatorUtils } from '@xdf/gallery';
import { ByteSettingsService } from '@xdf/layouts';
import { VueloDialogPruebaComponent } from '../vuelo-dialog-prueba/vuelo-dialog-prueba.component';
import { VuelosService } from '../../services/vuelo.service';

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


  form: FormGroup;
  logoSrc: any;
  minDate: Date;
  record: any;
  mode: any;
  title = "dialog_rate_new_title"
  aeropuertos: any[] = [];
  


  constructor(
    public dialogRef: MatDialogRef<VueloEscalaDialogComponent>,
    private formBuilder :FormBuilder,
    private notificationService: NotificationService,
    private translate: TranslateService,
    private aeropuertoService: VuelosService,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {
    this.record = data.record;
    this.mode = data.mode;
    this.minDate = data.minDate;

  }

  ngOnInit() {
    this.listarAeropuertos();
    this.logoSrc = "assets/images/logo_byte_menu-min.png";
      this.form = this.formBuilder.group({
        fechaLlegada:  new FormControl({ disabled: this.mode === 'view'},  [Validators.required]),
        fechaSalida:  new FormControl({ disabled: this.mode === 'view'},  [Validators.required]),
        codigoAeropuertoOrigen: new FormControl(  [Validators.required]),
        codigoAeropuertoDestino: new FormControl(  [Validators.required]),
        codigoVuelo: new FormControl(  [Validators.required])
      }); 
      console.log(this.record);
      if(this.record){
        this.form.controls['fechaLlegada'].setValue(this.record.fechaLlegada);
        this.form.controls['codigoAeropuertoOrigen'].setValue(this.record.codigoAeropuertoOrigen);
        this.form.controls['codigoAeropuertoDestino'].setValue(this.record.codigoAeropuertoDestino);
        this.form.controls['fechaSalida'].setValue(this.record.fechaSalida);
      }else{
        this.form.controls['codigoAeropuertoOrigen'].setValue('');
        this.form.controls['codigoAeropuertoDestino'].setValue('');

      }
  }

  @HostListener('window:keyup.esc') onKeyUp() {
    this.dialogRef.close(null);
  }
  close() {
    this.dialogRef.close(null);
  }
  save(){
    this.dialogRef.close(this.form.value)
  }

  listarAeropuertos(){
    this.aeropuertoService.getAeropuertos().subscribe(
      res => {
        this.aeropuertos = res;
        console.log(res);
      }
    )
  }

}
