@attribute [Route(RouteConstants.Store.Orders)]
@* Injection is handled in code-behind *@

<PageTitle>سفارشات من</PageTitle>

<MudContainer MaxWidth="MaxWidth.Large" Class="py-6">
    <PageHeader Title="سفارشات من" BackHref="@RouteConstants.Store.Products" />
    @if (_loading)
    {
        <LoadingState />
    }
    else if (_orders.Count == 0)
    {
        <MudAlert Severity="Severity.Info">هنوز سفارشی ثبت نکرده‌اید.</MudAlert>
    }
    else
    {
        <MudHidden Breakpoint="Breakpoint.MdAndUp" Invert="true">
        <MudTable Items="_orders" Dense="true">
            <HeaderContent>
                <MudTh>شناسه</MudTh>
                <MudTh>تاریخ</MudTh>
                <MudTh>وضعیت</MudTh>
                <MudTh>مبلغ</MudTh>
                <MudTh></MudTh>
            </HeaderContent>
            <RowTemplate>
                <MudTd>@context.Id</MudTd>
                <MudTd>
                    @if (context.PaymentDate != null)
                    {
                        @context.PaymentDate.ToDateTime().MiladiToJalaliWithTime()
                    }
                    else
                    {
                        <text>در انتظار پرداخت</text>
                    }
                </MudTd>
                <MudTd>@GetStatusText(context.PaymentStatus)</MudTd>
                <MudTd>@FormatPrice(context.FactorDetails.Sum(s=>(s.UnitPrice ?? 0) * (s.Count ?? 0)))</MudTd>
                <MudTd>
                    <MudButton Variant="Variant.Text" Href="@($"{RouteConstants.Store.OrderDetail}{context.Id}")" StartIcon="@Icons.Material.Filled.Receipt">جزئیات</MudButton>
                </MudTd>
            </RowTemplate>
        </MudTable>
        </MudHidden>

        <MudHidden Breakpoint="Breakpoint.MdAndUp">
            <MudStack Spacing="2">
                @foreach (var o in _orders)
                {
                    <MudPaper Class="pa-3 rounded-lg" Outlined="true">
                        <MudStack Spacing="1">
                            <MudStack Row="true" Justify="Justify.SpaceBetween">
                                <MudText>سفارش #@o.Id</MudText>
                                <MudText Class="mud-text-secondary">
                                    @if (o.PaymentDate != null)
                                    {
                                        @o.PaymentDate.ToDateTime().MiladiToJalaliWithTime()
                                    }
                                    else
                                    {
                                        <text>در انتظار پرداخت</text>
                                    }
                                </MudText>
                            </MudStack>
                            <MudStack Row="true" Justify="Justify.SpaceBetween">
                                <MudText>وضعیت: @GetStatusText(o.PaymentStatus)</MudText>
                                <MudText Color="Color.Primary">@FormatPrice(o.FactorDetails.Sum(s=>(s.UnitPrice ?? 0) * (s.Count ?? 0)))</MudText>
                            </MudStack>
                            <MudStack Row="true" Justify="Justify.FlexEnd">
                                <MudButton Size="Size.Small" Variant="Variant.Outlined" Href="@($"{RouteConstants.Store.OrderDetail}{o.Id}")" StartIcon="@Icons.Material.Filled.Receipt">جزئیات</MudButton>
                            </MudStack>
                        </MudStack>
                    </MudPaper>
                }
            </MudStack>
        </MudHidden>
    }
</MudContainer>
