import { Component, OnInit, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatPaginator } from '@angular/material/paginator';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatTableDataSource } from '@angular/material/table';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { lastValueFrom } from 'rxjs';
import { WorkflowService } from 'src/app/services/samwf/workflow.service';
import { MaterialModule } from '../../material/material.module';
import { WorkflowDetailAdvanceComponent } from './workflow-detail-advance/workflow-detail-advance.component';
@Component({
  selector: 'app-workflow-advance',
  templateUrl: './workflow-advance.component.html',
  styleUrls: ['./workflow-advance.component.css'],
})
export class WorkflowAdvanceComponent implements OnInit, MaterialModule {
  data_empty = false;

  displayedColumns: string[] = ['name', 'module', 'created_at', 'action'];
  advance: any[] = [];

  dataSource = new MatTableDataSource<any>(this.advance);
  @ViewChild(MatPaginator) paginator: MatPaginator;

  constructor(
    private routerNavigate: ActivatedRoute,
    private router: Router,
    public dialog: MatDialog,
    private translate: TranslateService,
    private _samwfService: WorkflowService,
    private _snackBar: MatSnackBar
  ) {
    translate.setDefaultLang('es');
  }

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

  applyFilter(filterValue: any) {
    this.dataSource.filter = filterValue.target.value.trim().toLowerCase();
  }

  backToWorkflows() {
    this.router.navigate(['workflow']);
  }

  updatePaginator() {
    this.translate.get('paginador').subscribe((data: any) => {
      this.dataSource.paginator = this.paginator;
      this.paginator._intl.itemsPerPageLabel = data[0];
      this.paginator._intl.firstPageLabel = data[1];
      this.paginator._intl.lastPageLabel = data[2];
      this.paginator._intl.nextPageLabel = data[3];
      this.paginator._intl.previousPageLabel = data[4];
    });
  }

  ngAfterViewInit(): void {
    this.translate.get('paginador').subscribe((data: any) => {
      this.dataSource.paginator = this.paginator;
      this.paginator._intl.itemsPerPageLabel = data[0];
      this.paginator._intl.firstPageLabel = data[1];
      this.paginator._intl.lastPageLabel = data[2];
      this.paginator._intl.nextPageLabel = data[3];
      this.paginator._intl.previousPageLabel = data[4];
    });

    this.dataSource.filterPredicate = function (data, filter: string): boolean {
      return (
        data.name.toString().toLowerCase().includes(filter) ||
        data.description.toString().toLowerCase().includes(filter) ||
        data.created_at.toString().includes(filter)
      );
    };
  }

  async openAdavanceRequest(request: any) {
    const dialogRef = this.dialog.open(WorkflowDetailAdvanceComponent, {
      data: request,
    });
  }

  private async getRequestUser() {
    await lastValueFrom(this._samwfService.getRequestsAdvanceByUser(1)).then(
      (data: any) => {
        this.advance = data;
        this.dataSource.data = data.filter((e: any) => {
          e.created_at = e.created_at.slice(0, 10);
          return e;
        });

        this.dataSource.data.length > 0
          ? (this.data_empty = false)
          : (this.data_empty = true);
      },
      (error: any) => {
        this._snackBar.open(error.message, 'Cerrar', { duration: 3000 });
      }
    );
  }
}
