import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AuthService } from '../../services/auth.service'
import { Router, RouterLink } from '@angular/router';

@Component({
  selector: 'app-recuperar-senha',
  standalone: true,
  imports: [RouterModule, CommonModule, FormsModule],
  templateUrl: './recuperar-senha.component.html',
  styleUrl: './recuperar-senha.component.css'
})
export class RecuperarSenhaComponent {
  email: string = '';
  loading = false;
  error = '';
  message = '';
  
  constructor(
      private authService: AuthService,
      private router: Router
    ) {  }

  solicitaResetSenha() {
    this.error = '';
    this.message = '';
    this.loading = true;
    if (!this.email || this.email.trim() ==="") {
      this.error = 'Por favor, seu email';
      this.loading = false;
      return;
    }
    this.authService.enviaEmailResetSenha(this.email).subscribe({
      next: (response) => {
        this.message = response;
        this.loading = false;
        this.email = '';
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 4000); 
      },
      error: (err) =>{
        this.error = 'Email informado não esta cadastrado.';
        this.loading = false;
        console.error('Erro1: ', err.body);
        this.email = '';
      },
    });
    
  }
}
