<?php
require_once __DIR__ . '/config.php';

$category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
$limit = 10; // প্রতি স্ক্রলে কয়টি করে নতুন প্রোডাক্ট আসবে

if ($category_id > 0) {
    try {
        $prod_stmt = $pdo->prepare("SELECT * FROM products WHERE category_id = ? ORDER BY id DESC LIMIT ? OFFSET ?");
        // PDO-তে LIMIT এবং OFFSET ইন্টিজার হিসেবে বাইন্ড করতে হয়
        $prod_stmt->bindValue(1, $category_id, PDO::PARAM_INT);
        $prod_stmt->bindValue(2, $limit, PDO::PARAM_INT);
        $prod_stmt->bindValue(3, $offset, PDO::PARAM_INT);
        $prod_stmt->execute();
        $products = $prod_stmt->fetchAll(PDO::FETCH_ASSOC);

        if (count($products) > 0) {
            foreach ($products as $product) {
                ?>
                <a href="product_details.php?id=<?php echo $product['id']; ?>" class="product-card-link">
                    <div class="product-card">
                        <?php if ($product['discount_price'] > 0 && $product['price'] > 0): ?>
                            <span class="product-badge-discount">
                                <?php echo round((($product['price'] - $product['discount_price']) / $product['price']) * 100); ?>% ছাড়
                            </span>
                        <?php endif; ?>
                        
                        <i class="fa-regular fa-heart wishlist-btn" title="উইশলিস্টে রাখুন"></i>
                        
                        <div class="product-img-wrapper">
                            <?php if (!empty($product['image']) && file_exists("uploads/products/" . $product['image'])): ?>
                                <img src="uploads/products/<?php echo htmlspecialchars($product['image']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                            <?php else: ?>
                                <img src="https://via.placeholder.com/200?text=No+Image" alt="No Image Available">
                            <?php endif; ?>
                        </div>
                        
                        <h4><?php echo htmlspecialchars($product['name']); ?></h4>
                        
                        <div class="price-row">
                            <span class="current-price">
                                ৳<?php echo number_format($product['discount_price'] ?? $product['price']); ?>
                            </span>
                            <?php if ($product['discount_price'] > 0): ?>
                                <span class="old-price">
                                    ৳<?php echo number_format($product['price']); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                        
                        <div class="rating-row">
                            <span><i class="fa-solid fa-star"></i> 4.8 (120)</span>
                            <button class="add-to-cart-btn" title="কার্টে যোগ করুন">
                                <i class="fa-solid fa-cart-plus"></i>
                            </button>
                        </div>
                    </div>
                </a>
                <?php
            }
        }
    } catch (PDOException $e) {
        // এরর হলে নীরব থাকা ভালো যাতে ইউজার এক্সপেরিয়েন্স নষ্ট না হয়
    }
}
exit();