import { HttpErrorResponse } from '@angular/common/http';
import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { lastValueFrom } from 'rxjs';
import { GetArtitleByInformation, ResponseDataGetArtitleByInformation } from 'src/app/interfaces/acquisition-management/artitle.interface';
import { ArtitleService } from 'src/app/services/acquisition-management/artitle.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';

@Component({
  selector: 'app-view-details-line',
  templateUrl: './view-details-line.component.html',
  styleUrls: ['./view-details-line.component.css']
})
export class ViewDetailsLineComponent implements OnInit {

  public isLoading: boolean;
  public objArtitles: GetArtitleByInformation;

  constructor(
    @Inject(MAT_DIALOG_DATA) public idInformation: string,
    private _artitleService: ArtitleService,
    public resourceService: ResourcesService,
  ) {
    this.objArtitles = {} as GetArtitleByInformation;
    this.isLoading = false;
  }

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

  private async getArtitleByInformation() {
    this.isLoading = true;
    await lastValueFrom(this._artitleService.getArtitleByInformation(this.idInformation)).then(
      (responseData: ResponseDataGetArtitleByInformation) => {
        console.log(responseData);

        if ( !responseData.error ) {
          this.objArtitles = responseData.response;
          console.log(this.objArtitles);

        } else {
          this.resourceService.openSnackBar( responseData.msg );
        }
      }, (error: HttpErrorResponse) => this.resourceService.checkErrors(error)
    );
    this.isLoading = false;
  }

}
