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

$slug = trim($_GET['slug'] ?? '');
$article = null;

if ($pdo && $slug) {
    $stmt = $pdo->prepare("SELECT id, title, slug, excerpt, body, image, created_at FROM news WHERE slug=? AND status='published' LIMIT 1");
    $stmt->execute([$slug]);
    $article = $stmt->fetch();
}

if (!$article) {
    require_once 'includes/header.php';
    echo '<section class="content-section"><div class="container text-center py-5"><h2>Article Not Found</h2><p class="text-muted">The article you are looking for does not exist or has been removed.</p><a href="news.php" class="btn-ghost mt-3" style="display:inline-flex;"><i class="ri-arrow-left-line"></i> Back to News</a></div></section>';
    require_once 'includes/footer.php';
    exit;
}

$page_title = htmlspecialchars($article['title'], ENT_QUOTES, 'UTF-8') . ' - SOL Christian Academy';
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> / <a href="news.php">News</a> / <?php e($article['title']); ?>
    </div>
    <h1 style="font-size:1.75rem;"><?php e($article['title']); ?></h1>
    <p><?php echo date('j F Y', strtotime($article['created_at'])); ?></p>
  </div>
</section>

<section class="content-section">
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-lg-8">
        <?php if ($article['image']): ?>
        <img src="<?php e($article['image']); ?>" alt="<?php e($article['title']); ?>" class="img-fluid rounded-4 shadow mb-4 w-100" style="max-height:400px;object-fit:cover;">
        <?php endif; ?>
        <div class="content-block" style="max-width:100%;">
          <?php echo $article['body']; ?>
        </div>
        <div class="mt-5">
          <a href="news.php" class="btn-ghost"><i class="ri-arrow-left-line"></i> Back to News</a>
        </div>
      </div>
    </div>
  </div>
</section>

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