import { Component, OnInit, ViewChild, Input, AfterViewInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { DecimalPipe } from '@angular/common';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { ColumnTemplate } from '@xdf/gallery';
import * as moment from 'moment';
import { VueloDialogService } from '../reservation-vuelo-dialog/services/reservation-vuelo-dialog.service';
import { ReservacionesService } from '../../services/reservaciones.service';

@Component({
  selector: 'byte-reservation-vuelo-grid',
  templateUrl: './reservation-vuelo-grid.component.html',
  styleUrls: ['./reservation-vuelo-grid.component.scss']
})
export class ReservationVueloGridComponent implements OnInit, AfterViewInit {


  @Input() mode;
  
  decimalParameter = 2;

  @ViewChild('paginator',{static: true}) paginator: MatPaginator;
  @ViewChild(MatSort, { static: false }) sort: MatSort | undefined;

  sortDirection = 'desc';
  sortColumn = 'fechaLlegada';

  displayedColumns: string[] = ['id', 'agente','pasajero','actions'];
  dataSource!: MatTableDataSource<any>;

  constructor(
    private taxRateDialogService : VueloDialogService,
    private decimalPipe : DecimalPipe,
    private reservationService: ReservacionesService
  ) {
   }
  ngAfterViewInit(): void {
    this.paginator.pageSize = 5
    this.dataSource.paginator = this.paginator
    this.sort.sortChange.subscribe(() => {
      console.log()
    })
  }

  autoSort(data) {
    return data.sort((a, b) => new Date(a.fechaLlegada) < new Date(b.fechaLlegada) ? 1 : -1)
  }

  numberWithCommas(x): string {
    return x ? this.decimalPipe.transform(x, '1.'+this.decimalParameter+'-'+this.decimalParameter) : '';
  }

  getDateFormat(liquidationDate) {
    return moment(liquidationDate).format('DD-MM-YYYY');
  }

  ngOnInit(): void {
    this.dataSource = new MatTableDataSource<any>();

  }

  onEdit(item) {
    this.taxRateDialogService.loadComponent(item, this.mode, this.getMaxDate()).subscribe(
      data => {
        if(data !== null) {
          let dataSource = [...this.dataSource.data];
          let pos = dataSource.findIndex(x => x === item)
          dataSource[pos] = data;
          this.dataSource.data = this.autoSort(dataSource);;
        }
      }
    )
  }

  onDelete(item) {
    let dataSource = [...this.dataSource.data];
    let pos = dataSource.findIndex(x => x === item)
    dataSource.splice(pos, 1)
    this.dataSource.data = this.autoSort(dataSource);;
  }

  getMaxDate() {
    let maxDate 
    if(this.dataSource.data.length > 0) {
      maxDate = new Date(Math.max.apply(null,this.dataSource.data.map(x => new Date(x.fechaLlegada))));
      maxDate.setDate(maxDate.getDate() + 1);
    } else {
      maxDate = new Date();
    }
    return maxDate;
  }

  onCreate() {
    this.taxRateDialogService.loadComponent(null, this.mode, this.getMaxDate()).subscribe(
      data => {
        if(data !== null) {
          this.dataSource = new MatTableDataSource(data['escalas']);
        }
      }
    )
  }


  listarEscalasByVuelo(id: number){
    this.reservationService.getEscalasByVueloId(id).subscribe(
      res => {
      }
    )
  }


  
}