import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute } from '@angular/router';
import { lastValueFrom } from 'rxjs';;
import { ResponseDataEmployeeContracts } 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-contracts-history-employee',
  templateUrl: './contracts-history-employee.component.html',
  styleUrls: ['./contracts-history-employee.component.css']
})
export class ContractsHistoryEmployeeComponent 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 _encService: EncService) {
    this.idEmployee = "";
    this.isLoading = false;
    this.txtBuscador = '';
    this.displayedColumns = [
      'CONTRACT_NAME',
      'START_DATE',
      'END_DATE',
      'CONTRACT_COST']
    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 getData() {
    this.isLoading = true;
    await lastValueFrom(this._employeeService.getDetailsOfContractsByEmployee(this.idEmployee)).then(
      (responseData: ResponseDataEmployeeContracts) => {
        responseData.response.forEach(async element => {
          element.CONTRACT_COST = await this._encService.desencriptar(element.CONTRACT_COST);
        });
        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;
    }
    this.dataSource.filter = filterValue.trim().toLowerCase();
    if (this.dataSource.paginator) {
      this.dataSource.paginator.firstPage();
    }
  }

}
