import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { ContractsBySubcontratists, ContractsBySubcontratistsResponse } from '../../../../interfaces/personal-managment/subcontratists.interface';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { ResourcesService } from '../../../../services/resources.service';
import { EmployeeService } from '../../../../services/personal-management/employee.service';
import { MatDialog } from '@angular/material/dialog';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';
import { ContractsHistoryEmployeeComponent } from '../contracts-history-employee/contracts-history-employee.component';

@Component({
    selector: 'app-contracts-management-employee',
    templateUrl: './contracts-management-employee.component.html',
    styleUrl: './contracts-management-employee.component.css',
    standalone: false
})
export class ContractsManagementEmployeeComponent implements OnInit, AfterViewInit {
  public dataSource: MatTableDataSource<ContractsBySubcontratists>;
  public displayedColumns: string[];
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;

  public isLoading: boolean;
  public hasError: boolean;
  public errorStr: string;

  constructor (
    public resourcesService: ResourcesService,
    private _employeeService: EmployeeService,
    private _dialog: MatDialog,
  ) {
    this.dataSource = new MatTableDataSource<ContractsBySubcontratists>();
    this.displayedColumns = [
      'EMPLOYEE',
      'CONTRACTS',
      'ACCIONES'
    ];
    this.isLoading = false;

    this.hasError = false;
    this.errorStr = "";
  }
  
  ngAfterViewInit(): void {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  async ngOnInit() {
    await this.getContractsByEmployees()
  }

  public async getContractsByEmployees() {
    this.isLoading = true;
    const USER: string = this.resourcesService.getUser();
    const LINE: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._employeeService.getContractsOfEveryEmployee(USER, LINE)).then(
      (responseData: ContractsBySubcontratistsResponse) => {
        this.hasError = responseData.error;
        this.errorStr = responseData.msg;
        if (!responseData.error) {
          this.dataSource.data = responseData.response;
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this.resourcesService.checkErrors(httpErrorResponse)
        if (response !== null && response.reload) {
          await this.getContractsByEmployees();
        }
      }
    );
    this.isLoading = false;
  }

  public async getHistoryContracts (contractsBySubcontratists: ContractsBySubcontratists) {
    this._dialog.open(ContractsHistoryEmployeeComponent, {
      width: '700px',
      maxWidth: '700px',
      data: contractsBySubcontratists
    });
  }
}
