import { AfterViewInit, Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Router } from '@angular/router';
import { Pasajero } from '../../interfaces/pasajeros.inteface';
import Swal from 'sweetalert2'
import { PageEvent } from '@angular/material/paginator';
import { PasajerosService } from '../../services/pasajeros.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', 'apellido', 'primer nombre', 'segundo nombre', 'telefono','opciones'];
  dataSource!: MatTableDataSource<any>;
  @ViewChild(MatSort) sort!: MatSort;
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild('input') entradaFiltro: ElementRef | undefined;  
  filtroInput: string = '';

  pasajeros: Pasajero[] = [];

  constructor(
    private router: Router,
    private pasajerosService: PasajerosService
  ){}

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

  listaPasajeros(){
    this.pasajerosService.getPasajeros().subscribe(
      res =>{
        this.pasajeros = res;
        this.dataSource = new MatTableDataSource(this.pasajeros);
        this.dataSource.paginator = this.paginator;  
        this.dataSource.sort = this.sort;
      },err => {

      }
    )
  }

  eliminar(event:any, id: number){
    event.stopPropagation();
    Swal.fire({
      title: 'Estas seguro?' + ' Pasajero 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.eliminarPasajero(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(['/pasajeros/detalles/',id ]);
  }

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

  eliminarPasajero(id: number){
    this.pasajerosService.deletePasajero(id).subscribe(
      res =>{
        this.listaPasajeros();
      },err => {

      }
    )
  }

  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();
    }
  }
}
