import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { GetInvoice, ResponseDataInvoice } from '../../../../interfaces/acquisition-management/invoice.interface';
import { ResourcesService } from '../../../../services/resources.service';
import { InvoiceService } from '../../../../services/acquisition-management/invoice.service';
import { EncService } from '../../../../services/enc.service';
import { GdelService } from '../../../../services/gdel.service';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';
import { ResponseDataGetPublicDocumentUrl } from '../../../../interfaces/gdel.interface';

@Component({
    selector: 'app-budget',
    templateUrl: './budget.component.html',
    styleUrl: './budget.component.css',
    standalone: false
})
export class BudgetComponent implements OnInit {
  public dataSource: MatTableDataSource<GetInvoice>;
  public displayedColumns: Array<string>;

  public isLoading: boolean;
  public isLoadingForm: boolean;
  
  constructor(
    public resourcesService: ResourcesService,
    private _invoiceService: InvoiceService,
    private _encService: EncService,
    private _gdelService: GdelService,
  ) {
    this.dataSource = new MatTableDataSource();
    this.displayedColumns = ['FALS_IDFA','LINE_OTPR','FALS_IDLI','LINE_NUPR','FALS_USMO','FALS_FEMO','FALS_ESTA','ACCIONES'];
    this.isLoading = false;
    this.isLoadingForm = false;
  }
  
  async ngOnInit() {
    this.isLoading = true;
    await this.getBudgetManagement();
    this.isLoading = false;
  }

  public async getBudgetManagement() {
    this.isLoading = true;
    const user: string = this.resourcesService.getUser();
    const line: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._invoiceService.getBudgetManagement(user, line)).then(
      (responseData: ResponseDataInvoice) => {
        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.getBudgetManagement();
        }
      }
    )
    this.isLoading = false;
  }

  public selectedTooltip(getInvoice: GetInvoice) {
    let msg: string = '';    
    if (getInvoice.LINE_OTPR !== null) {
      msg = `Orden de Mantenimiento Preventido #${getInvoice.LINE_OTPR}`;
    } else if (getInvoice.LINE_OTCO !== null) {
      msg = `Orden de Mantenimiento Correctivo #${getInvoice.LINE_OTCO}`;
    } else {
      msg = `NA`;
    }
    return msg;
  }

  public selectedOrder(getInvoice: GetInvoice) : string {
    let order: string = '';
    if (getInvoice.LINE_OTPR !== null) {
      order = `OTPR: #${getInvoice.LINE_OTPR}`;
    } else if (getInvoice.LINE_OTCO !== null) {
      order = `OTCO: #${getInvoice.LINE_OTCO}`;
    } else {
      order = `NA`;
    }
    return order;
  }

  public async downloadInvoice(getInvoice: GetInvoice) {
    this.isLoadingForm = true;
    const encCodeDocument:string = getInvoice.FALS_CODO;
    
    const idUser:string = this.resourcesService.getUser();
    const line:number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._gdelService.getPublicDocumentUrl(encCodeDocument, idUser, line)).then(
      (responseData: ResponseDataGetPublicDocumentUrl) => {
        if (!responseData.error) {
          window.open(responseData.response.public_uri, '_blank');
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this.resourcesService.checkErrors(httpErrorResponse)
        if (response !== null && response.reload) {
          await this.downloadInvoice(getInvoice);
        }
      }
    );
    this.isLoadingForm = false;
  }
}
