import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from './../../services/auth.service';
import { NotifierService } from "angular-notifier";
import {FormControl,  Validators} from '@angular/forms';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  
  error: any;
  private readonly notifier: NotifierService;

  constructor(
    private authService: AuthService,
    private router: Router,
    notifierService: NotifierService,
  ) {this.notifier = notifierService; }

  hide = true;
  email = new FormControl('', [Validators.required, Validators.email]);
  password = new FormControl('');
  getErrorMessage() {
    if (this.email.hasError('required')) {
      return 'You must enter a value';
    }
    return this.email.hasError('email') ? 'Not a valid email' : '';
  }

  ngOnInit() {
  }

  login(email: string, password: string) {
    this.authService.login(email, password).subscribe(
      success => this.router.navigate(['groupboilerplates']),
      error => this.notifier.notify("warning",(JSON.stringify("You have entered an invalid username or password")))
    );
  }

}
