<?php
include 'config.php'; // Include your database connection

$offset = isset($_GET['offset']) ? (int)$_GET['offset'] : 0;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
$category = isset($_GET['category']) ? $_GET['category'] : '';
$product = isset($_GET['product']) ? $_GET['product'] : '';

// Build the query
$query = "SELECT * FROM tbl_stock_items WHERE 1=1";

// Filter by category if provided
if (!empty($category)) {
    $query .= " AND category = '" . mysqli_real_escape_string($con, $category) . "'";
}

// Filter by product name using LIKE for partial matches
if (!empty($product)) {
    $query .= " AND item_name LIKE '%" . mysqli_real_escape_string($con, $product) . "%'";
}

$query .= " ORDER BY id DESC LIMIT $offset, $limit";

$result = mysqli_query($con, $query);

if ($result && mysqli_num_rows($result) > 0) {
    while ($product = mysqli_fetch_assoc($result)) {
        $pcat = $product['category'];

        // Fetch category name
        $catQuery = "SELECT * FROM tbl_categories WHERE id = '$pcat'";
        $catResult = mysqli_query($con, $catQuery);
        $catRow = mysqli_fetch_assoc($catResult);

        // Set product image or default
        $productImg = !empty($product['product_img']) ? $product['product_img'] : 'default.png';

        echo '<div class="col-md-4 product-item" data-category="' . $pcat . '" data-product-id="' . $product['id'] . '">
                <div class="product-card">
                    <div class="product-image">
                        <img src="uploads/' . htmlspecialchars($productImg) . '" alt="' . htmlspecialchars($product['item_name']) . '">
                        <div class="item-code">CODE: ' . htmlspecialchars($product['p_code']) . '</div>
                    </div>
                    <div class="product-info">
                        <div class="product-category">' . htmlspecialchars($catRow['category_name']) . '</div>
                        <h2 class="product-title">' . htmlspecialchars($product['item_name']) . '</h2>
                        <div class="product-stock d-flex justify-content-between">
                                <span>Stock: ' . htmlspecialchars($product['stock']) . '</span>
                                <span>I.Code: '.htmlspecialchars($product['item_code']) .'</span>
                            </div>
                        <div class="product-bottom">
                        
                            <div class="product-price">
                                <span class="price-now">' . htmlspecialchars($product['price']) . '</span>
                            </div>
                            
                            <div class="qtyWidget_x1" data-min="1"  data-step="1" aria-label="Quantity selector">
                                <button type="button" class="qtyBtn_x1 qtyBtn--minus_x1" aria-label="Decrease quantity">−</button>
                                <input class="qtyInput_x1" type="text" inputmode="numeric" aria-label="Quantity" value="1" data-product-id="' . $product['id'] . '" />
                                <button type="button" class="qtyBtn_x1 qtyBtn--plus_x1" aria-label="Increase quantity">+</button>
                            </div>
                        </div>
                        <button class="product-button w-100 d-flex justify-content-center" 
                                onclick="addToCart(' . $product['id'] . ', \'' . htmlspecialchars($product['item_name']) . '\', ' . htmlspecialchars($product['price']) . ')">
                            <span class="button-text">Add to Cart</span>
                        </button>
                    </div>
                </div>
            </div>';
    }
} else {
    // No products found
    echo '';
}

mysqli_close($con); // Close the database connection
?>
