@attribute [Route(RouteConstants.Store.OrderDetail + "{id:long}")]
@* Injection is handled in code-behind *@

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

@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.Store.Orders" />
    </MudContainer>
}
else
{
    <MudContainer MaxWidth="MaxWidth.Large" Class="py-6">
        <PageHeader Title="@($"جزئیات سفارش #{_order.Id}")" BackHref="@RouteConstants.Store.Orders" />
        <MudPaper Elevation="2" Class="pa-4 rounded-lg">
            <MudStack Spacing="2">
                @if (_order.PaymentDate != null)
                {
                    <MudText Typo="Typo.body2" Class="mud-text-secondary">تاریخ پرداخت: @_order.PaymentDate.ToDateTime().MiladiToJalaliWithTime()</MudText>
                }
                else
                {
                    <MudText Typo="Typo.body2" Class="mud-text-secondary">تاریخ ثبت: @DateTime.Now.MiladiToJalaliWithTime()</MudText>
                }
                <MudText Typo="Typo.body2">وضعیت: @GetStatusText(_order.PaymentStatus)</MudText>
                <MudText Typo="Typo.body2">آدرس: @_order.UserAddressText</MudText>
                <MudDivider Class="my-2" />
                <MudHidden Breakpoint="Breakpoint.MdAndUp" Invert="true">
                <MudTable Items="_order.FactorDetails">
                    <HeaderContent>
                        <MudTh>محصول</MudTh>
                        <MudTh>قیمت واحد</MudTh>
                        <MudTh>تعداد</MudTh>
                        <MudTh>قیمت کل</MudTh>
                    </HeaderContent>
                    <RowTemplate>
                        <MudTd>
                            <MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center">
                                <AppImage Path="@GetProductImageUrl(context.ProductThumbnailPath)" Alt="@context.ProductTitle"
                                          ImgWidth="60" ImgHeight="60" Class="rounded-lg" ObjectFit="ObjectFit.Cover" />
                                <MudText>@context.ProductTitle</MudText>
                            </MudStack>
                        </MudTd>
                        <MudTd>@FormatPrice(context.UnitPrice ?? 0)</MudTd>
                        <MudTd>@context.Count</MudTd>
                        <MudTd>@FormatPrice((context.UnitPrice ?? 0) * (context.Count ?? 0))</MudTd>
                    </RowTemplate>
                </MudTable>
                </MudHidden>

                <MudHidden Breakpoint="Breakpoint.MdAndUp">
                    <MudStack Spacing="2">
                        @foreach (var it in _order.FactorDetails)
                        {
                            <MudPaper Class="pa-3 rounded-lg" Outlined="true">
                                <MudStack Spacing="1">
                                    <MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center">
                                        <AppImage Path="@GetProductImageUrl(it.ProductThumbnailPath)" Alt="@it.ProductTitle"
                                                  ImgWidth="60" ImgHeight="60" Class="rounded-lg" ObjectFit="ObjectFit.Cover" />
                                        <MudText>@it.ProductTitle</MudText>
                                    </MudStack>
                                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                                        <MudText Class="mud-text-secondary">@FormatPrice(it.UnitPrice ?? 0) واحد</MudText>
                                        <MudText>تعداد: @it.Count</MudText>
                                    </MudStack>
                                    <MudText>جمع: @FormatPrice((it.UnitPrice ?? 0) * (it.Count ?? 0))</MudText>
                                </MudStack>
                            </MudPaper>
                        }
                    </MudStack>
                </MudHidden>
                <MudDivider Class="my-2" />
                
                @* نمایش جزئیات مالی *@
                @{
                    var subtotal = _order.FactorDetails.Sum(s => (s.UnitPrice ?? 0) * (s.Count ?? 0));
                }
                <MudStack Spacing="1" Class="pa-2" Style="background-color: var(--mud-palette-background-grey); border-radius: 8px;">
                    <MudStack Row="true" Justify="Justify.SpaceBetween">
                        <MudText Typo="Typo.body2">جمع کالاها:</MudText>
                        <MudText Typo="Typo.body2">@FormatPrice(subtotal)</MudText>
                    </MudStack>
                    
                    @if (_order.VatInfo is not null)
                    {
                        var vatPercent = (int)(_order.VatInfo.VatRate * 100);
                        <MudStack Row="true" Justify="Justify.SpaceBetween">
                            <MudText Typo="Typo.body2" Class="mud-text-secondary">مالیات بر ارزش افزوده (@vatPercent%):</MudText>
                            <MudText Typo="Typo.body2" Class="mud-text-secondary">@FormatPrice(_order.VatInfo.VatAmount)</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.VatInfo.TotalAmount)</MudText>
                        </MudStack>
                    }
                    else
                    {
                        <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(subtotal)</MudText>
                        </MudStack>
                    }
                </MudStack>

                @* نمایش PV سفارش *@
                @if (_pvData != null && _pvData.TotalPv > 0)
                {
                    <MudDivider Class="my-2" />
                    <MudStack Spacing="1" Class="pa-2" Style="background-color: var(--mud-palette-background-grey); border-radius: 8px;">
                        <MudStack Row="true" AlignItems="AlignItems.Center" Spacing="1">
                            <MudIcon Icon="@Icons.Material.Filled.TrendingUp" Color="Color.Info" Size="Size.Small" />
                            <MudText Typo="Typo.subtitle2" Color="Color.Info">امتیاز فروش (PV)</MudText>
                        </MudStack>
                        @foreach (var product in _pvData.Products)
                        {
                            <MudStack Row="true" Justify="Justify.SpaceBetween">
                                <MudText Typo="Typo.caption" Class="mud-text-secondary">@product.ProductTitle × @product.Quantity</MudText>
                                <MudText Typo="Typo.caption">@string.Format("{0:N0}", product.TotalPv) PV</MudText>
                            </MudStack>
                        }
                        <MudDivider Class="my-1" />
                        <MudStack Row="true" Justify="Justify.SpaceBetween">
                            <MudText Typo="Typo.body2" Class="fw-bold">جمع PV:</MudText>
                            <MudText Typo="Typo.body2" Color="Color.Info" Class="fw-bold">@string.Format("{0:N0}", _pvData.TotalPv) PV</MudText>
                        </MudStack>
                    </MudStack>
                }
            </MudStack>
        </MudPaper>
    </MudContainer>
}
