import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthenService } from '../_service/authen.service';
import { Login } from '../_model/login';

@Component({
  selector: 'app-login',
  templateUrl: '../_views/login.component.html',
  styleUrls: ['../_views/login.component.scss', '../../share/eclettica.scss']
})
export class LoginComponent implements OnInit {
  buttonLoading: boolean;
  authen: Login;
  isError: boolean;
  constructor(private router: Router,
    private authenService: AuthenService) {
  }

  ngOnInit() {
    this.authen = new Login();
    this.isError = false;
  }

  doLogin() {
    this.buttonLoading = true;
    this.authenService.login(this.authen).subscribe(res => {
      if (res.status === 'OK') {
        this.buttonLoading = false;
        localStorage.setItem('token', res.response.authToken);
        localStorage.setItem('userId', res.response.userId);
        this.router.navigate(['/home/profile']);
      } else {
        this.buttonLoading = false;
        localStorage.clear();
        this.isError = true;
        this.authen.password = null;
        setTimeout(() => {
          this.isError = false;
        }, 3000);
      }
    });
  }

  forgetPassword() {
    this.router.navigate(['/authen/recoverypassword']);
  }
  
}