<?php
// search.php - Search Blog Posts
// File location: /bidwheels.net/blog/search.php

require_once '../includes/functions.php';
require_once '../includes/header.php';

$keyword = isset($_GET['keyword']) ? sanitize($_GET['keyword']) : '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * BLOG_POSTS_PER_PAGE;

if (empty($keyword)) {
    redirect('/blog/index.php');
}

$posts = fetchAll("
    SELECT * FROM blog_posts 
    WHERE title LIKE '%$keyword%' 
    OR content LIKE '%$keyword%' 
    OR tags LIKE '%$keyword%'
    ORDER BY created_at DESC
    LIMIT $offset, " . BLOG_POSTS_PER_PAGE
);

$total = getCount('blog_posts', "title LIKE '%$keyword%' OR content LIKE '%$keyword%' OR tags LIKE '%$keyword%'");
$total_pages = ceil($total / BLOG_POSTS_PER_PAGE);
?>

<div class="container">
    <div class="blog-search-page" style="padding: 40px 0;">
        <div class="search-header" style="margin-bottom: 30px;">
            <h1>Search Results</h1>
            <p>Found <strong><?php echo $total; ?></strong> results for "<strong><?php echo htmlspecialchars($keyword); ?></strong>"</p>
        </div>
        
        <!-- Search Form -->
        <div class="search-form-container" style="background: white; padding: 20px; border-radius: 10px; margin-bottom: 30px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
            <form method="GET" action="" style="display: flex; gap: 10px;">
                <input type="text" name="keyword" class="form-control" placeholder="Search blog posts..." value="<?php echo htmlspecialchars($keyword); ?>" style="flex: 1; padding: 12px; border: 1px solid #ddd; border-radius: 5px;">
                <button type="submit" class="btn btn-primary" style="padding: 12px 25px; background: #e63946; color: white; border: none; border-radius: 5px; cursor: pointer;"><i class="fas fa-search"></i> Search</button>
            </form>
        </div>
        
        <!-- Results -->
        <?php if (count($posts) > 0): ?>
            <div class="blog-grid" style="display: grid; grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)); gap: 30px;">
                <?php foreach ($posts as $post): ?>
                    <div class="blog-card" style="background: white; border-radius: 10px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
                        <?php if ($post['featured_image']): ?>
                            <img src="<?php echo SITE_URL; ?>/uploads/blog/<?php echo $post['featured_image']; ?>" alt="<?php echo htmlspecialchars($post['title']); ?>" style="width: 100%; height: 200px; object-fit: cover;">
                        <?php endif; ?>
                        <div class="blog-content" style="padding: 20px;">
                            <h2 style="margin-bottom: 10px;"><?php echo htmlspecialchars($post['title']); ?></h2>
                            <div class="blog-meta" style="margin-bottom: 15px; color: #666; font-size: 14px;">
                                <span><i class="fas fa-calendar"></i> <?php echo date('M d, Y', strtotime($post['created_at'])); ?></span>
                                <?php if ($post['category']): ?>
                                    <span style="margin-left: 15px;"><i class="fas fa-tag"></i> <?php echo htmlspecialchars($post['category']); ?></span>
                                <?php endif; ?>
                            </div>
                            <p><?php echo substr(strip_tags($post['content']), 0, 150); ?>...</p>
                            <a href="post.php?id=<?php echo $post['id']; ?>" class="read-more" style="display: inline-block; margin-top: 15px; color: #e63946; text-decoration: none;">Read More <i class="fas fa-arrow-right"></i></a>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            
            <!-- Pagination -->
            <?php if ($total_pages > 1): ?>
                <div class="pagination" style="display: flex; justify-content: center; gap: 10px; margin-top: 40px;">
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <a href="?keyword=<?php echo urlencode($keyword); ?>&page=<?php echo $i; ?>" 
                           style="padding: 8px 15px; background: <?php echo $i == $page ? '#e63946' : 'white'; ?>; color: <?php echo $i == $page ? 'white' : '#333'; ?>; border: 1px solid #ddd; text-decoration: none; border-radius: 5px;">
                            <?php echo $i; ?>
                        </a>
                    <?php endfor; ?>
                </div>
            <?php endif; ?>
            
        <?php else: ?>
            <div class="no-results" style="text-align: center; padding: 60px; background: white; border-radius: 10px;">
                <i class="fas fa-search" style="font-size: 60px; color: #ccc; margin-bottom: 20px;"></i>
                <h3>No blog posts found</h3>
                <p>Try different keywords or browse all posts.</p>
                <a href="index.php" class="btn btn-primary" style="display: inline-block; margin-top: 15px; padding: 10px 25px; background: #e63946; color: white; text-decoration: none; border-radius: 5px;">View All Posts</a>
            </div>
        <?php endif; ?>
    </div>
</div>

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