@page "/datainbox"
@using MudBlazor.Internal
@using DiunaBI.Application.DTOModels
@implements IDisposable

<PageTitle>Data Inbox</PageTitle>

<MudContainer MaxWidth="MaxWidth.ExtraExtraLarge">
    <MudExpansionPanels Class="mb-4">
        <MudExpansionPanel Icon="@Icons.Material.Filled.FilterList"
                           Text="Filters"
                           Expanded="true">
            <MudGrid AlignItems="Center">
                <MudItem xs="12" sm="6" md="4">
                    <MudTextField @bind-Value="filterRequest.Search"
                                  Label="Search"
                                  Placeholder="Name, source..."
                                  Immediate="true"
                                  DebounceInterval="500"
                                  OnDebounceIntervalElapsed="SearchDataInbox"
                                  Clearable="true"/>
                </MudItem>

                <MudItem xs="12" sm="6" md="4">
                </MudItem>

                <MudItem xs="12" sm="12" md="4" Class="d-flex justify-end">
                    <MudIconButton Icon="@Icons.Material.Filled.Clear"
                                   OnClick="ClearFilters"
                                   Color="Color.Default"
                                   Size="Size.Medium"
                                   Title="Clear filters"/>
                </MudItem>
            </MudGrid>
        </MudExpansionPanel>
    </MudExpansionPanels>

    <MudDivider Class="my-4"></MudDivider>

    <MudTable Items="dataInbox.Items"
              Dense="true"
              Hover="true"
              Loading="isLoading"
              LoadingProgressColor="Color.Primary"
              OnRowClick="@((TableRowClickEventArgs<DataInboxDto> args) => OnRowClick(args.Item))"
              T="DataInboxDto"
              Style="cursor: pointer;">
        <HeaderContent>
            <MudTh>Name</MudTh>
            <MudTh>Source</MudTh>
            <MudTh>Created At</MudTh>
        </HeaderContent>
        <RowTemplate Context="row">
            <MudTd DataLabel="Name"><div @oncontextmenu="@(async (e) => await OnRowRightClick(e, row))" @oncontextmenu:preventDefault="true">@row.Name</div></MudTd>
            <MudTd DataLabel="Source"><div @oncontextmenu="@(async (e) => await OnRowRightClick(e, row))" @oncontextmenu:preventDefault="true">@row.Source</div></MudTd>
            <MudTd DataLabel="Created At"><div @oncontextmenu="@(async (e) => await OnRowRightClick(e, row))" @oncontextmenu:preventDefault="true">@DateTimeHelper.FormatDateTime(row.CreatedAt)</div></MudTd>
        </RowTemplate>
        <NoRecordsContent>
            <MudText>No data inbox items to display</MudText>
        </NoRecordsContent>
        <LoadingContent>
            Loading...
        </LoadingContent>
    </MudTable>

    @if (dataInbox.TotalCount > 0)
    {
        <MudGrid Class="mt-4" AlignItems="Center.Center">
            <MudItem xs="12" sm="6">
                <MudText Typo="Typo.body2">
                    Results @((dataInbox.Page - 1) * dataInbox.PageSize + 1) - @Math.Min(dataInbox.Page * dataInbox.PageSize, dataInbox.TotalCount)
                    of @dataInbox.TotalCount
                </MudText>
            </MudItem>
            <MudItem xs="12" sm="6" Class="d-flex justify-end">
                <MudPagination Count="dataInbox.TotalPages"
                               Selected="dataInbox.Page"
                               SelectedChanged="OnPageChanged"
                               ShowFirstButton="true"
                               ShowLastButton="true"
                               Variant="Variant.Outlined"
                               />
            </MudItem>
        </MudGrid>
    }
</MudContainer>
