import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs';
import { apiUriJean } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class GETBGMService {

  private _url:string; 
  private _token:string;

  constructor(private _http: HttpClient) {
    this._url = `${apiUriJean}/modulo`;
    this._token = `Bearer ${localStorage.getItem('token')}`;
  }

  // Obtener Módulos
  public getModules(){
    return this.getQuery("consulta").pipe(map((data: any) => data))
  }

  // Obtener Módulos Activos
  public getModuleActives(){
    return this.getQuery("consulta-activos").pipe(map((data: any) => data))
  }

  // Obtener Módulos Disponibles
  public getModuleAviable(){
    return this.getQuery("consulta-disponibles").pipe(map((data: any) => data))
  }

  // Registro de Módulos
  public registerModule(data: any){
    return this.postQuery("registra", data).pipe(map((data: any) => data))
  }

  // Modificar Módulos
  public updateModules(data: any) {
    return this.postQuery("modifica", data).pipe(map((data: any) => data))
  }

  // Eliminar Módulos
  public deleteModule(codigo: any){ 
    return this.postQuery("elimina", codigo).pipe(map((data: any) => data))
  }

  private getQuery(query: string) {
    const URL = `${this._url}/${query}/`;
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: this._token,
      }),
    });
  }

  private postQuery(query: string, body: any) {
    const URL = `${this._url}/${query}/`;
    return this._http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: this._token,
      }),
    });
  }

  putQuery(query: string, body: any) {
    const URL = `${this._url}/${query}/`;
    return this._http.put(URL, body, {
      headers: new HttpHeaders({
        Authorization: this._token,
      }),
    });
  }
}
