<?php
require_once 'config.php';

$pageTitle = 'Products - Performance Boat Parts';

require_once 'header.php';

$category = $_GET['category'] ?? '';
$search = $_GET['search'] ?? '';
$page = (int)($_GET['page'] ?? 1);
$perPage = 12;

$where = [];
$params = [];

if ($category) {
    $catStmt = $pdo->prepare("SELECT id FROM categories WHERE slug = ?");
    $catStmt->execute([$category]);
    $cat = $catStmt->fetch();
    if ($cat) {
        $where[] = "category_id = ?";
        $params[] = $cat['id'];
    }
}

if ($search) {
    $where[] = "(name LIKE ? OR description LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

$whereClause = $where ? "WHERE " . implode(" AND ", $where) : "";

$countStmt = $pdo->prepare("SELECT COUNT(*) FROM products $whereClause");
$countStmt->execute($params);
$totalProducts = $countStmt->fetchColumn();
$totalPages = ceil($totalProducts / $perPage);

$offset = ($page - 1) * $perPage;
$stmt = $pdo->prepare("SELECT * FROM products $whereClause ORDER BY name LIMIT $perPage OFFSET $offset");
$stmt->execute($params);
$products = $stmt->fetchAll();

$stmt = $pdo->query("SELECT * FROM categories");
$categories = $stmt->fetchAll();
?>

    <div class="breadcrumbs">
        <div class="container">
            <ul>
                <li><a href="index.php">Home</a></li>
                <li>Products<?= $category ? ' / ' . ucfirst($category) : '' ?></li>
            </ul>
        </div>
    </div>

    <section class="products">
        <div class="container">
            <div class="section-title">
                <h2>
                    <?php if ($category): ?>
                        <?= ucfirst($category) ?> Parts
                    <?php elseif ($search): ?>
                        Search Results for "<?= htmlspecialchars($search) ?>"
                    <?php else: ?>
                        All Products
                    <?php endif; ?>
                </h2>
                <p><?= $totalProducts ?> products found</p>
            </div>

            <div style="display: grid; grid-template-columns: 250px 1fr; gap: 40px;">
                <aside>
                    <div style="background: white; padding: 20px; border-radius: 10px; margin-bottom: 20px;">
                        <h3 style="margin-bottom: 15px;">Categories</h3>
                        <ul style="list-style: none;">
                            <li style="margin-bottom: 10px;"><a href="products.php" <?= !$category ? 'style="color: var(--primary); font-weight: bold;"' : '' ?>>All Products</a></li>
                            <?php foreach ($categories as $cat): ?>
                            <li style="margin-bottom: 10px;">
                                <a href="products.php?category=<?= $cat['slug'] ?>" <?= $category === $cat['slug'] ? 'style="color: var(--primary); font-weight: bold;"' : '' ?>>
                                    <?= htmlspecialchars($cat['name']) ?>
                                </a>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </aside>

                <div>
                    <?php if (count($products) === 0): ?>
                        <div class="alert alert-error">No products found. Try a different search.</div>
                    <?php else: ?>
                        <div class="product-grid">
                            <?php foreach ($products as $product): ?>
                            <div class="product-card">
                                <div class="product-image">
                                    <?php if ($product['featured']): ?>
                                    <span class="product-badge">Top Rated</span>
                                    <?php endif; ?>
                                    <span class="product-wishlist"><i class="fas fa-heart"></i></span>
                                    <a href="product.php?slug=<?= $product['slug'] ?>">
                                        <img src="<?= htmlspecialchars($product['image']) ?>" alt="<?= htmlspecialchars($product['name']) ?>">
                                    </a>
                                </div>
                                <div class="product-info">
                                    <h3><a href="product.php?slug=<?= $product['slug'] ?>"><?= htmlspecialchars($product['name']) ?></a></h3>
                                    <div class="product-rating">
                                        <span class="stars">
                                            <i class="fas fa-star"></i>
                                            <i class="fas fa-star"></i>
                                            <i class="fas fa-star"></i>
                                            <i class="fas fa-star"></i>
                                            <i class="fas fa-star-half-alt"></i>
                                        </span>
                                        <span class="count">(<?= rand(5, 50) ?>)</span>
                                    </div>
                                    <div class="product-price">$<?= number_format($product['price'], 2) ?></div>
                                    <div class="product-shipping">
                                        <i class="fas fa-truck"></i> <?= $product['price'] >= 200 ? 'Free Shipping' : 'Free shipping over $200' ?>
                                    </div>
                                    <div class="product-actions">
                                        <form method="POST" action="cart.php?action=add" style="flex:1;">
                                            <input type="hidden" name="product_id" value="<?= $product['id'] ?>">
                                            <button type="submit" class="btn btn-add-cart" style="width:100%;"><i class="fas fa-shopping-cart"></i> Add</button>
                                        </form>
                                        <a href="product.php?slug=<?= $product['slug'] ?>" class="btn btn-quick-view">Details</a>
                                    </div>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>

                        <?php if ($totalPages > 1): ?>
                        <div class="pagination">
                            <?php if ($page > 1): ?>
                            <a href="?page=<?= $page - 1 ?><?= $category ? '&category=' . $category : '' ?><?= $search ? '&search=' . $search : '' ?>">Prev</a>
                            <?php endif; ?>
                            
                            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                            <a href="?page=<?= $i ?><?= $category ? '&category=' . $category : '' ?><?= $search ? '&search=' . $search : '' ?>" <?= $i === $page ? 'class="active"' : '' ?>><?= $i ?></a>
                            <?php endfor; ?>
                            
                            <?php if ($page < $totalPages): ?>
                            <a href="?page=<?= $page + 1 ?><?= $category ? '&category=' . $category : '' ?><?= $search ? '&search=' . $search : '' ?>">Next</a>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>

<?php require_once 'footer.php'; ?>
