import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { PageEvent } from '@angular/material/paginator';
import { Router } from '@angular/router';
import Swal from 'sweetalert2';
import { Vuelo } from '../../interfaces/vuelos.interface';
import { VuelosService } from '../../services/vuelos.service';
import {MatPaginator} from '@angular/material/paginator';
import {MatSort} from '@angular/material/sort';
import {MatTableDataSource} from '@angular/material/table';

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

  displayedColumns: string[] = ['id', 'codigoAerolina', 'codigoAeronave', 'codigoDestino', 'costo','opciones'];
  dataSource!: MatTableDataSource<any>;
  @ViewChild(MatSort) sort!: MatSort;
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild('input') entradaFiltro: ElementRef | undefined;
  filtroInput: string = '';
  vuelos: Vuelo[] = [];

  constructor(
    private router: Router,
    private vuelosService: VuelosService
  ){}

  ngOnInit(): void {
    setTimeout(() => { 
      this.listarVuelos();
    }, 200);
  }

  eliminar(event:any, id: number){
    event.stopPropagation(); 
    Swal.fire({
      title: 'Estas seguro?' + ' Vuelo ID: ' + id,
      text: "No se podran revertir los cambios!",
      icon: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Si, eliminar!',
      cancelButtonText: 'Cancelar'
    }).then((result) => {
      if (result.isConfirmed) {
        this.eliminarVuelo(id);
        const Toast = Swal.mixin({
          toast: true,
          position: 'top-end',
          showConfirmButton: false,
          timer: 2000,
          timerProgressBar: true,
          didOpen: (toast) => {
            toast.addEventListener('mouseenter', Swal.stopTimer)
            toast.addEventListener('mouseleave', Swal.resumeTimer)
          }
        })
        Toast.fire({
          icon: 'success',
          title: 'Se elimino correctamente'
        })
      }
    })
  }

  detalles(id:number){
    this.router.navigate(['/vuelos/detalles/',id ]);
  }

  actualizar(id: number){
    this.router.navigate(['/vuelos/agregar/',id ]);
  }

  listarVuelos(){
    this.vuelosService.getVuelos().subscribe(
      res => {
        this.vuelos = res;
        this.dataSource = new MatTableDataSource(this.vuelos);
        this.dataSource.paginator = this.paginator;  
        this.dataSource.sort = this.sort;
      }
    )
  }

  aplicarFiltro(event: Event) {
    const filterValue = this.filtroInput;
    this.dataSource.filter = filterValue.trim().toLowerCase();
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

  eliminarFiltros(){
    this.filtroInput = '';
    this.dataSource.filter = '';
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

  eliminarVuelo(id: number){
    this.vuelosService.deleteVuelo(id).subscribe(
      res =>{
        this.listarVuelos();
      }
    )
  }

}