import { Injectable } from '@angular/core';
import { map, Observable } from 'rxjs';
import { HttpRequestService } from '../resources/http-request.service';

@Injectable({
  providedIn: 'root',
})
export class DynamicFormService {
  private _url: string;

  constructor( private _httpRequestService: HttpRequestService ) {
    this._url = 'form';
  }

  // Obtener Formularios
  public getForm(): Observable<ResponseData> {
    return this._httpRequestService.getQuery(`${this._url}/consulta`).pipe(map((data:any) => data));
    // return this.getQuery('consulta').pipe(map((data: any) => data));
  }

  // Creación de Formulaios
  public createForm(json: any) {
    return this._httpRequestService.postQuery(`${this._url}/registra`, json).pipe(map((data:any) => data));
    // return this.postQuery('registra', json).pipe(map((data: any) => data));
  }

  // Modificación de Formulario
  public updateForm(response: any, idForm: any) {
    return this._httpRequestService.putQuery(`${this._url}/modifica/${idForm}`, response).pipe(map((data:any) => data));
    // return this.putQuery(`modifica/${idForm}`, response).pipe(map((data: any) => data));
  }

  // Eliminar Formulario
  public deleteForm(codigo_form: any) {
    return this._httpRequestService.postQuery(`${this._url}/elimina`, codigo_form).pipe(map((data:any) => data));
    // return this.postQuery('elimina', codigo_form).pipe(map((data: any) => data));
  }

  // Obtener Formulario Único
  public getFormData(codigo_form: number): Observable<ResponseData> {
    return this._httpRequestService.getQuery(`${this._url}/consulta-data/${codigo_form}`).pipe(map((data:any) => data));
    // return this.getQuery(`consulta-data/${codigo_form}`).pipe(map((data: any) => data));
  }

  // Obtener Datos para un Select
  public getDataSelect(data: any): Observable<ResponseData> {
    return this._httpRequestService.postQuery(`${this._url}/consulta-select`, data).pipe(map((data:any) => data));
    // return this.postQuery('consulta-select', data).pipe(map((data: any) => data));
  }


}

export interface FormResponse {
  NOMBRE_FORM: string;
  MODULO: string;
  SUBMODULO: string;
}

export interface ResponseData {
  error: boolean;
  msg: string;
  response: Array<FormResponse>;
}
