@page "/registry/private"

@using Hcs.WebApp.Components.Dialogs
@using Hcs.WebApp.Services
@using Microsoft.AspNetCore.Authorization
@using System.Security.Claims

@inherits SyncedPageBase<Registry>

@attribute [Authorize]

@inject RegistryService RegistryService
@inject DialogService DialogService

<PageTitle>Частные справочники подсистемы НСИ</PageTitle>

<AuthorizedContent Roles="@($"{AppRole.ADMINISTRATOR_TYPE},{AppRole.OPERATOR_TYPE}")">
    <Content>
        <RadzenStack>
            <RadzenRow AlignItems="AlignItems.Center">
                <RadzenColumn Size="12" SizeMD="6">
                    <RadzenText Text="Частные справочники подсистемы НСИ" TextStyle="TextStyle.H5" class="rz-m-0" />
                </RadzenColumn>
                <RadzenColumn Size="12" SizeMD="6">
                    <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.End" Gap="0.5rem">
                        <RadzenButton Icon="sync" Text="@syncButtonText" Disabled="@(state != SyncedPageState.Idle)" Click="@SyncDataAsync" ButtonStyle="ButtonStyle.Primary" />
                    </RadzenStack>
                </RadzenColumn>
            </RadzenRow>
            <RadzenRow>
                <RadzenColumn SizeMD="12">
                    <RadzenDataGrid TItem="Registry" Data="@data" RowExpand="@RowExpandAsync" IsLoading="@(state != SyncedPageState.Idle)" AllowFiltering="true" AllowPaging="true" ShowPagingSummary="true" PageSizeOptions=@(new int[] { 5, 10, 20, 30 }) AllowSorting="true" ExpandMode="DataGridExpandMode.Single">
                        <Template Context="registry">
                            <RadzenDataGrid Data="@registry.Elements" AllowFiltering="true" AllowSorting="true">
                                <Columns>
                                    <RadzenDataGridColumn Property="@nameof(RegistryElement.Code)" Title="Код" SortOrder="SortOrder.Ascending" Resizable="false" Width="100px" MaxWidth="100px" />
                                    <RadzenDataGridColumn Property="@nameof(RegistryElement.GUID)" Title="Идентификатор" />
                                    <RadzenDataGridColumn Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="70px">
                                        <Template Context="registryElement">
                                            <RadzenButton Click="@(() => ShowElementAsync(registryElement))" @onclick:stopPropagation="true" ButtonStyle="ButtonStyle.Primary" Icon="visibility" Size="ButtonSize.Small" />
                                        </Template>
                                    </RadzenDataGridColumn>
                                </Columns>
                            </RadzenDataGrid>
                        </Template>
                        <Columns>
                            <RadzenDataGridColumn TItem="Registry" Property="@nameof(Registry.Number)" Title="Номер" Resizable="false" Width="100px" MaxWidth="100px" />
                            <RadzenDataGridColumn TItem="Registry" Property="@nameof(Registry.Name)" Title="Название" />
                            <RadzenDataGridColumn TItem="Registry" Property="@nameof(Registry.SyncedAt)" Title="Дата синхронизации" />
                        </Columns>
                    </RadzenDataGrid>
                </RadzenColumn>
            </RadzenRow>
        </RadzenStack>
    </Content>
</AuthorizedContent>

@code {
    protected override Campaign.CampaignType CampaignType => Campaign.CampaignType.ExportPrivateRegistryElements_15_7_0_1;

    protected override bool HasPermission(ClaimsPrincipal user)
    {
        return user.IsOperatorOrHigher();
    }

    protected override Task<IEnumerable<Registry>> GetDataAsync()
    {
        return RegistryService.GetAllRegistriesAsync(false);
    }

    async Task RowExpandAsync(Registry registry)
    {
        if (registry.Elements == null)
        {
            registry.Elements = await RegistryService.GetRegistryElementsAsync(registry.Id);
        }
    }

    async Task ShowElementAsync(RegistryElement registryElement)
    {
        await DialogService.OpenAsync<ViewRegistryElement>(
            "Просмотр записи",
            new Dictionary<string, object>()
            {
                { nameof(ViewRegistryElement.RegistryElement), registryElement }
            },
            new DialogOptions()
            {
                Width = "420px",
                Height = "600px"
            });
    }
}
