@attribute [Route(RouteConstants.DiscountStore.Orders)]

<PageTitle>سفارش‌های فروشگاه تخفیفی</PageTitle>

<MudContainer MaxWidth="MaxWidth.Large" Class="py-6">
    <MudStack Spacing="3">
        <PageHeader Title="سفارش‌های تخفیفی" BackHref="@RouteConstants.DiscountStore.Products" />

        @if (_loading)
        {
            <LoadingState />
        }
        else if (_orders.Count == 0)
        {
            <MudAlert Severity="Severity.Info">هنوز سفارشی ثبت نشده است.</MudAlert>
            <MudButton Variant="Variant.Outlined" StartIcon="@Icons.Material.Filled.Store"
                       Href="@RouteConstants.DiscountStore.Products">
                مشاهده فروشگاه تخفیفی
            </MudButton>
        }
        else
        {
            <!-- Desktop Table -->
            <MudHidden Breakpoint="Breakpoint.MdAndUp" Invert="true">
                <MudPaper Elevation="1" Class="pa-4 rounded-lg">
                    <MudTable Items="_orders">
                        <HeaderContent>
                            <MudTh>شماره سفارش</MudTh>
                            <MudTh>تعداد اقلام</MudTh>
                            <MudTh>مبلغ کل</MudTh>
                            <MudTh>از کیف تخفیفی</MudTh>
                            <MudTh>درگاه</MudTh>
                            <MudTh>وضعیت پرداخت</MudTh>
                            <MudTh>وضعیت ارسال</MudTh>
                            <MudTh>تاریخ</MudTh>
                            <MudTh></MudTh>
                        </HeaderContent>
                        <RowTemplate>
                            <MudTd>@context.OrderNumber</MudTd>
                            <MudTd>@context.ItemsCount</MudTd>
                            <MudTd>@FormatPrice(context.TotalPrice)</MudTd>
                            <MudTd>
                                @if (context.DiscountBalanceUsed > 0)
                                {
                                    <MudText Color="Color.Success">@FormatPrice(context.DiscountBalanceUsed)</MudText>
                                }
                                else
                                {
                                    <MudText Class="mud-text-secondary">—</MudText>
                                }
                            </MudTd>
                            <MudTd>@FormatPrice(context.GatewayAmount)</MudTd>
                            <MudTd>
                                <MudChip T="string" Size="Size.Small"
                                         Color="@DiscountOrderService.GetPaymentStatusColor(context.PaymentStatus)"
                                         Variant="Variant.Filled">
                                    @DiscountOrderService.GetPaymentStatusText(context.PaymentStatus)
                                </MudChip>
                            </MudTd>
                            <MudTd>
                                <MudChip T="string" Size="Size.Small"
                                         Color="@DiscountOrderService.GetDeliveryStatusColor(context.DeliveryStatus)"
                                         Variant="Variant.Outlined">
                                    @DiscountOrderService.GetDeliveryStatusText(context.DeliveryStatus)
                                </MudChip>
                            </MudTd>
                            <MudTd>@FormatDate(context.Created)</MudTd>
                            <MudTd>
                                <MudButton Size="Size.Small" Variant="Variant.Outlined" Color="Color.Primary"
                                           OnClick="() => ViewOrder(context.Id)">
                                    جزئیات
                                </MudButton>
                            </MudTd>
                        </RowTemplate>
                    </MudTable>
                </MudPaper>
            </MudHidden>

            <!-- Mobile Cards -->
            <MudHidden Breakpoint="Breakpoint.MdAndUp">
                <MudStack Spacing="2">
                    @foreach (var order in _orders)
                    {
                        <MudPaper Class="pa-3 rounded-lg cursor-pointer" Outlined="true"
                                  @onclick="() => ViewOrder(order.Id)">
                            <MudStack Spacing="1">
                                <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center">
                                    <MudText Typo="Typo.subtitle2">سفارش #@order.OrderNumber</MudText>
                                    <MudChip T="string" Size="Size.Small"
                                             Color="@DiscountOrderService.GetPaymentStatusColor(order.PaymentStatus)"
                                             Variant="Variant.Filled">
                                        @DiscountOrderService.GetPaymentStatusText(order.PaymentStatus)
                                    </MudChip>
                                </MudStack>
                                <MudStack Row="true" Justify="Justify.SpaceBetween">
                                    <MudText Typo="Typo.caption" Class="mud-text-secondary">@FormatDate(order.Created)</MudText>
                                    <MudChip T="string" Size="Size.Small"
                                             Color="@DiscountOrderService.GetDeliveryStatusColor(order.DeliveryStatus)"
                                             Variant="Variant.Outlined">
                                        @DiscountOrderService.GetDeliveryStatusText(order.DeliveryStatus)
                                    </MudChip>
                                </MudStack>
                                <MudDivider />
                                <MudStack Row="true" Justify="Justify.SpaceBetween">
                                    <MudText Typo="Typo.body2">@order.ItemsCount قلم</MudText>
                                    <MudText Typo="Typo.body2" Class="fw-semibold">@FormatPrice(order.TotalPrice) تومان</MudText>
                                </MudStack>
                                @if (order.DiscountBalanceUsed > 0)
                                {
                                    <MudText Typo="Typo.caption" Color="Color.Success">
                                        از کیف تخفیفی: @FormatPrice(order.DiscountBalanceUsed) تومان
                                    </MudText>
                                }
                            </MudStack>
                        </MudPaper>
                    }
                </MudStack>
            </MudHidden>

            @if (_totalPages > 1)
            {
                <MudStack AlignItems="AlignItems.Center" Class="mt-4">
                    <MudPagination Count="@_totalPages" Selected="@_currentPage"
                                   SelectedChanged="OnPageChanged" Color="Color.Primary"
                                   Variant="Variant.Outlined" />
                </MudStack>
            }
        }
    </MudStack>
</MudContainer>
