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

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

  private URL: string = environments.baseUrl;
  private dataUpdated = new Subject<void>();
  dataUpdated$ = this.dataUpdated.asObservable();

  constructor(private http: HttpClient) { }

  private getHeaders(): HttpHeaders {
    const token = localStorage.getItem('token') || '';
    return new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    });
  }

  getRegistros() {
    return this.http.get(`${this.URL}/getRegistrosP`, { headers: this.getHeaders() });
  }
  getMateria() {
    return this.http.get(`${this.URL}/getMateriasP`, { headers: this.getHeaders() });
  }
  getTipos() {
    return this.http.get(`${this.URL}/getTiposP`, { headers: this.getHeaders() });
  }
  getAlum() {
    return this.http.get(`${this.URL}/getAlumnosP`, { headers: this.getHeaders() });
  }
  crearRegistro(data: any) {
    return this.http.post(`${this.URL}/crearBitacoraP`, data, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }
  editarRegistro(data: any) {
    let qId = btoa(data.id);
    return this.http.put(`${this.URL}/editarBitacoraP/${qId}`, data, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }
  eliminarRegistro(id: any) {
    let qId = btoa(id);
    return this.http.put(`${this.URL}/eliminarBitacoraP/${qId}`, null, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }
  habilitarRegistro(id: any) {
    let qId = btoa(id);
    return this.http.put(`${this.URL}/habilitarBitacoraP/${qId}`, null, { headers: this.getHeaders() }).pipe(
      tap(() => this.dataUpdated.next())
    );
  }
}
