import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MesmprInterface } from 'src/app/interfaces/mes/mesmpr/mesmpr-interface';
import { USERInterface } from 'src/app/interfaces/user-interface';
import { IAMService } from '../iam/iam.service';

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

  jwt:any = ''
  header:HttpHeaders;
  proximo_periodo = new Date().getFullYear() + 1;
  usuario_session: USERInterface = {} as USERInterface;

  constructor(private http: HttpClient, ) {
    this.usuario_session = JSON.parse(localStorage.getItem('TIMUSERENC')!);
    this.jwt = localStorage.getItem('jwt');
    this.header = new HttpHeaders().set('Authorization', 'Bearer ' + this.jwt);
  }

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

  consultarAnio() {
    this.ngOnInit();
    return this.http.get<MesmprInterface>(`${this._url}/consultar/${this.proximo_periodo}/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`, { headers: this.header });
  }

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

}
