@page "/admin/domains"
@using Moonlight.App.Repositories.Domains
@using Moonlight.App.Database.Entities
@using Microsoft.EntityFrameworkCore
@using BlazorTable
@using Moonlight.App.Services
@using Moonlight.Shared.Components.Navigations

@inject DomainRepository DomainRepository
@inject DomainService DomainService
@inject SmartTranslateService SmartTranslateService

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

<AdminDomainsNavigation Index="0" />

<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>Domains</TL>
                        </span>
                    </h3>
                    <div class="card-toolbar">
                        <a href="/admin/domains/new" class="btn btn-sm btn-light-success">
                            <i class="bx bx-layer-plus"></i>
                            <TL>New domain</TL>
                        </a>
                    </div>
                </div>
                <div class="card-body pt-0">
                    <div class="table-responsive">
                        <Table TableItem="Domain" Items="Domains" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
                            <Column TableItem="Domain" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Sortable="true" Filterable="true" Width="10%"/>
                            <Column TableItem="Domain" Title="@(SmartTranslateService.Translate("Name"))" Field="@(x => x.Name)" Sortable="true" Filterable="true" Width="10%"/>
                            <Column TableItem="Domain" Title="@(SmartTranslateService.Translate("Shared domain"))" Field="@(x => x.SharedDomain)" Sortable="true" Filterable="true" Width="10%">
                                <Template>
                                    <span>@(context.SharedDomain.Name)</span>
                                </Template>
                            </Column>
                            <Column TableItem="Domain" Title="@(SmartTranslateService.Translate("Owner"))" Field="@(x => x.Owner)" Sortable="true" Filterable="true" Width="10%">
                                <Template>
                                    <a href="/admin/users/view/@(context.Owner.Id)">@(context.Owner.Email)</a>
                                </Template>
                            </Column>
                            <Column TableItem="Domain" Title="@(SmartTranslateService.Translate("Manage"))" Field="@(x => x.Id)" Sortable="false" Filterable="false" Width="10%">
                                <Template>
                                    <a href="/domain/@(context.Id)">Manage</a>
                                </Template>
                            </Column>
                            <Column TableItem="Domain" Title="" Field="@(x => x.Id)" Sortable="false" Filterable="false" Width="10%">
                                <Template>
                                    <DeleteButton Confirm="true"
                                                  AdditionalCssClasses="float-end"
                                                  OnClick="() => Delete(context)">
                                    </DeleteButton>
                                </Template>
                            </Column>
                            <Pager ShowPageNumber="true" ShowTotalCount="true"/>
                        </Table>
                    </div>
                </div>
            </div>
</LazyLoader>

@code
{
    private Domain[] Domains;
    private LazyLoader LazyLoader;

    private Task Load(LazyLoader arg)
    {
        Domains = DomainRepository
            .Get()
            .Include(x => x.SharedDomain)
            .Include(x => x.Owner)
            .ToArray();

        return Task.CompletedTask;
    }

    private async Task Delete(Domain context)
    {
        await DomainService.Delete(context);
        await LazyLoader.Reload();
    }
}