import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { AuthService } from '@app/services/auth2.service';
import Swal from 'sweetalert2';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  loginForm: FormGroup;
  submitted:boolean = false;

  constructor(private authService: AuthService, private formBuilder: FormBuilder, private router: Router) { }

  ngOnInit() {
    this.loginForm = this.formBuilder.group({
      email: ['', Validators.required, Validators.email],
      password: ['', Validators.required]
    });
  }

  get f() { return this.loginForm.controls; }

  login() {
    this.submitted = true;

    // stop here if form is invalid
    if (this.loginForm.invalid) {
      return;
    }

    Swal.fire({
      allowOutsideClick: false,
      type: 'info',
      text: 'Espere por favor...'
    });
    Swal.showLoading();

    this.authService.login(
      {
        email: this.f.email.value,
        password: this.f.password.value
      }
    )
    .subscribe(success => {
      if (success) {
        window.location.href="#/dashboard";
      }
      else {
        Swal.fire({
          type: 'error',
          title: 'No se pudo auntenticar',
          text: "Email o contraseña incorrecta"
        })  
      }
    },(err) => {
      Swal.fire({
        type: 'error',
        title: 'Error en el servidor',
        text: "No su pudo obtener la informacion"
      });
    });
  }

}

/* import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';

import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import { NgForm } from '@angular/forms';

import { User } from '../../models/user';
import { AuthService } from '../../services/auth.service';

import Swal from 'sweetalert2';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  user: User = new User();
  recordarme = false;
  returnUrl: string;
  error_message: string;

  constructor( private auth: AuthService,
               private router: Router,
               private route: ActivatedRoute,
               private location: Location ) {

              this.router.routeReuseStrategy.shouldReuseRoute = function(){
                return false;
              }

              this.router.events.subscribe((evt) => {
                if (evt instanceof NavigationEnd) {
                  // trick the Router into believing it's last link wasn't previously loaded
                  this.router.navigated = false;
                  // if you need to scroll back to top, here is the right place
                  window.scrollTo(0, 0);
                }
              });
            }

  ngOnInit() {
    this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
    if ( localStorage.getItem('email') ) {
      this.user.email = localStorage.getItem('email');
      //this.recordarme = true;
    }
  }

  login( form: NgForm ) {
    if (  form.invalid ) { return; }

    Swal.fire({
      allowOutsideClick: false,
      type: 'info',
      text: 'Espere por favor...'
    });
    Swal.showLoading();

    this.auth.login( this.user )
      .subscribe( resp => {

        Swal.close();

        localStorage.setItem('email', this.user.email);

        //this.router.navigate([this.returnUrl]);
        window.location.href="";
      }, (err) => {
        if(err.error.error.message == "INVALID_EMAIL"){
          this.error_message = "Correo electrónico inválido";
        }
        else if(err.error.error.message == "INVALID_PASSWORD"){
          this.error_message = "Contraseña incorrecta";
        }
        else {
          this.error_message = "Ha ocurrido un error";
        }
        Swal.fire({
          type: 'error',
          title: 'Error al autenticar',
          text: this.error_message
        });
      });
  }

}
*/