import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../../services/auth.service';
import Swal from 'sweetalert2';
// import { AuthService } from '../../services/auth.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent {
  
  usuario: string = '';
  password: string = '';

  constructor(
    private router: Router,
    private authService: AuthService
  ){}
  login(){
    if (this.authService.login(this.usuario,this.password)) {
      const Toast = Swal.mixin({
        toast: true,
        position: 'top-right',
        iconColor: 'white',
        customClass: {
          popup: 'colored-toast'
        },
        showConfirmButton: false,
        timer: 1500,
        timerProgressBar: true
      })
      Toast.fire({
        icon: 'success',
        title: 'Inicio correcto'
      })
      if (this.authService.getUserRole()=='agente') {
        this.router.navigate(['./pasajeros']);
      } else if(this.authService.getUserRole()=='supervisor') {
        this.router.navigate(['./aeropuertos']);
      }
    } else {
      const Toast = Swal.mixin({
        toast: true,
        position: 'top-right',
        iconColor: 'white',
        customClass: {
          popup: 'colored-toast'
        },
        showConfirmButton: false,
        timer: 1500,
        timerProgressBar: true
      })
      Toast.fire({
        icon: 'error',
        title: 'Usuario o password incorrectos!'
      })
    }
  }
}
