<?php
/**
 * Search Results Page
 * Digital Product Store
 */

require_once __DIR__ . '/config/database.php';

$query = sanitize($_GET['q'] ?? '');
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 12;
$offset = ($page - 1) * $per_page;

$page_title = 'Search Results' . ($query ? ': ' . $query : '');

$products = [];
$total_products = 0;

if (!empty($query)) {
    // Search products
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM products WHERE status = 'active' AND (name LIKE ? OR description LIKE ? OR short_description LIKE ?)");
    $search_term = "%$query%";
    $stmt->execute([$search_term, $search_term, $search_term]);
    $total_products = $stmt->fetchColumn();
    
    $stmt = $pdo->prepare("SELECT p.*, c.name as category_name, c.slug as category_slug 
                          FROM products p 
                          LEFT JOIN categories c ON p.category_id = c.id 
                          WHERE p.status = 'active' AND (p.name LIKE ? OR p.description LIKE ? OR p.short_description LIKE ?)
                          ORDER BY p.created_at DESC 
                          LIMIT ? OFFSET ?");
    $stmt->execute([$search_term, $search_term, $search_term, $per_page, $offset]);
    $products = $stmt->fetchAll();
}

$total_pages = ceil($total_products / $per_page);

include __DIR__ . '/header.php';
?>

<section class="py-5">
    <div class="container">
        <h1 class="mb-4">Search Results</h1>
        
        <!-- Search Form -->
        <div class="card mb-4">
            <div class="card-body">
                <form action="" method="GET" class="d-flex">
                    <input type="text" name="q" class="form-control form-control-lg me-2" placeholder="Search products..." value="<?php echo htmlspecialchars($query); ?>">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="fas fa-search me-2"></i>Search
                    </button>
                </form>
            </div>
        </div>
        
        <?php if (empty($query)): ?>
        <div class="text-center py-5">
            <i class="fas fa-search fa-3x text-muted mb-3"></i>
            <p class="text-muted">Enter a search term to find products</p>
        </div>
        <?php elseif (empty($products)): ?>
        <div class="text-center py-5">
            <i class="fas fa-search fa-3x text-muted mb-3"></i>
            <h4>No products found</h4>
            <p class="text-muted">Try different keywords or browse our <a href="<?php echo BASE_URL; ?>/products.php">products</a></p>
        </div>
        <?php else: ?>
        <p class="text-muted mb-4">Found <?php echo $total_products; ?> result(s) for "<?php echo htmlspecialchars($query); ?>"</p>
        
        <div class="row">
            <?php foreach ($products as $product): ?>
            <div class="col-lg-3 col-md-4 col-sm-6 mb-4">
                <div class="card product-card h-100">
                    <div class="position-relative">
                        <?php if ($product['image']): ?>
                        <img src="<?php echo BASE_URL . '/' . $product['image']; ?>" class="card-img-top" alt="<?php echo htmlspecialchars($product['name']); ?>" style="height: 200px; object-fit: cover;">
                        <?php else: ?>
                        <img src="https://via.placeholder.com/300x200?text=No+Image" class="card-img-top" alt="No Image">
                        <?php endif; ?>
                        <?php if ($product['sale_price'] && $product['sale_price'] > 0): ?>
                        <span class="badge bg-danger position-absolute top-0 end-0 m-2">Sale</span>
                        <?php endif; ?>
                    </div>
                    <div class="card-body">
                        <h5 class="card-title"><?php echo htmlspecialchars($product['name']); ?></h5>
                        <p class="card-text text-muted"><?php echo htmlspecialchars(truncate($product['short_description'], 60)); ?></p>
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <?php if ($product['sale_price'] && $product['sale_price'] > 0): ?>
                                <span class="original-price me-2"><?php echo format_price($product['price']); ?></span>
                                <span class="sale-price"><?php echo format_price($product['sale_price']); ?></span>
                                <?php else: ?>
                                <span class="price"><?php echo format_price($product['price']); ?></span>
                                <?php endif; ?>
                            </div>
                            <a href="<?php echo BASE_URL; ?>/product.php?slug=<?php echo $product['slug']; ?>" class="btn btn-primary btn-sm">View</a>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Pagination -->
        <?php if ($total_pages > 1): ?>
        <nav class="mt-4">
            <ul class="pagination justify-content-center">
                <?php if ($page > 1): ?>
                <li class="page-item">
                    <a class="page-link" href="?q=<?php echo urlencode($query); ?>&page=<?php echo $page - 1; ?>">Previous</a>
                </li>
                <?php endif; ?>
                
                <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                <li class="page-item <?php echo $i === $page ? 'active' : ''; ?>">
                    <a class="page-link" href="?q=<?php echo urlencode($query); ?>&page=<?php echo $i; ?>"><?php echo $i; ?></a>
                </li>
                <?php endfor; ?>
                
                <?php if ($page < $total_pages): ?>
                <li class="page-item">
                    <a class="page-link" href="?q=<?php echo urlencode($query); ?>&page=<?php echo $page + 1; ?>">Next</a>
                </li>
                <?php endif; ?>
            </ul>
        </nav>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</section>

<?php include __DIR__ . '/footer.php'; ?>
