import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environments } from '../../../../environments/environments';
import { Observable, Subject } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class RegistroAcademicoService {
  private URL: string = environments.baseUrl;
  private dataUpdated = new Subject<void>();
  dataUpdated$ = this.dataUpdated.asObservable();
  constructor(private http: HttpClient) { }

private getHeaders(isFormData: boolean = false): HttpHeaders {
    const token = localStorage.getItem('token') || '';
    let headers = new HttpHeaders({
        'Authorization': `Bearer ${token}`
    });

    if (!isFormData) {
        headers = headers.set('Content-Type', 'application/json');
    }

    return headers;
}

  createRegistroAcademico(data: any) {
    return this.http.post(`${this.URL}/registroAca`, data, { headers: this.getHeaders() }).pipe(tap(() => this.dataUpdated.next()));
  }
  getOneRegistroAcademico(id: string) {
    let qId = btoa(id);
    return this.http.get(`${this.URL}/getOneRegistro/${qId}`, { headers: this.getHeaders() });
  }

  getAll() {;
    return this.http.get(`${this.URL}/getAll`, { headers: this.getHeaders() });
  }
  getAllAd() {;
    return this.http.get(`${this.URL}/getAllAd`, { headers: this.getHeaders() });
  }

  ActualizarRegistroAcademico(data: any) {
    let id = btoa(data.idAlumno);
    return this.http.put(`${this.URL}/updateRegistro/${id}`, data, { headers: this.getHeaders() }).pipe(tap(() => this.dataUpdated.next()));
  }

crearRegistroAdministrativo(formData: FormData): Observable<any> {
  return this.http.post(`${this.URL}/registroAdministrativo`, formData, {
    headers: this.getHeaders(true)
  }).pipe(tap(() => this.dataUpdated.next()));
}

actualizarRegistroAdministrativo(formData: FormData, idAlumno: string): Observable<any> {
  const encodedId = btoa(idAlumno);
  return this.http.post(`${this.URL}/editarRegistroAdmin/${encodedId}`, formData, {
    headers: new HttpHeaders({
      'Authorization': `Bearer ${localStorage.getItem('token')}`
    })
  });
}


eliminarArchivo(rutaArchivo: string): Observable<any> {
  return this.http.post(`${this.URL}/eliminarArchivo`, { rutaArchivo }, {
    headers: this.getHeaders()
  }).pipe(
    tap(() => this.dataUpdated.next())
  );
}




  getOneRegistroAdmin(id: string) {
    let qId = btoa(id);
    return this.http.get(`${this.URL}/getRegistroAdmin/${qId}`, { headers: this.getHeaders() }).pipe(tap(() => this.dataUpdated.next()));
  }



  getPlanPago(){
    return this.http.get(`${this.URL}/getAllPlanPago`, { headers: this.getHeaders() });
  }

  getMetodoPago(){
    return this.http.get(`${this.URL}/getAllMetodoPago`, { headers: this.getHeaders() });
  }

  getPorcentajeBeca(){
    return this.http.get(`${this.URL}/getAllPorcentajeBeca`, { headers: this.getHeaders() });
  }

  getOne(id: string) {
    let qId = btoa(id);
    return this.http.get(`${this.URL}/UserOne/${qId}`, { headers: this.getHeaders() });
  }




}
