<?php
$page_title = 'News - SOL Christian Academy';
$page_image = 'assets/images/hero-1.jpg';
$current_page = 'news.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

$page = max(1, (int)($_GET['page'] ?? 1));
$perPage = 6;
$offset = ($page - 1) * $perPage;

$articles = [];
$totalPages = 1;
if ($pdo) {
    $countStmt = $pdo->query("SELECT COUNT(*) FROM news WHERE status='published'");
    $total = (int)$countStmt->fetchColumn();
    $totalPages = max(1, ceil($total / $perPage));

    $stmt = $pdo->prepare("SELECT id, title, slug, excerpt, image, created_at FROM news WHERE status='published' ORDER BY created_at DESC LIMIT ? OFFSET ?");
    $stmt->execute([$perPage, $offset]);
    $articles = $stmt->fetchAll();
}
require_once 'includes/header.php';
?>

<section class="page-header page-header-image" style="--hero-bg: url('<?php echo $page_image; ?>');">
  <div class="container">
    <div class="breadcrumb-custom mb-2">
      <a href="index.php">Home</a> / News
    </div>
    <h1>Latest News</h1>
    <p>Stay updated with the latest happenings at SOL Christian Academy</p>
  </div>
</section>

<section class="content-section">
  <div class="container">
    <?php if (!empty($articles)): ?>
    <div class="row g-4">
      <?php foreach ($articles as $item): ?>
      <div class="col-md-6 col-lg-4">
        <div class="news-card">
          <?php if ($item['image']): ?>
          <img src="<?php e($item['image']); ?>" alt="<?php e($item['title']); ?>" loading="lazy">
          <?php endif; ?>
          <div class="news-card-body">
            <div class="date"><?php echo date('j M Y', strtotime($item['created_at'])); ?></div>
            <h5><?php e($item['title']); ?></h5>
            <p><?php e(truncate($item['excerpt'] ?: '', 120)); ?></p>
            <a href="news-detail.php?slug=<?php e($item['slug']); ?>" class="fw-semibold" style="color:var(--primary);text-decoration:none;">Read More &rarr;</a>
          </div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>

    <?php if ($totalPages > 1): ?>
    <nav class="mt-5">
      <ul class="pagination justify-content-center">
        <li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
          <a class="page-link" href="?page=<?php echo $page - 1; ?>">Previous</a>
        </li>
        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
        <li class="page-item <?php echo $i === $page ? 'active' : ''; ?>">
          <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
        </li>
        <?php endfor; ?>
        <li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
          <a class="page-link" href="?page=<?php echo $page + 1; ?>">Next</a>
        </li>
      </ul>
    </nav>
    <?php endif; ?>

    <?php else: ?>
    <div class="text-center py-5">
      <p class="text-muted fs-5">No news articles yet. Check back soon!</p>
    </div>
    <?php endif; ?>
  </div>
</section>

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