import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { ContractsByEmployee, ContractsBySubcontratist, ContractsBySubcontratistResponse } from '../../../../interfaces/personal-managment/subcontratists.interface';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { EmployeeService } from '../../../../services/personal-management/employee.service';
import { ResourcesService } from '../../../../services/resources.service';
import { ActivatedRoute } from '@angular/router';
import { EncService } from '../../../../services/enc.service';
import { FunctionsService } from '../../../../services/functions.service';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
    selector: 'app-contracts-history-employee',
    templateUrl: './contracts-history-employee.component.html',
    styleUrl: './contracts-history-employee.component.css',
    standalone: false
})
export class ContractsHistoryEmployeeComponent implements OnInit {
  public isLoading: boolean;
  public hasError: boolean;
  public errorStr: string;
  public employee: ContractsByEmployee;

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

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: ContractsByEmployee,
    private _employeeService: EmployeeService,
    public _resourcesService: ResourcesService,
    private _activatedRoute: ActivatedRoute,
    private _encService: EncService,
    private _functionsService: FunctionsService,
  ) {
    this.isLoading = true;
    this.hasError = false;
    this.errorStr = "";
    this.employee = this._data;

    this.idEmployee = "";
    this.txtBuscador = '';
    this.displayedColumns = [
      'CONTRACT_NAME',
      'START_DATE',
      'END_DATE',
      'CONTRACT_COST']
    this.dataSource = new MatTableDataSource<any>();
  }

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

  public async getData() {
    this.isLoading = true;
    const USER: string = this._resourcesService.getUser();
    const LINE: number = this._resourcesService.getLineNumber();
    console.log(this.employee);
    

    await lastValueFrom(this._employeeService.getDetailsOfContractsByEmployee( this.employee.ID_EMPLOYEE, USER, LINE )).then(
      (responseData: ContractsBySubcontratistResponse) => {
        this.hasError = responseData.error;
        if (!responseData.error) {

          let contractsArr: ContractsBySubcontratist[] = [];
          for(const contract of responseData.response){
            contract.START_DATE = this._functionsService.orderDate(contract.START_DATE)!;
            contract.END_DATE = this._functionsService.orderDate(contract.END_DATE)!;
            contractsArr.push(contract);
          }
  
          this.dataSource.data = contractsArr;
        } else {
          this.errorStr = responseData.msg;
          this._resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this._resourcesService.checkErrors(httpErrorResponse)
        if (response !== null && response.reload) {
          await this.getData();
        }
      }
    );
    this.isLoading = false;
  }
}
