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

@Component({
  selector: 'app-budget-management',
  templateUrl: './budget-management.component.html',
  styleUrls: ['./budget-management.component.css']
})
export class BudgetManagementComponent 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;
    await lastValueFrom(this._invoiceService.getBudgetManagement()).then(
      (responseData: ResponseDataInvoice) => {
        if (!responseData.error) {
          this.dataSource.data = responseData.response;
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, (httpErrorResponse: HttpErrorResponse) => this.resourcesService.checkErrors(httpErrorResponse)
    )
    this.isLoading = false;
  }

  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 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 async downloadInvoice(getInvoice:GetInvoice) {
    this.isLoadingForm = true;
    const encCodeDocument:string = getInvoice.FALS_CODO;
    let codeDocument: string = await this._encService.decrypt(encCodeDocument);
    
    const idUser:string = this.resourcesService.getUser();
    const line:number = parseInt(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}`);
        }
      }, (httpErrorResponse: HttpErrorResponse) => this.resourcesService.checkErrors(httpErrorResponse)
    );
    this.isLoadingForm = false;
  }
}
