import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { BackendService } from '../../services/backend/backend.service';
import { CookieService } from 'ngx-cookie-service';

@Component({
	selector: 'app-login',
	templateUrl: './login.component.html',
	styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
	loginFG:FormGroup;
	submitted:boolean=false;
	wrongCredentials:boolean=false;
	userConnected:boolean=false;
	errorProcess:boolean=false;
	usernameConn:string='';

	constructor(private fb:FormBuilder,
				private bs:BackendService,
		private router:Router,
		private cs:CookieService) { }

	ngOnInit(): void {
		this.loginFG=this.fb.group({
			login:["", Validators.required],
			password:["", Validators.required],
		});
	}

	onLogin(): void {
		this.submitted=true;
		if(this.loginFG.invalid) {
			return;
		}

		this.bs.loginUser(this.loginFG.value).subscribe(
			data => {
				console.log(data);
				this.router.navigateByUrl("/infos");
			}, err => {
				this.wrongCredentials = true;
			});
	}
}
