import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { FilterNotificate, FilterRequest } from '../../../interfaces/process-managementv/workflow-management.interface';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { ResourcesService } from '../../../services/resources.service';
import { InternationalizationService } from '../../../services/internationalization/internationalization.service';
import { MatDialog } from '@angular/material/dialog';
import { EncService } from '../../../services/enc.service';
import { Router } from '@angular/router';
import { SearchRequestComponent } from './search-request/search-request.component';
import { SearchNotificateComponent } from './search-notificate/search-notificate.component';

@Component({
    selector: 'app-multicriteria-searches',
    templateUrl: './multicriteria-searches.component.html',
    styleUrl: './multicriteria-searches.component.css',
    standalone: false
})
export class MulticriteriaSearchesComponent implements AfterViewInit {
  public isLoadingRequest: boolean;
  public isLoadingNotificate: boolean;
  public isLoadingForm: boolean;

  public dataSourceRequest: MatTableDataSource<FilterRequest>;
  public displayedColumnsRequest: Array<string>;
  @ViewChild('paginatorRequest') paginatorRequest!: MatPaginator;
  @ViewChild(MatSort) sortRequest!: MatSort;
  
  public dataSourceNotificate: MatTableDataSource<any>;
  public displayedColumnsNotificate: Array<string>;
  @ViewChild('paginatorNotificate') paginatorNotificate!: MatPaginator;
  @ViewChild(MatSort) sortNotificate!: MatSort;

  constructor(
    public resourcesService: ResourcesService,
    public interService: InternationalizationService,
    private _dialog: MatDialog,
    private _encService: EncService,
    private _router: Router,
  ) {
    this.isLoadingRequest = false;
    this.isLoadingNotificate = false;
    this.isLoadingForm = false;

    this.dataSourceRequest = new MatTableDataSource<FilterRequest>();
    this.displayedColumnsRequest = [
      'NOMBRE_WORKFLOW',
      'ID_EJECUCION_WORKFLOW',
      'ESTADO_EJECUCION_WORKFLOW',
      'NOMBRE_TAREA',
      'ID_EJECUCION_TAREA',
      'ESTADO_EJECUCION_TAREA',
      'NOMBRE_PROCESO',
      'USUARIO_MODIFICA',
      'FECHA_MODIFICA',
      'ACCIONES',
    ];
    
    this.dataSourceNotificate = new MatTableDataSource<any>();
    this.displayedColumnsNotificate = [
      'ID_NOTIFICACION',
      'ID_EJECUCION_TAREA',
      'USUARIO_NOTIFICACION',
      'TIPO_NOTIFICACION',
      'ESTADO',
      'USUARIO_MODIFICA',
      'FECHA_MODIFICA',
      'ACCIONES'
    ];
  }
  
  ngAfterViewInit(): void {
    this.dataSourceRequest.paginator = this.paginatorRequest;
    this.dataSourceRequest.sort = this.sortRequest;
    
    this.dataSourceNotificate.paginator = this.paginatorNotificate;
    this.dataSourceNotificate.sort = this.sortNotificate;
  }

  public openSearchRequest() {
    this._dialog.open(SearchRequestComponent, {
      disableClose: true,
      data: [],
      width: '850px',
      maxWidth: '850px',
    }).afterClosed().subscribe((result: false | FilterRequest[]) => {
      if (result !== false) {
        this.isLoadingRequest = true;
        this.dataSourceRequest.data = result;
        this.isLoadingRequest = false;
      }
    });
  }

  public async viewDetailsRequest(filterRequest: FilterRequest) {
    const ID_EXECUTE_TASK: string = filterRequest.ID_EJECUCION_TAREA.toString();
    const ENC_EXECUTE_TASK: string = await this._encService.encrypt(ID_EXECUTE_TASK);
    this._router.navigate([`/sam/GEPR/GFTR/BUMU/details-request/${ENC_EXECUTE_TASK}`])
  }
  
  public openSearchNotificate() {
    this._dialog.open(SearchNotificateComponent, {
      disableClose: true,
      data: [],
      width: '500px',
      maxWidth: '500px',
    }).afterClosed().subscribe((result: false | FilterNotificate[]) => {
      if (result) {
        this.isLoadingNotificate = true;
        this.dataSourceNotificate.data = result;
        this.isLoadingNotificate = false;
      }
    });
  }

  public async viewDetailsNotificate(filterNotificate: FilterNotificate) {
    const ID_NOTIFICATE: string = filterNotificate.ID_NOTIFICACION.toString();
    const ENC_NOTIFICATE: string = await this._encService.encrypt(ID_NOTIFICATE);
    this._router.navigate([`/sam/GEPR/GFTR/BUMU/details-notificate/${ENC_NOTIFICATE}`])
  }
}
