import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MESMCVInterface } from 'src/app/interfaces/mes/mesmcv/mesmcv-interface';
import { ResponseData } from 'src/app/interfaces/response-data';
import { USERInterface } from 'src/app/interfaces/user-interface';

@Injectable({
  providedIn: 'root'
})
export class MESMCVService {
  private _url = 'https://qasirh.ittec.mx/v1/public/api/MESMCV';

  jwt = localStorage.getItem('jwt');
  header = new HttpHeaders().set('Authorization', 'Bearer ' + this.jwt);
  usuario_session: USERInterface = {} as USERInterface;


  constructor(private http: HttpClient, private _snackBar: MatSnackBar) { }

  ngOnInit() {
    this.usuario_session = JSON.parse(localStorage.getItem('TIMUSERENC')!);
  }

  obtener(idUsuarioSession: String) {
    this.ngOnInit() 
    return this.http.get<MESMCVInterface>(`${this._url}/consultar/${idUsuarioSession}/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`, {
      headers: this.header,
    });
  }

  cancelar(respuesta: any) {
    return this.http.post<ResponseData>(`${this._url}/cancelar`, respuesta, {
      headers: this.header,
    });
  }

  private formatoRFC(rfc: string){
    let rfc_decoded =  window.atob(rfc);
    let rfc_replace = rfc_decoded.replace("|", "/");
    return rfc_replace;
  } 
}
