import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { MatSnackBar } from '@angular/material/snack-bar';
import { map } from 'rxjs';
import { ResponseData } from '../../../interfaces/response-data';
import { MESHMSInterface } from '../../../interfaces/mes/mesmhs/mesmhs-interface';
import { USERInterface } from 'src/app/interfaces/user-interface';
@Injectable({
  providedIn: 'root',
})
export class MESMHSService {
  private _url = 'https://qasirh.ittec.mx/v1/public/api';
  jwt = localStorage.getItem('jwt');
  headers = new HttpHeaders({ 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')!);
  }

  obtenerHistorial(numero_usuario: string) {
    this.ngOnInit();

    return this._http
      .get<ResponseData>(
        `${this._url}/MESMHS/consultar/${numero_usuario}/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`,
        {
          headers: this.headers,
        }
      )
      .pipe(
        map(
          (data: any) => {
            return data;
          },
          (error: any) => {
            if (!error.ok) {
              this.snackAlert('Ocurrió un error inesperado');
            }
            if (error.error.msg != undefined) {
              this.snackAlert(error.error.msg);
            }
            if (error.status == 408) {
              this.snackAlert('Conexion lenta');
            }
          }
        )
      );
  }

  obtenerDetalleSolicitud(id_solicitud: any) {
    this.ngOnInit();
    return this._http
      .get<ResponseData>(
        `${this._url}/MESMHS/consultar/solicitud/${id_solicitud}/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`,
        { headers: this.headers }
      )
      .pipe(
        map(
          (data: any) => {
            return data.response;
          },
          (error: any) => {
            if (!error.ok) {
              this.snackAlert('Ocurrió un error inesperado');
            }
            if (error.error.msg != undefined) {
              this.snackAlert(error.error.msg);
            }
            if (error.status == 408) {
              this.snackAlert('Conexion lenta');
            }
          }
        )
      );
  }

  obtenerSubordinados(numero_usuario: any) {
    this.ngOnInit();
    return this._http
      .get<ResponseData>(
        `${this._url}/MCOMAU/consultar/empleados/${numero_usuario}/${this.formatoRFC(this.usuario_session.RFCEMPRESA)}`,
        { headers: this.headers }
      )
      .pipe(
        map(
          (data: any) => {
            return data.response;
          },
          (error: any) => {
            if (!error.ok) {
              this.snackAlert('Ocurrió un error inesperado');
            }
            if (error.error.msg != undefined) {
              this.snackAlert(error.error.msg);
            }
            if (error.status == 408) {
              this.snackAlert('Conexion lenta');
            }
          }
        )
      );
  }

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

  private snackAlert(mensaje: string) {
    this._snackBar.open(mensaje, 'Cerrar', {
      duration: 4000,
    });
  }
}
