@page "/auth/login/admin"
@layout AuthLayout
@using WebClientTpos.Client.Services
@using WebClientTpos.Shared.DTOs
@inherits AuthBase

@*
    EN: Admin login page — connected to Duende IdentityServer password grant.
    VI: Trang đăng nhập Admin — kết nối Duende IdentityServer password grant.
*@

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

<div class="auth-page">
    <AuthCard Title="@L["Auth_Admin_Title"]"
              Subtitle="@L["Auth_Admin_Subtitle"]"
              Icon="shield"
              IconClass="auth-icon--blue"
              RoleBadge="@L["Auth_Admin_RoleBadge"]"
              RoleBadgeClass="auth-role-badge--blue"
              SecurityHint="@L["Auth_Admin_SecurityHint"]"
              SecurityHintIcon="shield-alert"
              SecurityHintClass="auth-security-hint--warning">
        <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>
                }

                @if (_successMessage != null)
                {
                    <div style="margin-bottom:16px;padding:12px 16px;border-radius:8px;background:rgba(34,197,94,0.12);color:#22C55E;font-size:14px;display:flex;align-items:center;gap:8px;">
                        <i data-lucide="check-circle" style="width:16px;height:16px;flex-shrink:0;"></i>
                        <span>@_successMessage</span>
                    </div>
                }

                <AuthInput Label="@L["Auth_Admin_EmailLabel"]"
                           InputType="email"
                           Placeholder="@L["Auth_Admin_EmailPlaceholder"]"
                           PrefixIcon="mail"
                           InputId="admin-email"
                           AutoComplete="email"
                           Value="@_email"
                           ValueChanged="@(e => _email = e.Value?.ToString() ?? "")" />
                
                <AuthInput Label="@L["Auth_Admin_PasswordLabel"]"
                           InputType="password"
                           Placeholder="••••••••"
                           PrefixIcon="lock"
                           InputId="admin-password"
                           AutoComplete="current-password"
                           Value="@_password"
                           ValueChanged="@(e => _password = e.Value?.ToString() ?? "")" />
            </div>
            
            <AuthButton Variant="blue" IconName="shield" Disabled="@_isLoading" Loading="@_isLoading" OnClick="HandleLogin">
                @if (_isLoading)
                {
                    <span>Đang xử lý...</span>
                }
                else
                {
                    @L["Auth_Admin_Submit"]
                }
            </AuthButton>
        </ChildContent>
        <FooterContent>
            <div class="auth-role-hints">
                <span class="auth-role-hints-title">@L["Auth_Admin_OtherRoles"]</span>
                <div class="auth-role-list">
                    <a href="/auth/login/branch" class="auth-role-item">
                        <i data-lucide="store"></i> @L["Auth_Admin_RoleBranch"]
                    </a>
                    <a href="/auth/login/staff" class="auth-role-item">
                        <i data-lucide="user"></i> @L["Auth_Admin_RoleStaff"]
                    </a>
                </div>
            </div>
        </FooterContent>
    </AuthCard>
</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 string? _successMessage;

    /// <summary>
    /// EN: Handle login form submission — calls Duende IdentityServer.
    /// VI: Xử lý submit form đăng nhập — gọi Duende IdentityServer.
    /// </summary>
    private async Task HandleLogin()
    {
        if (_isLoading) return;
        _errorMessage = null;
        _successMessage = null;

        // EN: Basic client-side validation
        // VI: Validation cơ bản phía client
        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, "owner");

        if (ok)
        {
            _successMessage = "Đăng nhập thành công! Đang chuyển hướng...";
            _isLoading = false;
            StateHasChanged();
            await Task.Delay(800);
            Nav.NavigateTo("/admin", forceLoad: true);
        }
        else
        {
            _errorMessage = error ?? "Đăng nhập thất bại";
            _isLoading = false;
        }
    }
}
