import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { GetLocationStock, ResponseDataGetLocationStock } from '../../../interfaces/inventory-management/location-stock.interface';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { ResourcesService } from '../../../services/resources.service';
import { InventoryManagementService } from '../../../services/inventory-management/inventory-management.service';
import { Router } from '@angular/router';
import { EncService } from '../../../services/enc.service';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
    selector: 'app-location-stock',
    templateUrl: './location-stock.component.html',
    styleUrl: './location-stock.component.css',
    standalone: false
})
export class LocationStockComponent implements OnInit, AfterViewInit {
  public isLoading: boolean;
  public isLoadingForm: boolean;
  public dataSource: MatTableDataSource<GetLocationStock>;
  public displayedColumns: Array<string>; 
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;

  constructor(
    public resourcesService: ResourcesService,
    private _inventoryManagementService: InventoryManagementService,
    private _router: Router,
    private _encService: EncService,
  ) {
    this.isLoading = false;
    this.isLoadingForm = false;
    this.dataSource = new MatTableDataSource<GetLocationStock>();
    this.displayedColumns = [
      'ID_STOCK',
      'ID_UBICACION',
      'CODIGO_EQUIPAMIENTO',
      'USUARIO_MODIFICA',
      'FECHA_MODIFICA',
      'ESTADO',
      'ACCIONES',
    ];
  }
  
  async ngOnInit() {
    this.getLocationStock();
  }
  
  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }
  
  public async getLocationStock() {
    this.isLoading = true;
    const user: string = this.resourcesService.getUser();
    const line: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._inventoryManagementService.getLocationStock(user, line)).then(
      (responseData: ResponseDataGetLocationStock) => {
        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.getLocationStock();
        }
      }
    );
    this.isLoading = false;
  }

  public async openHistoryLocationStock(idStock: string) {
    idStock = idStock.replace('#', '');
    idStock = idStock.trim();
    const encStock = await this._encService.encrypt(idStock);
    this._router.navigate([`/sam/GIST/GEST/history/${encStock}`]);
  }
}
