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

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

<MudStack class="h-full w-full overflow-x-hidden overflow-y-scroll md:p-5 lg:px-8">
    
    <MudHidden Breakpoint="Breakpoint.Xs">
        <MudGrid>
            <MudItem xs="12" sm="6" lg="3">
                <MudPaper class="m-2 rounded-md p-3" Elevation="2">
                    <MudText Typo="Typo.body1" class="mb-4">سفارشات امروز</MudText>
                    <MudText Typo="Typo.h3" Align="Align.Center" class="text-amber-600"><b>@ViewModel.PageDto.TodayOrdersCount</b></MudText>
                    <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
                </MudPaper>
            </MudItem>
            <MudItem xs="12" sm="6" lg="3">
                <MudPaper class="m-2 rounded-md p-3" Elevation="2">
                    <MudText Typo="Typo.body1" class="mb-4">سفارشات در انتظار تایید</MudText>
                    <MudText Typo="Typo.h3" Align="Align.Center" class="text-blue-600"><b>@ViewModel.PageDto.PayedOrdersCount</b></MudText>
                    <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
                </MudPaper>
            </MudItem>
            <MudItem xs="12" sm="6" lg="3">
                <MudPaper class="m-2 rounded-md p-3" Elevation="2">
                    <MudText Typo="Typo.body1" class="mb-4">سفارشات ارسال نشده</MudText>
                    <MudText Typo="Typo.h3" Align="Align.Center" class="text-lime-600"><b>@ViewModel.PageDto.UnSendOrdersCount</b></MudText>
                    <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
                </MudPaper>
            </MudItem>
            <MudItem xs="12" sm="6" lg="3">
                <MudPaper class="m-2 rounded-md p-3" Elevation="2">
                    <MudText Typo="Typo.body1" class="mb-4">مجموع سفارش ماه</MudText>
                    <MudText Typo="Typo.h3" Align="Align.Center" class="text-rose-600"><b>@ViewModel.PageDto.ThisMonthOrdersCount</b></MudText>
                    <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
                </MudPaper>
            </MudItem>
        </MudGrid>
    </MudHidden>
    
    <MudHidden Breakpoint="Breakpoint.SmAndUp">

        <MudStack Row="true" class="no-scrollbar w-screen overflow-x-scroll whitespace-nowrap">

            <MudPaper class="m-2 w-fit rounded-md p-3" Elevation="2">
                <MudText Typo="Typo.body1" class="mb-6 ml-16">سفارشات امروز</MudText>
                <MudText Typo="Typo.h3" Align="Align.Center" class="text-amber-600"><b>@ViewModel.PageDto.TodayOrdersCount</b></MudText>
                <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
            </MudPaper>

            <MudPaper class="m-2 w-fit rounded-md p-3" Elevation="2">
                <MudText Typo="Typo.body1" class="mb-6 ml-16">سفارشات در انتظار تایید</MudText>
                <MudText Typo="Typo.h3" Align="Align.Center" class="text-blue-600"><b>@ViewModel.PageDto.PayedOrdersCount</b></MudText>
                <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
            </MudPaper>

            <MudPaper class="m-2 w-fit rounded-md p-3" Elevation="2">
                <MudText Typo="Typo.body1" class="mb-6 ml-16">سفارشات ارسال نشده</MudText>
                <MudText Typo="Typo.h3" Align="Align.Center" class="text-lime-600"><b>@ViewModel.PageDto.UnSendOrdersCount</b></MudText>
                <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
            </MudPaper>

            <MudPaper class="m-2 w-fit rounded-md p-3" Elevation="2">
                <MudText Typo="Typo.body1" class="mb-6 ml-16">مجموع سفارش ماه</MudText>
                <MudText Typo="Typo.h3" Align="Align.Center" class="text-rose-600"><b>@ViewModel.PageDto.ThisMonthOrdersCount</b></MudText>
                <MudText Typo="Typo.h6" Align="Align.Center" class="-mt-1 mb-4"><b>عدد</b></MudText>
            </MudPaper>
        </MudStack>
    </MudHidden>

    <MudGrid>
        <MudItem xs="12">

            <MudHidden Breakpoint="Breakpoint.SmAndUp">
                <MudGrid class="mb-3" Row="true">
                    <MudItem xs="12" sm="6">
                        <MudTextField T="string" Placeholder="جست جو بر اساس کد فاکتور" OnClearButtonClick="@ViewModel.SearchAsync" 
                                      Adornment="Adornment.Start" 
                                      Immediate="true"
                                      Clearable="true"
                                      AdornmentIcon="@Icons.Material.Filled.Search"
                                      IconSize="Size.Medium"
                                      class="mx-2 my-auto"
                                      @bind-Value="@ViewModel.FactorCodeSearch"
                                      OnAdornmentClick="@ViewModel.SearchAsync"></MudTextField>
                    </MudItem>
                    <MudItem xs="12" sm="6">
                        <MudAutocomplete ToStringFunc="arg => arg.Title" ValueChanged="@ViewModel.SearchByOrderStatusAsync"
                                         Value="@ViewModel.OrderStatusSearch"
                                         SearchFunc="@ViewModel.OrderStatusSearchAsync"
                                         T="FilterOptionDto<OrderStatus>"
                                         class="-mt-0.5 mx-2"
                                         OnClearButtonClick="async () => await ViewModel.SearchByOrderStatusAsync(null)"
                                         Clearable="true"
                                         Label="وظعیت سفارش">
                            <ProgressIndicatorInPopoverTemplate>
                                <AutocompleteUi />
                            </ProgressIndicatorInPopoverTemplate>
                            <ItemTemplate Context="e">
                                <p>@e.Title</p>
                            </ItemTemplate>
                        </MudAutocomplete>
                    </MudItem>
                </MudGrid>
            </MudHidden>
            <MudPaper>
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             T="OrderSDto" Items="@ViewModel.MainOrders" CurrentPage="@ViewModel.MainGridCurrentPage"
                             RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                             SortMode="@SortMode.None" Groupable="false">

                    <ToolBarContent>
                        <MudGrid class="collapse md:visible">
                            <MudItem md="5">
                                <MudTextField T="string" Placeholder="جست جو بر اساس کد فاکتور" OnClearButtonClick="@ViewModel.SearchAsync" 
                                              Adornment="Adornment.Start" 
                                              Immediate="true"
                                              Clearable="true"
                                              AdornmentIcon="@Icons.Material.Filled.Search"
                                              IconSize="Size.Medium"
                                              class="my-auto"
                                              @bind-Value="@ViewModel.FactorCodeSearch"
                                              OnAdornmentClick="@ViewModel.SearchAsync"></MudTextField>
                            </MudItem>
                            <MudItem md="5">
                                <MudAutocomplete ToStringFunc="arg => arg.Title" ValueChanged="@ViewModel.SearchByOrderStatusAsync"
                                                 Value="@ViewModel.OrderStatusSearch"
                                                 SearchFunc="@ViewModel.OrderStatusSearchAsync"
                                                 T="FilterOptionDto<OrderStatus>"
                                                 class="my-auto"
                                                 OnClearButtonClick="async () => await ViewModel.SearchByOrderStatusAsync(null)"
                                                 Clearable="true"
                                                 Label="وظعیت سفارش">
                                    <ProgressIndicatorInPopoverTemplate>
                                        <AutocompleteUi />
                                    </ProgressIndicatorInPopoverTemplate>
                                    <ItemTemplate Context="e">
                                        <p>@e.Title</p>
                                    </ItemTemplate>
                                </MudAutocomplete>
                            </MudItem>
                            <MudItem md="2">
                                <MudSwitch class="mt-4" T="bool" Value="@ViewModel.ShowOrderBags" ValueChanged="async (flag) => await ViewModel.ChangeShowOrderBagsAsync(flag) " Label="نمایش سبد خرید ها" Color="Color.Info" />
                            </MudItem>
                        </MudGrid>
                    </ToolBarContent>
                    <Columns>
                        <PropertyColumn Title="کد سفارش" Property="arg => arg.FactorCode" />
                        <PropertyColumn Title="نام خریدار" Property="arg => arg.CustomerFullName" />
                        <TemplateColumn T="OrderSDto" Title="تاریخ سفارش">
                            <CellTemplate>
                                <p>@context.Item.OrderAt.ToPersianDateTime().ToLongDateString()</p>
                            </CellTemplate>
                        </TemplateColumn>

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

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

                            <CellTemplate>
                                <p>@context.Item.TotalPrice.ToString("N0") ریالــ</p>
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn T="OrderSDto" Title="وضعیت سفارش">

                            <CellTemplate>
                                @switch (context.Item.OrderStatus)
                                {
                                    case OrderStatus.OrderBag:
                                        <MudChip Variant="Variant.Text" class="-my-4" Color="Color.Warning">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;
                                    case OrderStatus.Submitted:
                                        <MudChip Variant="Variant.Text" class="-my-4" Color="Color.Warning">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;

                                    case OrderStatus.Paid:
                                        <MudChip Variant="Variant.Filled" class="-my-4" Color="Color.Info">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;
                                    case OrderStatus.Processing:
                                        <MudChip Variant="Variant.Filled" class="-my-4" Color="Color.Info">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;

                                    case OrderStatus.Delivered:
                                        <MudChip Variant="Variant.Text" class="-my-4" Color="Color.Success">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;

                                    case OrderStatus.Done:
                                        <MudChip Variant="Variant.Text" class="-my-4" Color="Color.Success">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;

                                    case OrderStatus.Canceled:
                                        <MudChip Variant="Variant.Text" class="-my-4" Color="Color.Error">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;

                                    default:
                                        <MudChip Variant="Variant.Text" class="-my-4" Color="Color.Warning">@context.Item.OrderStatus.ToDisplay()</MudChip>
                                        break;
                                }
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.RemoveRedEye"
                                                   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.MainGridPageCount"
                                           SelectedChanged="@ViewModel.ChangePageAsync" class="mx-auto my-4" />
                        </MudStack>

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

</MudStack>


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