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';
import { PeriodoBoleta, TipoInformacion, TipoRegistroBitacora, TipoTareas } from '../pages/modulos/tiposTareas/tipos-tareas.component';

@Injectable({
  providedIn: 'root'
})
export class TipoTareasService {
  private baseUrl: string = environments.baseUrl;
  private dataUpdated = new Subject<void>();
  dataUpdated$ = this.dataUpdated.asObservable();
  constructor(private http: HttpClient) { 
  }
  private token = localStorage.getItem('token') || '';
  private reqHeader = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${this.token}`
  });
//Tipos de tareas
  getTipoTareas(){
    return this.http.get(`${this.baseUrl}/tipoTarea`, {headers: this.reqHeader});
  }

 crearTipoTarea(Tarea: TipoTareas){
   return this.http.post(`${this.baseUrl}/tipoTareas`, Tarea, {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));
  }

  actualizarTipoTarea(Tarea: TipoTareas){
    let clave= btoa(Tarea.clave);
    return this.http.put(`${this.baseUrl}/tipoTareas/${clave}`, Tarea , {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));
  }
  
  eliminarEstadoTT(id:any){
    id= btoa(id);
    return this.http.put(`${this.baseUrl}/eliminar/${id}`,id , {headers: this.reqHeader});
  }

  habilitarEstadoTT(id:any){
    id=  btoa(id);
    return this.http.put(`${this.baseUrl}/habilitar/${id}`, id, {headers: this.reqHeader});
  }

  eliminarTipoTarea(id: any){
    id= btoa(id);
    return this.http.delete(`${this.baseUrl}/tiposTareas/${id}`, {headers: this.reqHeader});
  }

  //Tipos de registro en bitacora
  getTipoBitacora(){
    return this.http.get(`${this.baseUrl}/registroBitacoraT`, {headers: this.reqHeader});
  }

 crearTipoBitacora(formData: TipoRegistroBitacora){
   return this.http.post(`${this.baseUrl}/registroBitacora`, formData, {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));
  }

  actualizarTipoBitacora(bitacora: TipoRegistroBitacora){
    let id = btoa(bitacora.clave);
    return this.http.put(`${this.baseUrl}/registroBitacora/${id}`, bitacora, {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));
  }
  
  eliminarEstadoTB(id:any){
    id= btoa(id);
    return this.http.put(`${this.baseUrl}/eliminarEstadoRB/${id}`,id , {headers: this.reqHeader});
  }
  
  habilitarEstadoTB(id:any){
    id=  btoa(id);
    return this.http.put(`${this.baseUrl}/habilitarEstadoRB/${id}`, id, {headers: this.reqHeader});
  }

  // Tipos de informacion
  getTipoInfo(){
    return this.http.get(`${this.baseUrl}/infoGeneralT`,{headers: this.reqHeader});
  }

 crearTipoInfo(info: TipoInformacion){
   return this.http.post(`${this.baseUrl}/infoGeneral`, info, {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));
  }

  actualizarTipoInfo(info: TipoInformacion){
    let id = btoa(info.clave);
    return this.http.put(`${this.baseUrl}/infoGeneral/${id}`, info, {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));
  }
  eliminarEstadoTI(id:any){
    id= btoa(id);
    return this.http.put(`${this.baseUrl}/eliminarEstado/${id}`,id , {headers: this.reqHeader});
  }

  habilitarEstadoTI(id:any){
    id=  btoa(id);
    return this.http.put(`${this.baseUrl}/habilitarEstado/${id}`, id, {headers: this.reqHeader});
  }
  
  eliminarTipoInfo(clave: any){
    clave= btoa(clave.toString());
    return this.http.delete(`${this.baseUrl}/infoGeneral/${clave}`, {headers: this.reqHeader});
  }

  //Tipos de periodo
  getTipoPeriodo(){
    return this.http.get(`${this.baseUrl}/periodoBoletaT`,{headers: this.reqHeader});
  }

 crearTipoPeriodo(periodo: PeriodoBoleta){
   return this.http.post(`${this.baseUrl}/periodoBoleta`, periodo, {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));
  }

  actualizarTipoPeriodo(periodo: PeriodoBoleta){
    let id = btoa(periodo.clave.toString());
    return this.http.put(`${this.baseUrl}/periodoBoleta/${id}`, periodo , {headers: this.reqHeader}).pipe(tap(() => this.dataUpdated.next()));

  }
  
  eliminarEstadoTP(id:any){
    id= btoa(id);
    return this.http.put(`${this.baseUrl}/eliminarEstadoPB/${id}`,id , {headers: this.reqHeader});
  }

  habilitarEstadoTP(id:any){
    id=  btoa(id);
    return this.http.put(`${this.baseUrl}/habilitarEstadoPB/${id}`, id, {headers: this.reqHeader});
  }

  eliminarTipoPeriodo(id: any){
    id= btoa(id.toString());
    return this.http.delete(`${this.baseUrl}/periodoBoleta/${id}`, {headers: this.reqHeader});
  }
}
