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 { SystemAdminService } from '../../../services/system-admin.service';
import { FunctionsService } from '../../../services/functions.service';
import { MatDialog } from '@angular/material/dialog';
import { lastValueFrom } from 'rxjs';
import { BackupListResponse } from '../../../interfaces/backup-list.interface';
import { OpenLogComponent } from './open-log/open-log.component';

@Component({
    selector: 'app-backups',
    templateUrl: './backups.component.html',
    styleUrl: './backups.component.css',
    standalone: false
})
export class BackupsComponent implements OnInit {
  public btnSmall: boolean = false;
  public txtBuscador:string;
  
  dataSource?: MatTableDataSource<any>;
  isLoading: boolean;
  hasError: boolean;
  errorStr: string;

  @ViewChild(MatPaginator) paginator?: MatPaginator;
  @ViewChild(MatSort) sort?: MatSort;

  displayedColumns = ['ID', 'FECHA', 'ESTADO', 'ACCIONES'];

  constructor(
    private _sysAdminService: SystemAdminService,
    private _funcService: FunctionsService,
    private _dialog: MatDialog,
  ) { 
    this.txtBuscador = '';
    this.isLoading = true;
    this.hasError = false;
    this.errorStr = "";
  }

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

  async getData(){
    try{
      this.isLoading = true;
      this.hasError = false;
      this.errorStr = "";
      
      let idUser = localStorage.getItem('idusuario')!;
      let backupList: BackupListResponse = await lastValueFrom(this._sysAdminService.getBackupList(idUser, 1));

      this.dataSource = new MatTableDataSource(backupList.response);
      this.dataSource.paginator = this.paginator!;
      this.dataSource.sort = this.sort!;

      this.isLoading = false;
    }catch(error: any){
      if(error.error == undefined){
        this.errorStr = 'Ocurrió un error inesperado.';
      }else if(error.error.msg == undefined){
        this.errorStr = 'Ocurrió un error inesperado.';
      }else{
        this.errorStr = error.error.msg;
      }

      this.hasError = true;
      this.isLoading = false;
    }
  }

  public onResize():void {
    this.btnSmall = window.innerWidth <= 1405;
  }

  applyFilter(event: any, type: string){
    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();
    }
  }

  orderDate(date: string): string{
    return this._funcService.orderDate(date)!;
  }

  openLOG(log: string){
    this._dialog.open(OpenLogComponent, {
      width: '380px',
      maxWidth: '380px',
      data: {
        log: log
      }
    });
  }
}
