@using MudBlazor.Internal
<MudText Typo="Typo.h4" Class="mb-4">Lista Faktur</MudText>

    <MudExpansionPanels Class="mb-4">
        <MudExpansionPanel Icon="@Icons.Material.Filled.FilterList"
                           Text="Filtry"
                           Expanded="true">
            <MudGrid>
                <MudItem xs="12" sm="6" md="4">
                    <MudTextField @bind-Value="filterRequest.Search"
                                  Label="Szukaj"
                                  Placeholder="Numer dokumentu, klient..."
                                  Immediate="true"
                                  DebounceInterval="500"
                                  OnDebounceIntervalElapsed="SearchInvoices"
                                  Clearable="true"/>
                </MudItem>

                <MudItem xs="12" sm="6" md="4">
                    <MudTextField @bind-Value="filterRequest.DocumentNo"
                                  Label="Numer dokumentu"
                                  Immediate="true"
                                  DebounceInterval="500"
                                  OnDebounceIntervalElapsed="SearchInvoices"
                                  Clearable="true"/>
                </MudItem>

                <MudItem xs="12" sm="6" md="4">
                    <MudTextField @bind-Value="filterRequest.ClientName"
                                  Label="Nazwa klienta"
                                  Immediate="true"
                                  DebounceInterval="500"
                                  OnDebounceIntervalElapsed="SearchInvoices"
                                  Clearable="true"/>
                </MudItem>

                <MudItem xs="12" sm="6" md="4">
                    <MudTextField @bind-Value="filterRequest.Type"
                                  Label="Typ dokumentu"
                                  Immediate="true"
                                  DebounceInterval="500"
                                  OnDebounceIntervalElapsed="SearchInvoices"
                                  Clearable="true"/>
                </MudItem>

                <MudItem xs="12" sm="6" md="4">
                    <MudButton Variant="Variant.Outlined"
                               OnClick="ClearFilters"
                               StartIcon="Icons.Material.Filled.Clear">
                        Wyczyść filtry
                    </MudButton>
                </MudItem>
            </MudGrid>
        </MudExpansionPanel>
    </MudExpansionPanels>

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

    <MudTable Items="invoices.Items"
              Dense="true"
              Hover="true"
              Loading="isLoading"
              LoadingProgressColor="Color.Info">
        <HeaderContent>
            <MudTh>Numer dokumentu</MudTh>
            <MudTh>Typ</MudTh>
            <MudTh>Klient</MudTh>
            <MudTh>Data rejestracji</MudTh>
            <MudTh>Data sprzedaży</MudTh>
            <MudTh>Wartość brutto</MudTh>
            <MudTh>Akcje</MudTh>
        </HeaderContent>
        <RowTemplate>
            <MudTd DataLabel="Numer dokumentu">@context.DocumentNo</MudTd>
            <MudTd DataLabel="Typ">@context.Type</MudTd>
            <MudTd DataLabel="Klient">@context.ClientName</MudTd>
            <MudTd DataLabel="Data rejestracji">@context.RegisterDate.ToShortDateString()</MudTd>
            <MudTd DataLabel="Data sprzedaży">@context.SellDate.ToShortDateString()</MudTd>
            <MudTd DataLabel="Wartość brutto">@context.TotalBrutto.ToString("C2")</MudTd>
            <MudTd DataLabel="Akcje">
                <MudIconButton Icon="@Icons.Material.Filled.Visibility"
                               Size="Size.Small"
                               OnClick="() => ViewInvoice(context.Id)"/>
                <MudIconButton Icon="@Icons.Material.Filled.Edit"
                               Size="Size.Small"
                               OnClick="() => EditInvoice(context.Id)"/>
                <MudIconButton Icon="@Icons.Material.Filled.Delete"
                               Size="Size.Small"
                               Color="Color.Error"
                               OnClick="() => DeleteInvoice(context.Id)"/>
            </MudTd>
        </RowTemplate>
        <NoRecordsContent>
            <MudText>Brak faktur do wyświetlenia</MudText>
        </NoRecordsContent>
        <LoadingContent>
            Ładowanie...
        </LoadingContent>
    </MudTable>

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