@page "/payments"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]

@inject IDialogService DialogService
@inject NavigationManager NavigationManager
@inject ISnackbar Snackbar
@inject IUserUtility UserUtility
@inject IRestWrapper RestWrapper

<MudStack class="h-full w-full p-8">
    <MudGrid>
        <MudItem xs="12">
            <MudStack Row="true" class="mb-5">
                <MudText Typo="Typo.h4">پرداختـــ ها</MudText>
                <MudSpacer />
            </MudStack>
            <MudPaper>
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             T="PaymentSDto" Items="@ViewModel.PageDto" CurrentPage="@ViewModel.CurrentPage"
                             RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                             SortMode="@SortMode.None" Groupable="false">

                    @* <ToolBarContent>
                        <MudTextField T="string" Placeholder="جست جو بر اساس کد پیگیری" Adornment="Adornment.Start" Immediate="true"
                                      Clearable="true"
                                      ValueChanged="@ViewModel.SearchChanged"
                                      AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" class="my-auto"
                                      OnAdornmentClick="@ViewModel.SearchAsync"></MudTextField>
                    </ToolBarContent> *@
                    <Columns>
                        <PropertyColumn Title="شماره فاکتور" Property="arg => arg.FactorNumber" />
                        <PropertyColumn Title="پرداخت کننده" Property="arg => arg.CustomerFullName" />
                        <PropertyColumn Title="شماره تماس" Property="arg => arg.CustomerPhoneNumber" />
                        <TemplateColumn T="PaymentSDto" Title="نوع پرداخت">

                            <CellTemplate>
                                <p>@context.Item.Type.ToDisplay()</p>
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn T="PaymentSDto" Title="وظعیت پرداخت">

                            <CellTemplate>
                                <p>@context.Item.Status.ToDisplay()</p>
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="PaymentSDto" Title="مبلغ سفارش ">

                            <CellTemplate>
                                <p>@context.Item.Amount.ToString("N0") ریالــ</p>
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="PaymentSDto" Title="تاریخ پرداخت">
                            <CellTemplate>
                                <p>@context.Item.CreatedAt.ToPersianDateTime().ToLongDateString()</p>
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.PanoramaFishEye"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   Color="@Color.Info"
                                                   OnClick="async()=>await ViewModel.ShowClicked(context.Item)" />
                                </MudStack>
                            </CellTemplate>
                        </TemplateColumn>
                    </Columns>
                    <PagerContent>
                        <MudStack Row="true" class="w-full">

                            <MudPagination Rectangular="true" Variant="Variant.Filled" Count="@ViewModel.PageCount"
                                           SelectedChanged="@ViewModel.ChangePageAsync" class="mx-auto my-4" />
                        </MudStack>

                    </PagerContent>
                </MudDataGrid>
            </MudPaper>
        </MudItem>
    </MudGrid>
</MudStack>

@code
{
    public PaymentsPageViewModel ViewModel { get; set; }
    protected override async Task OnInitializedAsync()
    {
        ViewModel = new PaymentsPageViewModel(NavigationManager, Snackbar, UserUtility, RestWrapper, DialogService);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}