import { Component, AfterViewInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';

import { AppComponent } from 'src/app/app.component';
import { ENCService } from 'src/app/services/enc/enc.service';
import { IAMService } from 'src/app/services/iam/iam.service';

@Component({
  selector: 'app-iam',
  templateUrl: './iam.component.html',
  styleUrls: ['./iam.component.css'],
})
export class IAMComponent implements AfterViewInit {
  formGroup: FormGroup;
  hide = true;
  anio: number = 0;
  btnLoginDisable = false;
  solicitud_datos: string = '';
  solicitud: any = {};
  empresa: string = '';

  constructor(
    public router: Router,
    private _IAMService: IAMService,
    private activatedRoute: ActivatedRoute,
  ) {
    this.anio = new Date().getFullYear();
    this.activatedRoute.params.subscribe((params) => {
      this.solicitud_datos = params['solicitud_datos'] == undefined
          ? ''
          : params['solicitud_datos'].replace('data=', '');
    });
    this.empresa = localStorage.getItem('RFCEMPRESA')!;
    this.formGroup = this.createFormGroup();
    
  }

  imagenLogo() {
    let empresa = localStorage.getItem('RFCEMPRESA');

    if (window.location.href.includes('ferrariventilatori')) {
      return `https://ferrariventilatori.mx/vacaciones/assets/img/${empresa}.png`;
    } else {
      return `../../../assets/img/${empresa}.png`;
    }
  }

  ngAfterViewInit(): void {    
    this.cargarColores();
    let cont = 0;
    setInterval( () => {
     /*  if (cont == 0) {
        this.solicitud_datos = this.solicitud_datos;
        cont++;
      } */


      if (localStorage.getItem("jwt") !== undefined && location.href.includes("/iam") && ! location.href.includes(";solicitud_datos=")) {
        this.router.navigate(['/mesmpr']);
        
      }
    }, 1000);
  }

  async cargarColores() {
    document.getElementById('colorApp1')!.classList.remove('primary');
    document.getElementById('colorApp1')!.classList.add(this.empresa);

    document.getElementById('colorApp2')!.classList.remove('primary');
    document.getElementById('colorApp2')!.classList.add(this.empresa);

    document.getElementById('colorApp3')!.classList.remove('primary');
    document.getElementById('colorApp3')!.classList.add(this.empresa);
  }

  private createFormGroup() {
    return new FormGroup({
      email: new FormControl('', [Validators.required]),
      password: new FormControl('', Validators.required),
    });
  }

  get email() {
    return this.formGroup.get('email');
  }
  get password() {
    return this.formGroup.get('password');
  }

  async login() {
    this.btnLoginDisable = true;
    console.log(this.solicitud_datos);
    await this._IAMService.login(this.formGroup.value, this.solicitud_datos);
    this.btnLoginDisable = false;
  }

  enableBtnLogin() {
    this.btnLoginDisable = false;
  }

  imagenLogoITTEC() {
    if (window.location.href.includes('ferrariventilatori')) {
      return "https://ferrariventilatori.mx/vacaciones/assets/img/ITTEC-FOOTER.png"
    } else {
      return '../assets/img/ITTEC-FOOTER.png';
    }
  }
}
