import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';

const DATA_EXAMPLE: Array<any> = [
  {
    NUMERO_SOLICITUD: '0000000001',
    NUMERO_PEDIDO: '0000000001',
    PROVEEDOR: 'Jean Jairo Benitez Meza',
    FECHA_REGISTRA: '2023-01-19 13:12:41',
    FECHA_MODIFICA: '2023-01-19 13:12:41',
    ESTADO: 'Activo',
  }
];



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

  public displayedColumns: Array<string>;
  public dataSource: MatTableDataSource<any>;
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;
  public txtBuscador:string;

  public isLoading: boolean;
  public isLoadingForm: boolean;
  public btnSmall: boolean;

  constructor() {
    this.displayedColumns = [ 'NUMERO_SOLICITUD', 'NUMERO_PEDIDO', 'PROVEEDOR', 'FECHA_REGISTRA', 'ESTADO', 'ACCIONES' ];
    this.dataSource = new MatTableDataSource(DATA_EXAMPLE);
    this.isLoading = false;
    this.isLoadingForm = false;
    this.btnSmall = false;
    this.txtBuscador = '';
  }

  ngOnInit(): void {
    this.onResize();
  }

  ngAfterViewInit(): void {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  // Método de filtrado de datos
  public applyFilter(event: any, type: string):void {
    let filterValue:string;
    if (type == 'INP') {
      filterValue = (event.target as HTMLInputElement).value;
    }else{
      this.txtBuscador = event;
      filterValue = event;
    }
    this.dataSource.filter = filterValue.trim().toLowerCase();
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

  // Método para reajustar los botones según sus dimensiones
  public onResize():void {
    this.btnSmall = window.innerWidth <= 1405;
  }

  // Método para dar formato a las fechas y horas
  public formatDate(dateRegister:string, dateUpdate:string | null):string {
    let date:string = (dateUpdate === null) ? dateRegister : dateUpdate;
    let arrDateTime:Array<string> = date.split(' ');
    let arrDate:Array<string> = arrDateTime[0].split('-');
    return `${arrDate[2]}-${arrDate[1]}-${arrDate[0]} ${arrDateTime[1]}`;
  }

}
