<?php
/**
 * Blog Page
 * Digital Marketplace
 */

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

$page_title = 'Blog';

// Get blog posts
$posts = get_blog_posts(12);

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

<section class="section">
    <div class="container">
        <div class="text-center mb-5" data-aos="fade-up">
            <h1><i class="fas fa-blog me-2"></i>Our Blog</h1>
            <p class="text-muted">Latest news, tips, and insights from our team</p>
        </div>
        
        <?php if (empty($posts)): ?>
            <div class="text-center py-5">
                <i class="fas fa-newspaper fa-3x text-muted mb-3"></i>
                <p class="text-muted">No blog posts available at the moment.</p>
            </div>
        <?php else: ?>
            <div class="row g-4">
                <?php foreach ($posts as $post): ?>
                <div class="col-md-6 col-lg-4" data-aos="fade-up">
                    <article class="blog-card">
                        <?php if ($post['featured_image']): ?>
                        <div class="blog-image">
                            <a href="<?php echo BASE_URL; ?>/blog-post.php?slug=<?php echo $post['slug']; ?>">
                                <img src="<?php echo UPLOAD_URL . '/' . $post['featured_image']; ?>" 
                                     alt="<?php echo sanitize($post['title']); ?>">
                            </a>
                        </div>
                        <?php endif; ?>
                        
                        <div class="blog-content">
                            <div class="blog-meta">
                                <span><i class="far fa-calendar me-1"></i><?php echo format_date($post['published_at']); ?></span>
                                <?php if ($post['author_name']): ?>
                                <span class="ms-3"><i class="far fa-user me-1"></i><?php echo sanitize($post['author_name']); ?></span>
                                <?php endif; ?>
                            </div>
                            
                            <h4 class="blog-title">
                                <a href="<?php echo BASE_URL; ?>/blog-post.php?slug=<?php echo $post['slug']; ?>">
                                    <?php echo sanitize($post['title']); ?>
                                </a>
                            </h4>
                            
                            <p class="blog-excerpt">
                                <?php echo truncate(strip_tags($post['excerpt'] ?? $post['content']), 150); ?>
                            </p>
                            
                            <a href="<?php echo BASE_URL; ?>/blog-post.php?slug=<?php echo $post['slug']; ?>" class="btn btn-outline-primary btn-sm">
                                Read More <i class="fas fa-arrow-right ms-1"></i>
                            </a>
                        </div>
                    </article>
                </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

<style>
.blog-card {
    background: var(--gradient-card);
    border: 1px solid var(--dark-lighter);
    border-radius: var(--radius);
    overflow: hidden;
    transition: var(--transition);
    height: 100%;
}

.blog-card:hover {
    border-color: var(--primary);
    transform: translateY(-5px);
}

.blog-image {
    height: 200px;
    overflow: hidden;
}

.blog-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: var(--transition);
}

.blog-card:hover .blog-image img {
    transform: scale(1.05);
}

.blog-content {
    padding: 1.5rem;
}

.blog-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1rem;
    font-size: 0.875rem;
    color: var(--gray-500);
}

.blog-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.blog-title a {
    color: var(--white);
    text-decoration: none;
    transition: var(--transition);
}

.blog-title a:hover {
    color: var(--primary);
}

.blog-excerpt {
    color: var(--gray-400);
    margin-bottom: 1rem;
    line-height: 1.6;
}
</style>

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