@page "/forgot-password"
@using WebClientTpos.Shared.DTOs
@using WebClientTpos.Shared
@inject HttpClient Http
@inject IStringLocalizer<ForgotPassword> L

@* 
    EN: Forgot password page.
    VI: Trang quên mật khẩu.
*@

<PageTitle>@L["Auth_ForgotPassword_Title"]</PageTitle>

<div class="auth-container">
    <section class="auth-card">
        <h1 class="auth-title">@L["Auth_ForgotPassword_Title"]</h1>
        <p class="auth-subtitle">@L["Auth_ForgotPassword_Subtitle"]</p>
        
        <EditForm Model="@forgotPasswordModel" OnValidSubmit="HandleForgotPassword" FormName="ForgotPasswordForm">
            <DataAnnotationsValidator />
            
            <div class="form-group">
                <label for="forgot-email">@L["Auth_ForgotPassword_Email"] *</label>
                <InputText id="forgot-email" 
                          @bind-Value="forgotPasswordModel.Email" 
                          class="form-input" 
                          placeholder="email@example.com" 
                          autocomplete="email" />
                <ValidationMessage For="() => forgotPasswordModel.Email" class="validation-message" />
            </div>
            
            <button type="submit" class="btn-primary btn-full" disabled="@isSubmitting">
                @if (isSubmitting)
                {
                    <span class="spinner-small"></span>
                    <span>@L["Common_Loading"]</span>
                }
                else
                {
                    @L["Auth_ForgotPassword_Submit"]
                }
            </button>
        </EditForm>
        
        @if (!string.IsNullOrEmpty(message))
        {
            <div class="alert @(success ? "alert-success" : "alert-error")">
                @message
            </div>
        }
        
        <div class="auth-footer">
            <a href="/login" class="link-primary">@L["Auth_ForgotPassword_BackToLogin"]</a>
        </div>
    </section>
</div>

@code {
    private ForgotPasswordDto forgotPasswordModel = new();
    private bool isSubmitting = false;
    private string message = "";
    private bool success = false;

    /// <summary>
    /// EN: Handle forgot password form submission.
    /// VI: Xử lý submit form quên mật khẩu.
    /// </summary>
    private async Task HandleForgotPassword()
    {
        isSubmitting = true;
        message = "";

        try
        {
            var response = await Http.PostAsJsonAsync("api/auth/forgot-password", forgotPasswordModel);
            
            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadFromJsonAsync<ApiResponse<object>>();
                if (result?.Success == true)
                {
                    success = true;
                    message = L["Auth_ForgotPassword_Success"];
                    forgotPasswordModel = new(); // Clear form
                }
                else
                {
                    success = false;
                    message = result?.Error ?? L["Auth_ForgotPassword_Error"];
                }
            }
            else
            {
                success = false;
                message = L["Auth_ForgotPassword_Error"];
            }
        }
        catch (Exception ex)
        {
            success = false;
            message = $"{L["Common_Error"]}: {ex.Message}";
        }
        finally
        {
            isSubmitting = false;
        }
    }
}
