@page "/admin/servers/images"

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

@inject ImageRepository ImageRepository
@inject SmartTranslateService SmartTranslateService

<OnlyAdmin>
    <div class="row">
        <LazyLoader @ref="LazyLoader" Load="Load">
            <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>Images</TL>
                        </span>
                    </h3>
                    <div class="card-toolbar">
                        <a href="/admin/servers/images/new" class="btn btn-sm btn-light-success">
                            <i class="bx bx-layer-plus"></i>
                            <TL>New image</TL>
                        </a>
                    </div>
                </div>
                <div class="card-body pt-0">
                    @if (Images.Any())
                    {
                        <div class="table-responsive">
                            <Table TableItem="Image" Items="Images" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
                                <Column TableItem="Image" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
                                <Column TableItem="Image" Title="@(SmartTranslateService.Translate("Name"))" Field="@(x => x.Name)" Sortable="true" Filterable="true"/>
                                <Column TableItem="Image" Title="@(SmartTranslateService.Translate("Description"))" Field="@(x => x.Description)" Sortable="true" Filterable="true"/>
                                <Column TableItem="Image" Title="@(SmartTranslateService.Translate("Uuid"))" Field="@(x => x.Uuid)" Sortable="true" Filterable="true"/>
                                <Column TableItem="Image" Title="" Field="@(x => x.Id)" Sortable="false" Filterable="false">
                                    <Template>
                                        <a href="/admin/servers/images/edit/@(context.Id)">
                                            @(SmartTranslateService.Translate("Edit"))
                                        </a>
                                    </Template>
                                </Column>
                                <Column TableItem="Image" Title="" Field="@(x => x.Id)" Sortable="false" Filterable="false">
                                    <Template>
                                        <WButton Text="@(SmartTranslateService.Translate("Delete"))"
                                                 WorkingText="@(SmartTranslateService.Translate("Deleting"))"
                                                 CssClasses="btn-danger"
                                                 OnClick="() => Delete(context)">
                                        </WButton>
                                    </Template>
                                </Column>
                                <Pager ShowPageNumber="true" ShowTotalCount="true"/>
                            </Table>
                        </div>
                    }
                    else
                    {
                        <div class="alert alert-info">
                            <TL>No images found</TL>
                        </div>
                    }
                </div>
            </div>
        </LazyLoader>
    </div>
</OnlyAdmin>

@code
{
    private Image[] Images;
    private LazyLoader LazyLoader;

    private Task Load(LazyLoader arg)
    {
        Images = ImageRepository
            .Get()
            .Include(x => x.DockerImages)
            .Include(x => x.Variables)
            .ToArray();

        return Task.CompletedTask;
    }

    private async Task Delete(Image image)
    {
        ImageRepository.Delete(image);
        await LazyLoader.Reload();
    }
}