import { Component, Inject, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { InformationsData, ResponseDataInformationProvider } from 'src/app/interfaces/acquisition-management/provider.interface';
import { FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { lastValueFrom } from 'rxjs';
import { OrderService } from 'src/app/services/acquisition-management/order.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';
import { ProviderService } from 'src/app/services/acquisition-management/provider.service';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
  selector: 'app-information-provider',
  templateUrl: './information-provider.component.html',
  styleUrls: ['./information-provider.component.css']
})
export class InformationProviderComponent implements OnInit {

  public formGroupInformation: FormGroup;
  private providerInformation: InformationsData;

  public isLoading: boolean;
  constructor(
    @Inject(MAT_DIALOG_DATA) public providerNumber: string,
    private _providerService: ProviderService,
    public resourcesService: ResourcesService,
  ) {
    this.formGroupInformation = this.createFormGroupInformation();
    this.providerInformation = {} as InformationsData;
    this.isLoading = false;
  }

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

  private createFormGroupInformation(): FormGroup {
    return new FormGroup({
      'NOMBRE_COMERCIAL': new FormControl(''),
      'CORREO': new FormControl(''),
      'NOMBRE': new FormControl(''),
      'APELLIDO_PATERNO': new FormControl(''),
      'APELLIDO_MATERNO': new FormControl(''),
      'LADA1': new FormControl(''),
      'TELEFONO1': new FormControl(''),
      'LADA2': new FormControl(''),
      'TELEFONO2': new FormControl(''),
      'GIRO_EMPRESA': new FormControl(''),
      'TIPO_EMPRESA': new FormControl(''),
      'RFC': new FormControl(''),
      'TAXID': new FormControl(''),
      'SITIO_WEB': new FormControl(''),
    });
  }

  private async getProviderByNumber() {
    this.isLoading = true;
    await lastValueFrom(this._providerService.getProviderByNumber(this.providerNumber)).then(
      (responseData: ResponseDataInformationProvider) => {
        if ( !responseData.error ) {
          this.providerInformation = responseData.response;

          this.NOMBRE_COMERCIAL.setValue(this.providerInformation.RAZON_SOCIAL);
          this.CORREO.setValue(this.providerInformation.CORREO);
          this.NOMBRE.setValue(this.providerInformation.NOMBRE);
          this.APELLIDO_PATERNO.setValue(this.providerInformation.PRIMER_APELLIDO);
          this.APELLIDO_MATERNO.setValue(this.providerInformation.SEGUNDO_APELLIDO);
          this.LADA1.setValue(this.providerInformation.LADA1);
          this.TELEFONO1.setValue(this.providerInformation.TELEFONO1);
          this.LADA2.setValue(this.providerInformation.LADA2);
          this.TELEFONO2.setValue(this.providerInformation.TELEFONO2);
          this.GIRO_EMPRESA.setValue(this.providerInformation.GIRO_EMPRESA);
          this.TIPO_EMPRESA.setValue(this.providerInformation.TIPO_EMPRESA);
          this.RFC.setValue(this.providerInformation.RFC);
          this.TAXID.setValue(this.providerInformation.TAXID);
          this.SITIO_WEB.setValue(this.providerInformation.SITIO_WEB);

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

  get NOMBRE_COMERCIAL () {return this.formGroupInformation.get('NOMBRE_COMERCIAL')!;}
  get CORREO () {return this.formGroupInformation.get('CORREO')!;}
  get NOMBRE () {return this.formGroupInformation.get('NOMBRE')!;}
  get APELLIDO_PATERNO () {return this.formGroupInformation.get('APELLIDO_PATERNO')!;}
  get APELLIDO_MATERNO () {return this.formGroupInformation.get('APELLIDO_MATERNO')!;}
  get LADA1 () {return this.formGroupInformation.get('LADA1')!;}
  get TELEFONO1 () {return this.formGroupInformation.get('TELEFONO1')!;}
  get LADA2 () {return this.formGroupInformation.get('LADA2')!;}
  get TELEFONO2 () {return this.formGroupInformation.get('TELEFONO2')!;}
  get GIRO_EMPRESA () {return this.formGroupInformation.get('GIRO_EMPRESA')!;}
  get TIPO_EMPRESA () {return this.formGroupInformation.get('TIPO_EMPRESA')!;}
  get RFC () {return this.formGroupInformation.get('RFC')!;}
  get TAXID () {return this.formGroupInformation.get('TAXID')!;}
  get SITIO_WEB () {return this.formGroupInformation.get('SITIO_WEB')!;}

}
