@page
@model AsadiTools.Pages.Cart.CartIndexModel
@{ ViewData["Title"] = "سبد خرید"; Layout = "_Layout"; }

@if (!Model.Items.Any())
{
    <div class="min-h-[60vh] flex items-center justify-center">
        <div class="text-center py-20">
            <div class="text-6xl mb-4">🛒</div>
            <h2 class="text-xl font-bold text-gray-600 mb-2">سبد خرید شما خالی است</h2>
            <p class="text-gray-400 mb-6">قطعات مورد نیاز خود را از فروشگاه انتخاب کنید</p>
            <a href="/Shop" class="bg-blue-700 text-white px-6 py-3 rounded-xl font-bold hover:bg-blue-800 transition-colors">مشاهده فروشگاه</a>
        </div>
    </div>
}
else
{
    <div class="max-w-4xl mx-auto px-4 py-10">
        <h1 class="text-2xl font-extrabold text-gray-900 mb-8">سبد خرید</h1>
        <div class="grid md:grid-cols-3 gap-6">
            <!-- Items -->
            <div class="md:col-span-2 space-y-3">
                @foreach (var item in Model.Items)
                {
                    <div class="bg-white rounded-2xl border border-gray-100 p-5 flex items-center gap-4">
                        <div class="bg-blue-50 rounded-xl w-14 h-14 flex items-center justify-center text-2xl shrink-0">🔧</div>
                        <div class="flex-1 min-w-0">
                            <h3 class="font-bold text-gray-900 text-sm">@item.NameFa</h3>
                            @if (item.Sku != null) { <p class="text-xs text-gray-400">کد: @item.Sku</p> }
                            <p class="text-blue-700 font-bold mt-1">@SiteData.FormatPrice(item.Price)</p>
                        </div>
                        <!-- Qty -->
                        <div class="flex items-center gap-2 shrink-0">
                            <form method="post" asp-page-handler="UpdateQty">
                                @Html.AntiForgeryToken()
                                <input type="hidden" name="productId" value="@item.ProductId" />
                                <input type="hidden" name="qty" value="@(item.Qty - 1)" />
                                <button type="submit" class="w-8 h-8 rounded-lg border border-gray-200 flex items-center justify-center hover:bg-gray-50 text-lg">−</button>
                            </form>
                            <span class="w-8 text-center font-bold text-sm">@item.Qty</span>
                            <form method="post" asp-page-handler="UpdateQty">
                                @Html.AntiForgeryToken()
                                <input type="hidden" name="productId" value="@item.ProductId" />
                                <input type="hidden" name="qty" value="@(item.Qty + 1)" />
                                <button type="submit" class="w-8 h-8 rounded-lg border border-gray-200 flex items-center justify-center hover:bg-gray-50 text-lg">+</button>
                            </form>
                        </div>
                        <div class="text-left shrink-0">
                            <p class="font-bold text-gray-800 text-sm">@SiteData.FormatPrice(item.Subtotal)</p>
                            <form method="post" asp-page-handler="Remove" class="mt-1">
                                @Html.AntiForgeryToken()
                                <input type="hidden" name="productId" value="@item.ProductId" />
                                <button type="submit" class="text-red-400 hover:text-red-600 text-xs">حذف</button>
                            </form>
                        </div>
                    </div>
                }

                <form method="post" asp-page-handler="Clear" class="mt-2">
                    @Html.AntiForgeryToken()
                    <button type="submit" class="text-sm text-red-400 hover:text-red-600 transition-colors">🗑️ پاک کردن سبد خرید</button>
                </form>
            </div>

            <!-- Summary -->
            <div>
                <div class="bg-white rounded-2xl border border-gray-100 p-6 sticky top-24">
                    <h2 class="font-bold text-gray-900 mb-4 pb-3 border-b">خلاصه سفارش</h2>
                    <div class="space-y-2 text-sm mb-4">
                        <div class="flex justify-between text-gray-600">
                            <span>تعداد اقلام:</span>
                            <span>@Model.Items.Sum(i => i.Qty) عدد</span>
                        </div>
                        <div class="flex justify-between font-bold text-gray-900 text-base pt-2 border-t">
                            <span>جمع کل:</span>
                            <span class="text-blue-700">@SiteData.FormatPrice(Model.Total)</span>
                        </div>
                    </div>
                    <a href="/Checkout" class="block bg-blue-700 text-white text-center py-3.5 rounded-xl font-bold hover:bg-blue-800 transition-colors mb-3">ادامه و ثبت سفارش</a>
                    <a href="/Shop" class="block text-sm text-blue-600 text-center hover:underline">← ادامه خرید</a>
                </div>
            </div>
        </div>
    </div>
}
