@page
@model AsadiTools.Pages.Admin.Orders.OrdersIndexModel
@{ ViewData["Title"] = "سفارش‌ها"; Layout = "_AdminLayout"; }

<div class="p-6 md:p-8">
    <h1 class="text-2xl font-extrabold text-gray-900 mb-8">سفارش‌ها</h1>

    @if (!Model.Orders.Any())
    {
        <div class="text-center py-20 text-gray-400">
            <div class="text-5xl mb-4">📦</div>
            <p>هنوز سفارشی ثبت نشده</p>
        </div>
    }
    else
    {
        <div class="space-y-4">
            @foreach (var o in Model.Orders)
            {
                <div class="bg-white rounded-2xl border border-gray-100 p-6">
                    <div class="flex flex-wrap items-start justify-between gap-4 mb-4">
                        <div>
                            <div class="flex items-center gap-3 mb-1">
                                <span class="font-mono text-sm text-gray-500">@o.OrderNumber</span>
                                <span class="text-xs px-2 py-0.5 rounded-full font-medium @SiteData.OrderStatusBadge(o.Status)">
                                    @SiteData.OrderStatusLabel(o.Status)
                                </span>
                            </div>
                            <div class="font-bold text-gray-900">@o.CustomerName</div>
                            <div class="text-sm text-gray-500">@o.CustomerPhone</div>
                            @if (o.CustomerAddress != null) { <div class="text-xs text-gray-400 mt-1">@o.CustomerAddress</div> }
                        </div>
                        <div class="text-left">
                            <div class="text-xl font-extrabold text-blue-700">@SiteData.FormatPrice(o.Total)</div>
                            <div class="text-xs text-gray-400 mt-1">@SiteData.ToJalaliWithTime(o.CreatedAt)</div>
                        </div>
                    </div>

                    <!-- Items -->
                    <div class="bg-gray-50 rounded-xl p-4 mb-4">
                        <p class="text-xs text-gray-500 font-bold mb-2">اقلام سفارش:</p>
                        <ul class="space-y-1">
                            @foreach (var item in o.Items)
                            {
                                <li class="flex justify-between text-sm">
                                    <span class="text-gray-700">@item.ProductNameFa × @item.Quantity</span>
                                    <span class="text-gray-500">@SiteData.FormatPrice(item.Subtotal)</span>
                                </li>
                            }
                        </ul>
                    </div>

                    @if (o.Notes != null) { <p class="text-sm text-gray-500 mb-4">یادداشت: @o.Notes</p> }

                    <!-- Status update -->
                    <form method="post" asp-page-handler="UpdateStatus" class="flex items-center gap-3">
                        @Html.AntiForgeryToken()
                        <input type="hidden" name="id" value="@o.Id" />
                        <span class="text-sm text-gray-500 font-medium">وضعیت:</span>
                        <select name="status" class="border border-gray-200 rounded-lg px-3 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                            @foreach (var s in Enum.GetValues<AsadiTools.Models.OrderStatus>())
                            {
                                <option value="@s" selected="@(o.Status == s)">@SiteData.OrderStatusLabel(s)</option>
                            }
                        </select>
                        <button type="submit" class="bg-blue-700 text-white px-4 py-1.5 rounded-lg text-sm font-medium hover:bg-blue-800 transition-colors">
                            ذخیره
                        </button>
                    </form>
                </div>
            }
        </div>
    }
</div>
