<?php
require_once __DIR__ . '/config/config.php';
$q = isset($_GET['q']) ? trim($_GET['q']) : '';
$pageTitle = 'Search';
$results = [];
if ($q !== '') {
    $pdo = getDB();
    $term = '%' . $q . '%';
    $stmt = $pdo->prepare("SELECT id, name, slug, description FROM products WHERE is_active = 1 AND (name LIKE ? OR description LIKE ?) ORDER BY name");
    $stmt->execute([$term, $term]);
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content search-page">
    <div class="container">
        <h1>Search</h1>
        <form method="get" class="search-form-inline">
            <input type="text" name="q" value="<?php echo htmlspecialchars($q); ?>" placeholder="Search products...">
            <button type="submit" class="btn">Search</button>
        </form>
        <?php if ($q !== ''): ?>
        <?php if (empty($results)): ?>
        <p>No products found for "<?php echo htmlspecialchars($q); ?>".</p>
        <?php else: ?>
        <ul class="search-results">
            <?php foreach ($results as $r): ?>
            <li><a href="product-detail.php?slug=<?php echo urlencode($r['slug']); ?>"><?php echo htmlspecialchars($r['name']); ?></a></li>
            <?php endforeach; ?>
        </ul>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
