@inherits LayoutComponentBase

@implements IDisposable

@inject NavigationManager NavigationManager
@inject NotificationService NotificationService
@inject IWebHostEnvironment WebHostEnvironment

<RadzenDialog />
<RadzenLayout>
    <RadzenHeader>
        <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Gap="0">
            <RadzenSidebarToggle Click="@(() => sidebarExpanded = !sidebarExpanded)" />
            @if (WebHostEnvironment.IsDevelopment())
            {
                <RadzenLabel Text="Система автоматической интеграции с ГИС ЖКХ" />
                <RadzenLabel Text="(СРЕДА РАЗРАБОТКИ)" Style="color: red;" class="rz-ml-1" />
            }
            else
            {
                <RadzenLabel Text="Система автоматической интеграции с ГИС ЖКХ" />
            }
        </RadzenStack>
    </RadzenHeader>
    <RadzenSidebar FullHeight="true" @bind-Expanded="@sidebarExpanded">
        <RadzenStack Style="height: 100%" JustifyContent="JustifyContent.SpaceBetween">
            <RadzenPanelMenu>
                <RadzenPanelMenuItem Path="/" Text="Главная" Icon="home" />
                <AuthorizeView Roles="@($"{AppRole.ADMINISTRATOR_TYPE},{AppRole.OPERATOR_TYPE}")">
                    <Authorized>
                        <RadzenPanelMenuItem Path="/campaigns" Text="Кампании" Icon="graph_2" />
                        <RadzenPanelMenuItem Path="/operations" Text="Операции" Icon="construction" />
                        <RadzenPanelMenuItem Text="Объекты" Icon="business_center">
                            <RadzenPanelMenuItem Path="/objects/houses" Text="Жилищный фонд" />
                        </RadzenPanelMenuItem>
                        <RadzenPanelMenuItem Text="Справочники" Icon="book_2">
                            <RadzenPanelMenuItem Path="/registry/common" Text="Общие" />
                            <RadzenPanelMenuItem Path="/registry/private" Text="Частные" />
                        </RadzenPanelMenuItem>
                    </Authorized>
                </AuthorizeView>
                <AuthorizeView Roles="@AppRole.ADMINISTRATOR_TYPE">
                    <Authorized>
                        <RadzenPanelMenuItem Text="Администрирование" Icon="admin_panel_settings">
                            <RadzenPanelMenuItem Path="/management/users" Text="Пользователи" />
                        </RadzenPanelMenuItem>
                    </Authorized>
                </AuthorizeView>
            </RadzenPanelMenu>
            <RadzenPanelMenu>
                <AuthorizeView>
                    <Authorized>
                        <RadzenPanelMenuItem Path="/account/manage" Text="@context.User.Identity?.Name" />
                        <RadzenPanelMenuItem Path="/identity/logout" Text="Выйти" Icon="logout" />
                    </Authorized>
                    <NotAuthorized>
                        <RadzenPanelMenuItem Path="/account/login" Text="Вход" Icon="login" />
                    </NotAuthorized>
                </AuthorizeView>
            </RadzenPanelMenu>
        </RadzenStack>
    </RadzenSidebar>
    <RadzenBody>
        <RadzenNotification Style="position: absolute;top: 0; right: 0;" />
        @Body
        <div id="blazor-error-ui">
            Произошла непредвиденная ошибка
            <a href="" class="reload">Перезагрузить</a>
            <a class="dismiss">🗙</a>
        </div>
    </RadzenBody>
</RadzenLayout>

@code {
    bool sidebarExpanded = true;
    string? currentUrl;

    protected override void OnInitialized()
    {
        currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);

        NavigationManager.LocationChanged += OnLocationChanged;
    }

    public void Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
    }

    private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
    {
        currentUrl = NavigationManager.ToBaseRelativePath(e.Location);

        NotificationService.Messages.Clear();

        StateHasChanged();
    }
}
