@page "/auth/login/branch"
@layout AuthLayout
@using WebClientTpos.Client.Services
@inherits AuthBase

@*
    EN: Branch login page — split panel layout, orange CTA, trust stats.
    VI: Trang đăng nhập Chi nhánh — layout chia đôi, CTA cam, thống kê tin cậy.
    Design: pencil-design/src/pages/tPOS/auth/login/branch-desktop.pen
*@

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

<div class="auth-page--split">
    <BrandPanel PanelClass="auth-brand-panel--orange"
                LogoText="a"
                Title="@L["Auth_Branch_BrandTitle"]"
                Description="@L["Auth_Branch_BrandDesc"]" />

    <div class="auth-form-panel">
        <AuthCard Title="@L["Auth_Branch_Title"]"
                  Subtitle="@L["Auth_Branch_Subtitle"]"
                  Transparent="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_Branch_EmailLabel"]"
                               InputType="text"
                               Placeholder="@L["Auth_Branch_EmailPlaceholder"]"
                               PrefixIcon="mail"
                               InputId="branch-email"
                               AutoComplete="email"
                               Value="@_email"
                               ValueChanged="@(e => _email = e.Value?.ToString() ?? "")" />

                    <AuthInput Label="@L["Auth_Branch_PasswordLabel"]"
                               InputType="password"
                               Placeholder="••••••••"
                               PrefixIcon="lock"
                               InputId="branch-password"
                               AutoComplete="current-password"
                               Value="@_password"
                               ValueChanged="@(e => _password = e.Value?.ToString() ?? "")"
                               ForgotPasswordLink="/forgot-password"
                               ForgotPasswordText="@L["Auth_Branch_ForgotPwd"]" />
                </div>

                <AuthButton Variant="orange" Disabled="@_isLoading" Loading="@_isLoading" OnClick="HandleLogin">
                    @if (_isLoading)
                    {
                        <span>Đang xử lý...</span>
                    }
                    else
                    {
                        @L["Auth_Branch_Submit"]
                    }
                </AuthButton>
            </ChildContent>
            <FooterContent>
                <span class="auth-footer-text">
                    @L["Auth_Branch_NoAccount"] <a href="/register">@L["Auth_Branch_Contact"]</a>
                </span>
            </FooterContent>
        </AuthCard>
    </div>
</div>

@code {
    [Inject] private AuthService AuthSvc { get; set; } = default!;
    [Inject] private NavigationManager Nav { get; set; } = default!;

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

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

        if (string.IsNullOrWhiteSpace(_email))
        {
            _errorMessage = "Vui lòng nhập email";
            return;
        }
        if (string.IsNullOrWhiteSpace(_password))
        {
            _errorMessage = "Vui lòng nhập mật khẩu";
            return;
        }

        _isLoading = true;
        StateHasChanged();

        var (ok, error) = await AuthSvc.LoginAsync(_email, _password, "branch");

        if (ok)
        {
            _isLoading = false;
            StateHasChanged();
            await Task.Delay(500);
            Nav.NavigateTo("/admin", forceLoad: true);
        }
        else
        {
            _errorMessage = error ?? "Đăng nhập thất bại";
            _isLoading = false;
        }
    }
}
