import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormGroup, FormBuilder, FormControl } from '@angular/forms';
import { MatStepper } from '@angular/material';
import { ActivatedRoute } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { NotificationService } from '@xdf/commons';
import { ValidatorUtils } from '@xdf/gallery';
import { AereopuertoService } from '../../services/aereopuerto.service';
import { DatePipe } from '@angular/common';
import { Costo } from '../../interfaces/costo';
import { cos } from '@amcharts/amcharts4/.internal/core/utils/Math';
import { CostoService } from '../../services/costo.service';

@Component({
  selector: 'byte-costos',
  templateUrl: './costos.component.html',
  styleUrls: ['./costos.component.scss']
})

export class CostosComponent implements OnInit {

  @Input()
  stepper: MatStepper;

  @Output()
  nextPage: EventEmitter<boolean> = new EventEmitter<boolean>();

  @Output()
  flag: EventEmitter<boolean> = new EventEmitter<boolean>();

  detail: any;
  flightId: number;
  type: boolean;
  form: FormGroup;
  dataSource = [];
  airline = [{ name: "aereolinea1" }, { name: "aereolinea2" }, { name: "aereolinea3" }]
  listAirport = [];
  viewMode: boolean;
  mode = 'new';



  constructor(
    private formBuilder: FormBuilder,
    private activatedRoute: ActivatedRoute,
    private airportService: AereopuertoService,
    private datePipe: DatePipe,
    private costService: CostoService,
    private notificationService: NotificationService,
    private translateService: TranslateService,
    private validatorUtils: ValidatorUtils
  ) {

    this.mode = this.activatedRoute.snapshot.data.mode;
    this.viewMode = this.mode === 'view';
    this.form = this.formBuilder.group({
      id: new FormControl({ value: 0, disabled: this.viewMode }),
      airline: new FormControl({ value: '', disabled: this.viewMode }),
      amount: new FormControl({ value: '', disabled: this.viewMode }),
      fromDate: new FormControl({ value: '', disabled: this.viewMode }),
      toDate: new FormControl({ value: '', disabled: this.viewMode }),
    });
    this.airportService.getAllAirports().subscribe(data => {
      this.listAirport = data;
    });
  }

  ngOnInit() {
    if (this.mode != 'create') {
      this.type = true;
    }
  }

  getForm() {
    return this.dataSource;
  }

  buildForm(detail: any) {
    this.detail = detail;
    if (detail) {
      this.form.patchValue({
        airline: detail.airline,
        amount: detail.amount,
        fromDate: detail.fromDate,
        toDate: detail.toDate
      });
    }
  }

  saveGeneralInformation(validateForm: boolean): boolean {
    this.detail.airline = this.form.controls.airline.value;
    this.detail.amount = this.form.controls.amount.value;
    this.detail.fromDate = this.form.controls.fromDate.value;
    this.detail.toDate = this.form.controls.toDate.value;
    return true;
  }

  addTable() {
    let cost = new Costo();
    this.form.controls.fromDate.setValue(this.datePipe.transform(this.form.controls.fromDate.value, 'yyyy-MM-dd'));
    this.form.controls.toDate.setValue(this.datePipe.transform(this.form.controls.toDate.value, 'yyyy-MM-dd'));
    cost.id = this.form.controls.id.value;
    cost.airline = this.form.controls.airline.value;
    cost.fromDate = this.form.controls.fromDate.value;
    cost.amount = this.form.controls.amount.value;
    cost.toDate = this.form.controls.toDate.value;
    if (this.mode == 'create') {
      this.dataSource.push(cost);
    } else {
      if (this.dataSource.length != 0) {
        this.dataSource.filter(item => {
          if (item.id == cost.id) {
            item.airline = cost.airline;
            item.fromDate = cost.fromDate;
            item.amount = cost.amount;
            item.toDate = cost.toDate;
          }
        });
      }
    }
  }

  editItem(body: any) {
    this.form.patchValue({
      id: body.id,
      airline: body.airline,
      amount: body.amount,
      fromDate: body.fromDate,
      toDate: body.toDate,
    });
  }

  deleteItem(body: any) {
    if (body.id == 0) {
      this.dataSource = this.dataSource.filter(item => item !== body);
    } else {
      this.dataSource = this.dataSource.filter(item => item.id !== body.id);
    }
  }

  setId(id: number) {
    this.flightId = id;
    if (this.mode != 'create') {
      this.costService.getAllFlight(this.flightId).subscribe(data => {
        data.forEach(info => {
          let cost = new Costo();
          cost.id = info.id;
          cost.airline = info.airline;
          cost.fromDate = info.fromDate;
          cost.toDate = info.toDate;
          cost.amount = info.amount;
          this.dataSource.push(cost);
        });
      });
    }
  }
  reset() {
    this.form.reset();
  }

  isDirty() {
    return this.form.dirty;
  }

  save() {
    return this.flag.emit(true);
  }

  getErrors(name: string, placeholder: string) {
    return this.validatorUtils.getErrors(this.form.controls[name] as FormControl, name, placeholder);
  }
}
