@page "/superadmin/system/audit"
@using global::System.Net.Http
@layout WebClientTpos.Client.Layout.SuperAdminLayout
@inherits SuperAdminBase
@inject IamApiService IamApi
@using WebClientTpos.Client.Services

<PageTitle>Audit Log — aPOS Super Admin</PageTitle>

<div class="sa-topbar">
    <div class="sa-topbar__left">
        <h1 class="sa-topbar__title">Nhật ký kiểm toán</h1>
        <p class="sa-topbar__subtitle">Theo dõi tất cả hoạt động trên nền tảng</p>
    </div>
    <div class="sa-topbar__right">
        <button class="sa-btn-outline" @onclick="LoadAsync">
            <i data-lucide="refresh-cw" style="width:14px;height:14px;"></i> Làm mới
        </button>
    </div>
</div>

<div class="sa-content">
    <div class="sa-panel">
        @if (_logs.Any())
        {
            <table class="sa-table">
                <thead>
                    <tr>
                        <th>Thời gian</th>
                        <th>Sự kiện</th>
                        <th>Người thực hiện</th>
                        <th>Đối tượng</th>
                        <th>Chi tiết</th>
                        <th>Trạng thái</th>
                        <th>IP</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var log in _logs)
                    {
                        <tr>
                            <td style="white-space:nowrap;">@(log.Timestamp?.ToLocalTime().ToString("dd/MM HH:mm:ss") ?? "—")</td>
                            <td><span class="sa-badge sa-badge--info">@(log.EventType ?? "—")</span></td>
                            <td>@(log.ActorEmail ?? log.ActorId ?? "—")</td>
                            <td>@(log.ResourceType ?? "—") @(log.ResourceId != null ? $"#{log.ResourceId[..Math.Min(8, log.ResourceId.Length)]}" : "")</td>
                            <td style="max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">@(log.Action ?? "—")</td>
                            <td>
                                <span class="sa-badge @(log.Success == true ? "sa-badge--success" : "sa-badge--danger")">
                                    @(log.Success == true ? "Thành công" : "Thất bại")
                                </span>
                            </td>
                            <td style="font-size:11px;color:var(--sa-text-tertiary);">@(log.IpAddress ?? "—")</td>
                        </tr>
                    }
                </tbody>
            </table>
        }
        else if (IsLoading)
        {
            <div class="sa-panel__body" style="text-align:center;padding:48px;">
                <p style="color:var(--sa-text-tertiary);">Đang tải...</p>
            </div>
        }
        else
        {
            <div class="sa-empty"><p>Chưa có nhật ký nào</p></div>
        }
    </div>
</div>

@code {
    private List<IamApiService.AuditLogDto> _logs = new();

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await LoadAsync();
    }

    private async Task LoadAsync()
    {
        IsLoading = true;
        StateHasChanged();
        _logs = await IamApi.GetAuditLogsAsync(100);
        IsLoading = false;
        StateHasChanged();
    }
}
