@page "/auth/forgot-password-new"
@layout AuthLayout
@inherits AuthBase

@*
    EN: Forgot password page — centered card, email/phone input -> sends reset link.
    VI: Trang quên mật khẩu — card giữa, input email/SĐT -> gửi link đặt lại.
    Design: pencil-design/src/pages/tPOS/auth/forgot-password/desktop.pen
*@

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

<div class="auth-page">
    @if (!_resetSent)
    {
        <AuthCard Title="@L["Auth_Forgot_Title"]"
                  Subtitle="@L["Auth_Forgot_Subtitle"]"
                  Icon="key-round"
                  IconClass="auth-icon--orange"
                  BackLink="/login"
                  BackLinkText="@L["Auth_Forgot_BackLink"]"
                  Compact="true">
            <ChildContent>
                <div class="auth-form">
                    @if (!string.IsNullOrEmpty(_errorMessage))
                    {
                        <div style="margin-bottom:16px;padding:12px 16px;border-radius:8px;background:rgba(239,68,68,0.12);color:#EF4444;font-size:14px;display:flex;align-items:center;gap:8px;">
                            <i data-lucide="alert-circle" style="width:16px;height:16px;flex-shrink:0;"></i>
                            <span>@_errorMessage</span>
                        </div>
                    }

                    <AuthInput Label="@L["Auth_Forgot_InputLabel"]"
                               InputType="text"
                               Placeholder="@L["Auth_Forgot_InputPlaceholder"]"
                               PrefixIcon="mail"
                               InputId="forgot-email"
                               Value="@_email"
                               ValueChanged="@(e => _email = e.Value?.ToString() ?? "")" />
                </div>

                <AuthButton Variant="orange" Disabled="@_isLoading" Loading="@_isLoading" OnClick="HandleSubmit">
                    @L["Auth_Forgot_Submit"]
                </AuthButton>
            </ChildContent>
        </AuthCard>
    }
    else
    {
        <AuthCard Title="@L["Auth_Forgot_SentTitle"]"
                  Subtitle="@L["Auth_Forgot_SentSubtitle"]"
                  Icon="mail-check"
                  IconClass="auth-icon--success">
            <ChildContent>
                <span class="auth-highlight">@_email</span>

                <AuthButton Variant="orange" IconName="external-link">
                    @L["Auth_Forgot_OpenEmail"]
                </AuthButton>

                <AuthButton Variant="outline" OnClick="HandleResend">
                    @L["Auth_Forgot_ResendEmail"]
                </AuthButton>
            </ChildContent>
            <FooterContent>
                <span class="auth-footer-text">
                    @L["Auth_Forgot_NotReceived"]
                </span>
                <span class="auth-footer-text">
                    @L["Auth_Forgot_CheckSpam"] <a href="/auth/forgot-password-new">@L["Auth_Forgot_TryAnother"]</a>
                </span>
            </FooterContent>
        </AuthCard>
    }
</div>

@code {
    [Inject] private HttpClient Http { get; set; } = default!;

    private string _email = "";
    private bool _resetSent = false;
    private bool _isLoading = false;
    private string? _errorMessage;

    private async Task HandleSubmit()
    {
        if (_isLoading) return;
        _errorMessage = null;

        if (string.IsNullOrWhiteSpace(_email))
        {
            _errorMessage = "Vui lòng nhập email hoặc số điện thoại";
            return;
        }

        _isLoading = true;
        StateHasChanged();

        try
        {
            var response = await Http.PostAsJsonAsync("/api/auth/forgot-password", new { Email = _email });
            // EN: Show success state regardless (security: don't reveal if email exists)
            // VI: Hiển thị thành công (bảo mật: không tiết lộ email có tồn tại không)
            _resetSent = true;
        }
        catch
        {
            _resetSent = true;
        }
        finally
        {
            _isLoading = false;
        }
    }

    private void HandleResend()
    {
        _resetSent = false;
    }
}
