@page "/discounts"
@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>
                <MudChip Color="Color.Info" Variant="Variant.Outlined">124 عدد</MudChip>
                <MudSpacer />
                <MudButton Variant="Variant.Filled"
                           DisableElevation="true"
                           StartIcon="@Icons.Material.Outlined.Add"
                           Color="Color.Secondary"
                           OnClick="@ViewModel.AddAsync"
                           class="my-auto">افزودن تخفیفــ جدید</MudButton>
            </MudStack>
            <MudPaper>
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             T="DiscountSDto" 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>
                        <TemplateColumn T="DiscountSDto" Title="کد تخفیفــ">
                            <CellTemplate>
                                @if (context.Item.HasCode)
                                {
                                    <p>@context.Item.Code</p>
                                }
                                else
                                {
                                    <p>بدون کد</p>
                                }
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="DiscountSDto" Title="تاریخ شروع">
                            <CellTemplate>
                                <p>@context.Item.StartDate.ToPersianDateTime().ToLongDateString()</p>
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="DiscountSDto" Title="تاریخ انقضا">
                            <CellTemplate>
                                <p>@context.Item.ExpireDate.ToPersianDateTime().ToLongDateString()</p>
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="DiscountSDto" Title="نوع تخفیف">
                            <CellTemplate>
                                <p>@context.Item.Type.ToDisplay()</p>
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="DiscountSDto" Title="نوع کسرشدن">
                            <CellTemplate>
                                <p>@context.Item.AmountType.ToDisplay()</p>
                            </CellTemplate>
                        </TemplateColumn>


                        <TemplateColumn T="DiscountSDto" Title="نوع کسرشدن">
                            <CellTemplate>
                                <p>@context.Item.AmountType.ToDisplay()</p>
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="DiscountSDto" Title="مقدار تخفیف">
                            <CellTemplate>
                                @if (context.Item.AmountType == DiscountAmountType.Percent)
                                {
                                    <p>@context.Item.DiscountPercent %</p>
                                }
                                else
                                {
                                    <p>@context.Item.DiscountAmount ریالـ</p>
                                }
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="DiscountSDto" Title="تعداد قابل استفاده">
                            <CellTemplate>
                                @if (context.Item.IsInfinity)
                                {
                                    <p>نامحدود</p>
                                }
                                else
                                {
                                    <p>@context.Item.Count</p>
                                }
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   Color="@Color.Info"
                                                   OnClick="async()=>await ViewModel.EditAsync(context.Item)" />
                                    <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.DeleteAsync(context.Item.Id)"
                                                   Color="@Color.Error" />
                                </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 DiscountPageViewModel ViewModel { get; set; }
    protected override async Task OnInitializedAsync()
    {
        ViewModel = new DiscountPageViewModel(NavigationManager, Snackbar, UserUtility, RestWrapper, DialogService);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}
