<?php
require_once 'includes/header.php';
$cart_items = $_SESSION['cart'] ??[];
$subtotal = 0;
?>

<div class="bg-gray-100 py-3 border-b"><div class="container mx-auto px-4 text-sm font-bangla"><a href="index.php" class="text-blue-600">হোম</a> / শপিং কার্ট</div></div>

<section class="py-10 bg-gray-50 min-h-[60vh] pb-24">
    <div class="container mx-auto px-4">
        <h1 class="text-2xl font-bold font-bangla mb-8">আপনার <span class="text-blue-600">কার্ট</span></h1>

        <?php if (empty($cart_items)): ?>
            <div class="bg-white p-12 rounded-2xl text-center shadow-sm max-w-lg mx-auto">
                <i class="fa-solid fa-cart-arrow-down text-6xl text-gray-200 mb-4 block"></i>
                <h2 class="text-xl font-bold font-bangla mb-2">কার্ট ফাঁকা!</h2>
                <a href="index.php" class="bg-blue-600 text-white px-6 py-2 rounded-full font-bangla inline-block mt-4">কেনাকাটা করুন</a>
            </div>
        <?php else: ?>
            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
                <div class="lg:col-span-2 space-y-4">
                    <?php foreach ($cart_items as $cart_id => $item): 
                        $item_total = $item['price'] * $item['quantity'];
                        $subtotal += $item_total;
                    ?>
                        <div class="bg-white p-4 rounded-xl shadow-sm border flex items-center gap-4 relative">
                            <a href="cart_action.php?action=remove&id=<?= $cart_id ?>" class="absolute top-2 right-2 text-red-500 bg-red-50 w-8 h-8 flex items-center justify-center rounded-full"><i class="fa-solid fa-trash"></i></a>
                            
                            <img src="uploads/products/<?= $item['image'] ?: 'default.png' ?>" class="w-20 h-20 object-cover rounded-lg border bg-gray-50">
                            
                            <div class="flex-grow pr-8">
                                <h3 class="font-bold font-bangla text-gray-800 line-clamp-1"><?= htmlspecialchars($item['name']) ?></h3>
                                
                                <?php if(!empty($item['options'])): ?>
                                    <div class="flex flex-wrap gap-1 mt-1 mb-2">
                                        <?php foreach($item['options'] as $k => $v): ?>
                                            <span class="text-[10px] bg-gray-100 px-2 py-0.5 rounded font-bangla border"><strong class="text-gray-600"><?= $k ?>:</strong> <?= $v ?></span>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endif; ?>

                                <div class="flex items-center justify-between">
                                    <span class="font-bold text-blue-600 en-font">৳ <?= number_format($item['price'], 2) ?></span>
                                    <form action="cart_action.php" method="POST" class="flex border rounded overflow-hidden">
                                        <input type="hidden" name="action" value="update_qty"><input type="hidden" name="cart_id" value="<?= $cart_id ?>">
                                        <button type="button" onclick="this.nextElementSibling.stepDown(); this.form.submit();" class="px-2 bg-gray-100 hover:bg-gray-200">-</button>
                                        <input type="number" name="quantity" value="<?= $item['quantity'] ?>" min="1" class="w-10 text-center en-font outline-none text-sm" readonly>
                                        <button type="button" onclick="this.previousElementSibling.stepUp(); this.form.submit();" class="px-2 bg-gray-100 hover:bg-gray-200">+</button>
                                    </form>
                                    <span class="font-bold text-gray-800 en-font">৳ <?= number_format($item_total, 2) ?></span>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>

                <div class="lg:col-span-1">
                    <div class="bg-white p-6 rounded-xl shadow-sm border sticky top-24">
                        <h3 class="font-bold font-bangla mb-4 border-b pb-2">অর্ডার সামারি</h3>
                        <div class="flex justify-between font-bangla text-gray-600 mb-2"><span>সাবটোটাল:</span><span class="en-font font-bold">৳ <?= number_format($subtotal, 2) ?></span></div>
                        <div class="flex justify-between font-bangla text-gray-600 text-xs mb-4"><span>ডেলিভারি চার্জ:</span><span>চেকআউটে যোগ হবে</span></div>
                        <div class="flex justify-between border-t pt-4 font-bold text-lg text-blue-700"><span>মোট:</span><span class="en-font">৳ <?= number_format($subtotal, 2) ?></span></div>
                        <a href="checkout.php" class="block text-center mt-6 w-full bg-yellow-500 hover:bg-yellow-600 text-white py-3 rounded-lg font-bold font-bangla shadow-md">চেকআউট করুন</a>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    </div>
</section>
<?php require_once 'includes/footer.php'; ?>