@page "/admin/webspaces/"

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

@inject SmartTranslateService SmartTranslateService
@inject Repository<WebSpace> WebSpaceRepository
@inject WebSpaceService WebSpaceService

<OnlyAdmin>
    <AdminWebspacesNavigation Index="0"/>

    <div class="card">
        <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>Webspaces</TL>
                </span>
            </h3>
            <div class="card-toolbar">
                <a href="/admin/webspaces/new" class="btn btn-sm btn-light-success">
                    <i class="bx bx-user-plus"></i>
                    <TL>New webspace</TL>
                </a>
            </div>
        </div>
        <div class="card-body">
            <LazyLoader @ref="LazyLoader" Load="Load">
                <div class="table-responsive">
                    <Table TableItem="WebSpace" Items="WebSpaces" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
                        <Column TableItem="WebSpace" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
                        <Column TableItem="WebSpace" Title="@(SmartTranslateService.Translate("Domain"))" Field="@(x => x.Domain)" Sortable="true" Filterable="true">
                            <Template>
                                <a href="/webspace/@(context.Id)/">
                                    @(context.Domain)
                                </a>
                            </Template>
                        </Column>
                        <Column TableItem="WebSpace" Title="@(SmartTranslateService.Translate("Owner"))" Field="@(x => x.Id)" Sortable="false" Filterable="false">
                            <Template>
                                <a href="/admin/users/view/@(context.Owner.Id)">
                                    @(context.Owner.Email)
                                </a>
                            </Template>
                        </Column>
                        <Column TableItem="WebSpace" Title="@(SmartTranslateService.Translate("Cloud panel"))" Field="@(x => x.CloudPanel.Name)" Sortable="true" Filterable="true">
                            <Template>
                                <a href="/admin/webspaces/servers/edit/@(context.CloudPanel.Id)/">
                                    @(context.CloudPanel.Name)
                                </a>
                            </Template>
                        </Column>
                        <Column TableItem="WebSpace" Title="@(SmartTranslateService.Translate("Manage"))" Field="@(x => x.Id)" Sortable="false" Filterable="false">
                            <Template>
                                <DeleteButton Confirm="true" OnClick="() => Delete(context)">
                                </DeleteButton>
                            </Template>
                        </Column>
                        <Pager ShowPageNumber="true" ShowTotalCount="true"/>
                    </Table>
                </div>
            </LazyLoader>
        </div>
    </div>
</OnlyAdmin>

@code
{
    private LazyLoader LazyLoader;

    private WebSpace[] WebSpaces;

    private Task Load(LazyLoader lazyLoader)
    {
        WebSpaces = WebSpaceRepository
            .Get()
            .Include(x => x.CloudPanel)
            .Include(x => x.Owner)
            .ToArray();

        return Task.CompletedTask;
    }

    private async Task Delete(WebSpace webSpace)
    {
        await WebSpaceService.Delete(webSpace);
        await LazyLoader.Reload();
    }
}