import { OpenLogComponent } from './open-log/open-log.component';
import { MatDialog } from '@angular/material/dialog';
import { FunctionsService } from './../../../services/functions.service';
import { MatSort } from '@angular/material/sort';
import { MatPaginator } from '@angular/material/paginator';
import { BackupListResponse } from './../../../interfaces/backup-list.interface';
import { SystemAdminService } from 'src/app/services/system-admin.service';
import { EncService } from 'src/app/services/enc/enc.service';
import { lastValueFrom } from 'rxjs';
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';

@Component({
  selector: 'app-backups',
  templateUrl: './backups.component.html',
  styleUrls: ['./backups.component.css']
})
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 _encService: EncService,
    private _sysAdminService: SystemAdminService,
    private _funcService: FunctionsService,
    private _dialog: MatDialog,
  ) { 
    this.txtBuscador = '';
    this.isLoading = true;
    this.hasError = false;
    this.errorStr = "";
  }

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

  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();
    }
  }

  async getData(){
    try{
      this.isLoading = true;
      this.hasError = false;
      this.errorStr = "";
      
      let idUser = localStorage.getItem('idusuario');
      let shortEnc = await lastValueFrom(this._encService.shortEncrypt(idUser!));
      let backupList: BackupListResponse = await lastValueFrom(this._sysAdminService.getBackupList(shortEnc.response.encrypted, 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;
    }
  }

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

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