@page "/objects/houses"

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

@inherits SyncedPageBase<House>

@attribute [Authorize]

@inject HouseService HouseService
@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="House" Data="@data" IsLoading="@(state != SyncedPageState.Idle)" AllowFiltering="true" AllowPaging="true" ShowPagingSummary="true" PageSizeOptions=@(new int[] { 5, 10, 20, 30 }) AllowSorting="true">
                        <Columns>
                            <RadzenDataGridColumn TItem="House" Property="@nameof(House.Id)" Title="ID" SortOrder="SortOrder.Descending" Resizable="false" Width="100px" MaxWidth="100px" />
                            <RadzenDataGridColumn TItem="House" Property="@nameof(House.FiasId)" Title="ID ФИАС" />
                            <RadzenDataGridColumn TItem="House" Property="@nameof(House.HcsId)" Title="ID ГИС ЖКХ" />
                            <RadzenDataGridColumn TItem="House" Property="@nameof(House.ThirdPartyId)" Title="ID интеграции" />
                            <RadzenDataGridColumn TItem="House" Property="@nameof(House.IsMkd)" Title="МКД" Resizable="false" Width="100px" MaxWidth="100px">
                                <Template Context="house">
                                    @if (house.IsMkd)
                                    {
                                        <RadzenIcon Icon="check" />
                                    }
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="House" Property="@nameof(House.IsZhd)" Title="ЖД" Resizable="false" Width="100px" MaxWidth="100px">
                                <Template Context="house">
                                    @if (house.IsZhd)
                                    {
                                        <RadzenIcon Icon="check" />
                                    }
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="House" Property="@nameof(House.SyncedAt)" Title="Дата синхронизации" />
                        </Columns>
                    </RadzenDataGrid>
                </RadzenColumn>
            </RadzenRow>
        </RadzenStack>
    </Content>
</AuthorizedContent>

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

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

    protected override Task<IEnumerable<House>> GetDataAsync()
    {
        return HouseService.GetAllHousesAsync();
    }
}
