@page "/admin/subscriptions/"
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Repositories
@using BlazorTable
@using Moonlight.App.Services.Interop

@inject SmartTranslateService SmartTranslateService
@inject SubscriptionRepository SubscriptionRepository
@inject SubscriptionService SubscriptionService

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

<div class="card">
                    <LazyLoader @ref="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>Subscriptions</TL>
                                </span>
                            </h3>
                            <div class="card-toolbar">
                                <a href="/admin/subscriptions/new" class="btn btn-sm btn-light-success">
                                    <i class="bx bx-credit-card"></i>
                                    <TL>New subscription</TL>
                                </a>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <Table TableItem="Subscription" Items="Subscriptions" PageSize="25" TableClass="table table-row-bordered table-row-gray-100 align-middle gs-0 gy-3" TableHeadClass="fw-bold text-muted">
                                    <Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Id"))" Field="@(x => x.Id)" Sortable="true" Filterable="true"/>
                                    <Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Name"))" Field="@(x => x.Name)" Sortable="true" Filterable="true"/>
                                    <Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Description"))" Field="@(x => x.Description)" Sortable="true" Filterable="true"/>
                                    <Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Price"))" Field="@(x => x.Price)" Sortable="true" Filterable="true"/>
                                    <Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Currency"))" Field="@(x => x.Currency)" Sortable="true" Filterable="true"/>
                                    <Column TableItem="Subscription" Title="@(SmartTranslateService.Translate("Duration"))" Field="@(x => x.Duration)" Sortable="true" Filterable="true"/>
                                    <Column TableItem="Subscription" Title="" Field="@(x => x.Id)" Sortable="false" Filterable="false">
                                        <Template>
                                            <a href="/admin/subscriptions/edit/@(context.Id)/">
                                                <TL>Manage</TL>
                                            </a>
                                        </Template>
                                    </Column>
                                    <Column TableItem="Subscription" Title="" 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>
                        </div>
                    </LazyLoader>
                </div>

@code
{
    private Subscription[] Subscriptions;
    private LazyLoader LazyLoader;

    private Task Load(LazyLoader arg)
    {
        Subscriptions = SubscriptionRepository
            .Get()
            .ToArray();

        return Task.CompletedTask;
    }

    private async Task Delete(Subscription subscription)
    {
        await SubscriptionService.Delete(subscription);
        await LazyLoader.Reload();
    }
}