@attribute [Route("/discount-store/order/{Id:long}")]

<PageTitle>جزئیات سفارش تخفیفی</PageTitle>

@if (!string.IsNullOrEmpty(_paymentMessage))
{
    <MudContainer MaxWidth="MaxWidth.Medium" Class="pt-4">
        <MudAlert Severity="@_paymentSeverity" Variant="Variant.Filled" Class="mb-2"
                  CloseIconClicked="() => _paymentMessage = null" ShowCloseIcon="true">
            @_paymentMessage
        </MudAlert>
    </MudContainer>
}

@if (_loading)
{
    <MudContainer MaxWidth="MaxWidth.Medium" Class="py-6">
        <LoadingState />
    </MudContainer>
}
else if (_order is null)
{
    <MudContainer MaxWidth="MaxWidth.Medium" Class="py-6">
        <EmptyState Icon="@Icons.Material.Filled.SearchOff"
                    Title="سفارش مورد نظر یافت نشد."
                    ActionText="بازگشت به لیست سفارش‌ها"
                    ActionHref="@RouteConstants.DiscountStore.Orders" />
    </MudContainer>
}
else
{
    <MudContainer MaxWidth="MaxWidth.Large" Class="py-6">
        <!-- Header -->
        <PageHeader Title="@($"سفارش #{_order.OrderNumber}")" BackHref="@RouteConstants.DiscountStore.Orders" />
        <MudStack Row="true" Spacing="2" Class="flex-wrap mb-4">
            <MudChip T="string" Color="@DiscountOrderService.GetPaymentStatusColor(_order.PaymentStatus)"
                     Variant="Variant.Filled">
                @DiscountOrderService.GetPaymentStatusText(_order.PaymentStatus)
            </MudChip>
            <MudChip T="string" Color="@DiscountOrderService.GetDeliveryStatusColor(_order.DeliveryStatus)"
                     Variant="Variant.Outlined">
                @DiscountOrderService.GetDeliveryStatusText(_order.DeliveryStatus)
            </MudChip>
        </MudStack>

        @if (_order.PaymentStatus == 2)
        {
            <MudAlert Severity="Severity.Error" Variant="Variant.Outlined" Class="mb-4">
                <MudText>پرداخت این سفارش ناموفق بود و سفارش منقضی شده است. موجودی رزرو شده آزاد شد.</MudText>
                <MudButton Variant="Variant.Text" Color="Color.Primary" Class="mt-2"
                           Href="@RouteConstants.DiscountStore.Products">
                    بازگشت به فروشگاه و ثبت سفارش جدید
                </MudButton>
            </MudAlert>
        }

        <MudPaper Elevation="2" Class="pa-4 rounded-lg">
            <MudStack Spacing="2">
                <MudText Typo="Typo.caption" Class="mud-text-secondary">
                    تاریخ ثبت: @FormatDate(_order.Created)
                </MudText>

                @* ── Address ── *@
                @if (_order.Address is not null)
                {
                    <MudText Typo="Typo.body2">
                        <b>آدرس:</b> @_order.Address.Title — @_order.Address.Address
                    </MudText>
                    @if (!string.IsNullOrWhiteSpace(_order.Address.PostalCode))
                    {
                        <MudText Typo="Typo.caption" Class="mud-text-secondary">کد پستی: @_order.Address.PostalCode</MudText>
                    }
                }

                <MudDivider Class="my-2" />

                @* ── Desktop: Table ── *@
                <MudHidden Breakpoint="Breakpoint.MdAndUp" Invert="true">
                    <MudText Typo="Typo.h6" Class="mb-2">اقلام سفارش</MudText>
                    <MudTable Items="_order.Items" Dense="true">
                        <HeaderContent>
                            <MudTh>محصول</MudTh>
                            <MudTh>قیمت واحد</MudTh>
                            <MudTh>تعداد</MudTh>
                            <MudTh>سقف تخفیف</MudTh>
                            <MudTh>تخفیف</MudTh>
                            <MudTh>قیمت نهایی</MudTh>
                        </HeaderContent>
                        <RowTemplate>
                            <MudTd>@context.Title</MudTd>
                            <MudTd>@FormatPrice(context.UnitPrice)</MudTd>
                            <MudTd>@context.Count</MudTd>
                            <MudTd>
                                @if (context.MaxDiscountPercent > 0)
                                {
                                    <MudChip T="string" Size="Size.Small" Color="Color.Error" Variant="Variant.Outlined">@context.MaxDiscountPercent%</MudChip>
                                }
                            </MudTd>
                            <MudTd>
                                @if (context.DiscountAmount > 0)
                                {
                                    <MudText Color="Color.Success">@FormatPrice(context.DiscountAmount)-</MudText>
                                }
                                else
                                {
                                    <MudText Class="mud-text-secondary">—</MudText>
                                }
                            </MudTd>
                            <MudTd>@FormatPrice(context.FinalPrice)</MudTd>
                        </RowTemplate>
                    </MudTable>
                </MudHidden>

                @* ── Mobile: Cards ── *@
                <MudHidden Breakpoint="Breakpoint.MdAndUp">
                    <MudText Typo="Typo.h6" Class="mb-2">اقلام سفارش</MudText>
                    <MudStack Spacing="2">
                        @foreach (var item in _order.Items)
                        {
                            <MudPaper Class="pa-3 rounded-lg" Outlined="true">
                                <MudStack Spacing="1">
                                    <MudText Typo="Typo.subtitle2">@item.Title</MudText>
                                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                                        <MudText Typo="Typo.body2" Class="mud-text-secondary">قیمت واحد:</MudText>
                                        <MudText Typo="Typo.body2">@FormatPrice(item.UnitPrice)</MudText>
                                    </MudStack>
                                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                                        <MudText Typo="Typo.body2" Class="mud-text-secondary">تعداد:</MudText>
                                        <MudText Typo="Typo.body2">@item.Count</MudText>
                                    </MudStack>
                                    @if (item.DiscountAmount > 0)
                                    {
                                        <MudStack Row="true" Justify="Justify.SpaceBetween">
                                            <MudText Typo="Typo.body2" Color="Color.Success">تخفیف (@item.MaxDiscountPercent%):</MudText>
                                            <MudText Typo="Typo.body2" Color="Color.Success">@FormatPrice(item.DiscountAmount)-</MudText>
                                        </MudStack>
                                    }
                                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                                        <MudText Typo="Typo.subtitle2">قیمت نهایی:</MudText>
                                        <MudText Typo="Typo.subtitle2" Color="Color.Primary">@FormatPrice(item.FinalPrice)</MudText>
                                    </MudStack>
                                </MudStack>
                            </MudPaper>
                        }
                    </MudStack>
                </MudHidden>

                <MudDivider Class="my-2" />

                @* ── Financial Summary ── *@
                <MudStack Spacing="1" Class="pa-3" Style="background-color: var(--mud-palette-background-grey); border-radius: 8px;">
                    <MudText Typo="Typo.h6" Class="mb-1">خلاصه مالی</MudText>
                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                        <MudText Typo="Typo.body2">جمع کل:</MudText>
                        <MudText Typo="Typo.body2">@FormatPrice(_order.TotalPrice)</MudText>
                    </MudStack>

                    @if (_order.DiscountBalanceUsed > 0)
                    {
                        <MudStack Row="true" Justify="Justify.SpaceBetween">
                            <MudText Typo="Typo.body2" Color="Color.Success">از کیف تخفیفی:</MudText>
                            <MudText Typo="Typo.body2" Color="Color.Success">@FormatPrice(_order.DiscountBalanceUsed)-</MudText>
                        </MudStack>
                    }

                    <MudDivider Class="my-1" />

                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                        <MudText Typo="Typo.subtitle1" Class="fw-bold">پرداخت درگاه:</MudText>
                        <MudText Typo="Typo.subtitle1" Color="Color.Primary" Class="fw-bold">
                            @FormatPrice(_order.GatewayAmount)
                        </MudText>
                    </MudStack>
                </MudStack>

                @* ── Tracking / Transaction ── *@
                @if (!string.IsNullOrWhiteSpace(_order.TrackingCode))
                {
                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                        <MudText Typo="Typo.body2">کد رهگیری:</MudText>
                        <MudText Typo="Typo.body2" Class="fw-semibold">@_order.TrackingCode</MudText>
                    </MudStack>
                }
                @if (!string.IsNullOrWhiteSpace(_order.TransactionId))
                {
                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                        <MudText Typo="Typo.caption" Class="mud-text-secondary">شناسه تراکنش:</MudText>
                        <MudText Typo="Typo.caption" Class="mud-text-secondary">@_order.TransactionId</MudText>
                    </MudStack>
                }

                @* ── Notes ── *@
                @if (!string.IsNullOrWhiteSpace(_order.Notes))
                {
                    <MudDivider Class="my-1" />
                    <MudText Typo="Typo.body2" Class="mud-text-secondary">
                        <b>توضیحات:</b> @_order.Notes
                    </MudText>
                }
                @if (!string.IsNullOrWhiteSpace(_order.AdminNotes))
                {
                    <MudAlert Severity="Severity.Info" Variant="Variant.Outlined" Dense="true">
                        <b>پیام مدیر:</b> @_order.AdminNotes
                    </MudAlert>
                }
            </MudStack>
        </MudPaper>
    </MudContainer>
}
