import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import {
	FormBuilder,
	FormControl,
	FormGroup,
	Validators,
} from '@angular/forms';

type ForgotPasswordForm = {
	email: FormControl<string | null>;
};

@Component({
	selector: 'app-forgot-password',
	templateUrl: './forgot-password.component.html',
	styleUrls: ['./forgot-password.component.scss'],
})
export class ForgotPasswordComponent implements OnInit {
	authForm!: FormGroup<ForgotPasswordForm>;
	submitted = false;
	returnUrl!: string;

	constructor(
		private formBuilder: FormBuilder,
		private route: ActivatedRoute,
		private router: Router
	) {
	}

	ngOnInit() {
		this.authForm = this.formBuilder.group({
			email: [
				'',
				[Validators.required, Validators.email, Validators.minLength(5)],
			],
		});
		// get return url from route parameters or default to '/'
		this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
	}

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

	onSubmit() {
		this.submitted = true;
		// stop here if form is invalid
		if (this.authForm.invalid) {
			return;
		} else {
			this.router.navigate(['/dashboard/rests']);
		}
	}
}
