<?php
/**
 * News / Results Archive Page
 */
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/db.php';

$db = DB();
$pageTitle = 'News & Press Releases | IBVTE';
$pageDesc  = 'Keep up to date with the latest news, press releases, circulars, and results from the Indian Board of Vocational and Technical Education (IBVTE).';

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

try {
    $posts = $db->fetchAll("SELECT * FROM news_posts WHERE is_active = 1 ORDER BY created_at DESC");
} catch (Exception $e) {
    $posts = [];
}
?>

<div class="container py-5" style="min-height: 70vh;">
    <div class="row justify-content-center">
        <div class="col-lg-10">
            <div class="text-center mb-5">
                <h1 class="fw-bold mb-2" style="color: #3b0b7c;"><i class="fas fa-newspaper me-2"></i>News & Press Releases</h1>
                <p class="text-muted">Latest circulars, results, and official updates from IBVTE</p>
            </div>

            <?php if (!empty($posts)): ?>
                <div class="row g-4">
                    <?php foreach ($posts as $post): 
                        $cleanContent = strip_tags($post['content']);
                        $excerpt = mb_strimwidth($cleanContent, 0, 200, "...");
                        $detailUrl = 'news-detail.php?slug=' . urlencode($post['slug']);
                        $imageSrc = !empty($post['featured_image']) ? 'assets/uploads/news/' . $post['featured_image'] : null;
                    ?>
                        <div class="col-md-6 col-lg-4">
                            <div class="card h-100 border-0 shadow-sm hover-up overflow-hidden" style="border-radius: 15px; transition: transform 0.2s;">
                                <?php if ($imageSrc && in_array(strtolower(pathinfo($post['featured_image'], PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'png', 'gif'])): ?>
                                    <img src="<?= htmlspecialchars($imageSrc) ?>" class="card-img-top" style="height: 200px; object-fit: cover;" alt="Featured Image">
                                <?php else: ?>
                                    <div class="card-img-top bg-light d-flex align-items-center justify-content-center" style="height: 200px;">
                                        <i class="fas fa-bullhorn fa-3x text-primary opacity-50"></i>
                                    </div>
                                <?php endif; ?>
                                <div class="card-body p-4 d-flex flex-column">
                                    <div class="d-flex align-items-center justify-content-between mb-2">
                                        <span class="badge bg-primary text-uppercase"><?= htmlspecialchars($post['post_type']) ?></span>
                                        <small class="text-muted"><i class="far fa-calendar-alt me-1"></i> <?= date('d M, Y', strtotime($post['created_at'])) ?></small>
                                    </div>
                                    <h5 class="card-title fw-bold" style="color: #3b0b7c; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.4;">
                                        <?= htmlspecialchars($post['title']) ?>
                                    </h5>
                                    <p class="card-text text-muted small flex-grow-1" style="display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;">
                                        <?= htmlspecialchars($excerpt) ?>
                                    </p>
                                    <div class="mt-3">
                                        <a href="<?= $detailUrl ?>" class="btn btn-outline-primary btn-sm rounded-pill px-3">
                                            Read More <i class="fas fa-chevron-right ms-1"></i>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="text-center py-5">
                    <i class="far fa-folder-open fa-4x text-muted mb-3"></i>
                    <h4>No News / Press Releases Found</h4>
                    <p class="text-muted">There are no updates available at the moment. Please check back later.</p>
                    <a href="index.php" class="btn btn-primary rounded-pill px-4 mt-2">
                        <i class="fas fa-home me-2"></i> Go to Homepage
                    </a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<style>
    .hover-up:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 20px rgba(0,0,0,0.08) !important;
    }
</style>

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