import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute, Router } from '@angular/router';
import { lastValueFrom } from 'rxjs';
import { ResponseDataEmployeeInterventions } from 'src/app/interfaces/personal-managment/employee.interface';
import { EncService } from 'src/app/services/enc/enc.service';
import { EmployeeService } from 'src/app/services/personal-management/employee.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';

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

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

  constructor(private _employeeService: EmployeeService,
    public _resourcesService: ResourcesService,
    private _activatedRoute: ActivatedRoute,
    private _router: Router,
    private _encService: EncService) {
    this.idEmployee = "";
    this.isLoading = false;
    this.txtBuscador = '';
    this.displayedColumns = [
      'WORKTEAM_NAME',
      'INTERVENTION_NAME',
      'INTERVENTION_DESCRIPTION',
      'ACCIONES']
    this.dataSource = new MatTableDataSource<any>();
  }

  ngOnInit(): void {
    this.idEmployee = this._activatedRoute.snapshot.paramMap.get('id')!;
    this.decript();
  }

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

  private async decript() {
    this.idEmployee = await this._encService.desencriptar(this.idEmployee);
    this.getData();
  }

  public async openDetails(element: any){
    this._resourcesService.openSnackBar('Función para la vista de detalles por intervención');
    // this._router.navigate(['sam/GPRS/employee/form/' + element.INTERVENTION_ID + '/1']);
  }

  public async getData() {
    this.isLoading = true;
    await lastValueFrom(this._employeeService.getInterventionsByEmployee(this.idEmployee)).then(
      (responseData: ResponseDataEmployeeInterventions) => {
        this.dataSource.data = responseData.response
      }, (error: HttpErrorResponse) => this._resourcesService.checkErrors(error)
    );
    this.isLoading = false;
  }

  // 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;
    }
    console.log(filterValue);
    this.dataSource.filter = filterValue.trim().toLowerCase();
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

}
