@page "/admin/users"

@using Moonlight.App.Extensions.Attributes
@using Moonlight.App.Models.Enums
@using Moonlight.App.Repositories
@using BlazorTable

@attribute [RequirePermission(Permission.AdminUsers)]

@inject Repository<User> UserRepository

<AdminUsersNavigation Index="0"/>

<div class="card">
    <div class="card-body">
        <LazyLoader Load="Load">
            <Table TableItem="User"
                   Items="AllUsers"
                   PageSize="50"
                   TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3 fs-6"
                   TableHeadClass="fw-bold text-muted">
                <Column TableItem="User" Title="Id" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
                <Column TableItem="User" Title="Email" Field="@(x => x.Email)" Sortable="true" Filterable="true">
                    <Template>
                        <a href="/admin/users/view/@(context.Id)">@(context.Email)</a>
                    </Template>
                </Column>
                <Column TableItem="User" Title="Username" Field="@(x => x.Username)" Sortable="true" Filterable="true"/>
                <Column TableItem="User" Title="Created at" Field="@(x => x.CreatedAt)" Sortable="true" Filterable="true">
                    <Template>
                        <span>@(Formatter.FormatDate(context.CreatedAt))</span>
                    </Template>
                </Column>
                <Pager ShowPageNumber="true" ShowTotalCount="true" AlwaysShow="true"/>
            </Table>
        </LazyLoader>
    </div>
</div>

@code
{
    private User[] AllUsers;

    private Task Load(LazyLoader _)
    {
        AllUsers = UserRepository
            .Get()
            .ToArray();

        return Task.CompletedTask;
    }
}