@page "/admin/users"

@using Moonlight.Shared.Components.Navigations
@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities
@using BlazorTable
@using Moonlight.App.Helpers
@using Moonlight.App.Services

@inject UserRepository UserRepository
@inject SmartTranslateService SmartTranslateService

@attribute [PermissionRequired(nameof(Permissions.AdminUsers))]

<AdminSessionNavigation Index="0"/>

<div class="card">
    <LazyLoader Load="Load">
        <div class="card-header border-0 pt-5">
            <h3 class="card-title align-items-start flex-column">
                <span class="card-label fw-bold fs-3 mb-1">
                    <TL>Users</TL>
                </span>
            </h3>
            <div class="card-toolbar">
                <a href="/admin/users/new" class="btn btn-sm btn-light-success">
                    <i class="bx bx-user-plus"></i>
                    <TL>New user</TL>
                </a>
            </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <Table TableItem="User" Items="Users" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
                    <Column TableItem="User" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
                    <Column TableItem="User" Title="@(SmartTranslateService.Translate("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="@(SmartTranslateService.Translate("First name"))" Field="@(x => x.FirstName)" Sortable="true" Filterable="true"/>
                    <Column TableItem="User" Title="@(SmartTranslateService.Translate("Last name"))" Field="@(x => x.LastName)" Sortable="true" Filterable="true"/>
                    <Column TableItem="User" Title="@(SmartTranslateService.Translate("Created at"))" Field="@(x => x.CreatedAt)" Sortable="true" Filterable="true">
                        <Template>
                            <span>@(Formatter.FormatDate(context.CreatedAt))</span>
                        </Template>
                    </Column>
                    <Column TableItem="User" Title="@(SmartTranslateService.Translate("Manage"))" Field="@(x => x.Id)" Sortable="false" Filterable="false">
                        <Template>
                            <a href="/admin/users/edit/@(context.Id)/">
                                <TL>Manage</TL>
                            </a>
                        </Template>
                    </Column>
                    
                    <Column TableItem="User" Title="@(SmartTranslateService.Translate("Discord ID"))" Field="@(x => x.DiscordId)" Sortable="true" Filterable="true"/>
                    <Pager ShowPageNumber="true" ShowTotalCount="true"/>
                </Table>
            </div>
        </div>
    </LazyLoader>
</div>

@code
{
    private User[] Users;

    private async Task Load(LazyLoader lazyLoader)
    {
        Users = UserRepository.Get().ToArray();
        await InvokeAsync(StateHasChanged);
    }
}