<?php
/**
 * Homepage Blog Section
 * Include this in your homepage (index.php) to display featured blogs
 * Usage: <?php require_once 'home_blog_section.php'; ?>
 */

require_once 'db_config.php';
require_once 'blog_helpers.php';

// Get featured blogs (show_on_home = 1, published, and past publish date)
$featuredBlogs = getBlogs($conn, [
    'show_on_home' => 1,
    'limit' => 6,
    'order' => 'b.publish_date DESC, b.created_at DESC'
]);
?>

<?php if (!empty($featuredBlogs)): ?>
<section class="featured-blogs-section">
     <div class="container">
        <div class="section-header">
            <h2>Latest from Our Blog</h2>
            <p>Stay updated with our latest insights and articles</p>
            <a href="blogs.php" class="view-all-btn">View All Posts →</a>
        </div>
        
        <div class="featured-blogs-grid">
            <?php foreach ($featuredBlogs as $blog): ?>
                <article class="blog-article">
                    <a href="blog.php?slug=<?php echo htmlspecialchars($blog['slug']); ?>" class="blog-card-link">
                        <div class="blog-image-wrapper">
                            <?php if ($blog['featured_image']): ?>
                                <img src="uploads/blog/<?php echo htmlspecialchars($blog['featured_image']); ?>" 
                                     alt="<?php echo htmlspecialchars($blog['title']); ?>"
                                     class="blog-image">
                            <?php else: ?>
                                <div class="blog-image-placeholder"></div>
                            <?php endif; ?>
                            <span class="category-badge"><?php echo htmlspecialchars($blog['category_name'] ?? 'Blog'); ?></span>
                        </div>
                    </a>
                    
                    <div class="blog-content-outside">
                        <h3>
                            <a href="blog.php?slug=<?php echo htmlspecialchars($blog['slug']); ?>">
                                <?php echo htmlspecialchars($blog['title']); ?>
                            </a>
                        </h3>
                        
                        <p class="blog-excerpt">
                            <?php echo htmlspecialchars(substr($blog['excerpt'] ?? $blog['content'], 0, 120)); ?>...
                        </p>
                        
                        <span class="blog-date">
                            <?php echo date('M d, Y', strtotime($blog['publish_date'] ?? $blog['created_at'])); ?>
                        </span>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
    </div>
</section>

<style>
    .featured-blogs-section {
        padding: 0;
        background: #fff;
        margin: 0 auto 80px;
        width: 100%;
        max-width: 1320px;
    }
    
    .featured-blogs-section .container {
        max-width: 1320px;
        margin: 0 auto;
        padding: 0 20px;
    }
    
    .section-header {
        text-align: center;
    }
    
    .section-header {
        margin-bottom: 60px;
    }
    
    .section-header h2 {
        font-size: 42px;
        color: #333;
        margin: 0;
        font-weight: 700;
        line-height: 1.2;
    }
    
    .section-header p {
        text-transform: uppercase;
        font-size: 14px;
        letter-spacing: 3px;
        color: #999;
        margin-bottom: 16px;
        font-weight: 500;
    }
    
    .view-all-btn {
        display: inline-block;
        color: #c76b8a;
        text-decoration: none;
        font-weight: 600;
        font-size: 15px;
        transition: all 0.3s;
    }
    
    .view-all-btn:hover {
        color: #b55975;
        margin-right: 5px;
    }
    
    .featured-blogs-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 30px;
        width: 100%;
    }
    
    .blog-article {
        display: flex;
        flex-direction: column;
        gap: 18px;
    }
    
    .blog-card-link {
        text-decoration: none;
        display: block;
    }
    
    .blog-image-wrapper {
        position: relative;
        overflow: hidden;
        border-radius: 8px;
        background: #ecf0f1;
        aspect-ratio: 3/2;
        width: 100%;
    }
    
    .blog-image {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.3s ease;
        display: block;
    }
    
    .blog-image-placeholder {
        width: 100%;
        height: 100%;
        background: linear-gradient(135deg, #ecf0f1 0%, #d5dbdb 100%);
    }
    
    .blog-card-link:hover .blog-image {
        transform: scale(1.05);
    }
    
    .category-badge {
        position: absolute;
        top: 12px;
        left: 12px;
        background: #3498db;
        color: white;
        padding: 6px 14px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 600;
        z-index: 1;
    }
    
    .blog-content-outside {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
    
    .blog-content-outside h3 {
        margin: 0;
        color: #2c3e50;
        font-size: 18px;
        line-height: 1.5;
        font-weight: 600;
    }
    
    .blog-content-outside h3 a {
        color: inherit;
        text-decoration: none;
        transition: color 0.3s;
    }
    
    .blog-content-outside h3 a:hover {
        color: #c76b8a;
    }
    
    .blog-excerpt {
        color: #666;
        font-size: 14px;
        margin: 0;
        line-height: 1.6;
    }
    
    .blog-date {
        color: #999;
        font-size: 13px;
        display: block;
    }
    
    @media (max-width: 1199px) {
        .featured-blogs-grid {
            grid-template-columns: repeat(2, 1fr);
            gap: 25px;
        }
        
        .featured-blogs-section {
            padding: 60px 5vw;
        }
    }
    
    @media (max-width: 768px) {
        .featured-blogs-section {
            padding: 50px 10px;
        }
        
        .featured-blogs-grid {
            grid-template-columns: 1fr;
            gap: 30px;
        }
        
        .section-header h2 {
            font-size: 28px;
        }
        
        .section-header p {
            font-size: 14px;
        }
    }
</style>
<?php endif; ?>
