import { Component, OnInit } from '@angular/core';
import {
  FormGroup,
  UntypedFormControl,
  UntypedFormGroup,
  Validators,
} from '@angular/forms';
import { Router } from '@angular/router';
import { BaseForm } from '@core/base/base-form';
import { ComponentBase } from '@core/base/common-base';
import { AuthService } from '@core/services/authentication/auth.service';
import { ToastrService } from 'ngx-toastr';
import { takeUntil } from 'rxjs';

@Component({
  selector: 'app-forget-password',
  templateUrl: './forget-password.component.html',
  styleUrls: ['./forget-password.component.scss'],
})
export class ForgetPasswordComponent
  extends ComponentBase
  implements BaseForm, OnInit
{
  form!: FormGroup;
  constructor(
    private readonly toastr: ToastrService,
    private readonly router: Router,
    private readonly authService: AuthService
  ) {
    super();
  }

  ngOnInit(): void {
    this.initForm();
  }

  initForm(): void {
    this.form = new UntypedFormGroup({
      email: new UntypedFormControl(null, [
        Validators.required,
        Validators.email,
      ]),
    });
  }

  onSubmit(): void {
    if (super.submitValidity(this.form)) {
      this.authService
        .ForgetPassword(this.form.value)
        .pipe(takeUntil(this.destroy$))
        .subscribe((response) => {
          console.log(response);
          this.toastr.success('Successfully !');
          this.router.navigate(['../verify']);
        });
    }
  }
}
