import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { CommonModule } from '@angular/common';
import { HeaderComponent } from '../header/header.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@Component({
  selector: 'app-forgot-password',
  standalone: true,
  imports: [CommonModule, HeaderComponent, FormsModule, ReactiveFormsModule],
  templateUrl: './forgot-password.component.html',
  styleUrls: ['./forgot-password.component.css']
})
export class ForgotPasswordComponent {
  email = '';
  message = '';
  error = '';
    isSubmitting: boolean = false;

  constructor(private http: HttpClient, private router: Router) {}

  onSubmit() {
    this.message = this.error = '';

    this.http.post<any>('https://infoways.in/funtelapi/forgot_password.php', {
      action: 'forgot',
      email: this.email
    }).subscribe({
      next: (res) => {
        res.status === 'success' ? this.message = res.message : this.error = res.message;
      },
      error: () => this.error = 'Server error. Try again.'
    });
  }

  backToLogin() {
    this.router.navigate(['/login']);
  }
}
