@page "/admin/websites/"

@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 WebsiteRepository WebsiteRepository
@inject WebsiteService WebsiteService

<OnlyAdmin>
    <AdminWebsitesNavigation 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>Websites</TL>
                </span>
            </h3>
            <div class="card-toolbar">
                <a href="/admin/websites/new" class="btn btn-sm btn-light-success">
                    <i class="bx bx-user-plus"></i>
                    <TL>New website</TL>
                </a>
            </div>
        </div>
        <div class="card-body">
            <LazyLoader @ref="LazyLoader" Load="Load">
                <div class="table-responsive">
                    <Table TableItem="Website" Items="Websites" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
                        <Column TableItem="Website" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
                        <Column TableItem="Website" Title="@(SmartTranslateService.Translate("Base domain"))" Field="@(x => x.BaseDomain)" Sortable="true" Filterable="true">
                            <Template>
                                <a href="/website/@(context.Id)/">
                                    @(context.BaseDomain)
                                </a>
                            </Template>
                        </Column>
                        <Column TableItem="Website" 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="Website" Title="@(SmartTranslateService.Translate("Plesk server"))" Field="@(x => x.PleskServer.Id)" Sortable="true" Filterable="true">
                            <Template>
                                <a href="/admin/websites/servers/edit/@(context.PleskServer.Id)/">
                                    @(context.PleskServer.Name)
                                </a>
                            </Template>
                        </Column>
                        <Column TableItem="Website" 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 Website[] Websites;

    private Task Load(LazyLoader lazyLoader)
    {
        Websites = WebsiteRepository
            .Get()
            .Include(x => x.Owner)
            .Include(x => x.PleskServer)
            .ToArray();

        return Task.CompletedTask;
    }

    private async Task Delete(Website website)
    {
        await WebsiteService.Delete(website);
        await LazyLoader.Reload();
    }
}