<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$slug = preg_replace('/[^a-zA-Z0-9\-]/', '', trim($_GET['slug'] ?? ''));
$dataFile = __DIR__ . '/data/pages.json';
$pages = file_exists($dataFile) ? json_decode(file_get_contents($dataFile), true) : [];
if (!is_array($pages)) $pages = [];
$page = ($slug !== '' && isset($pages[$slug])) ? $pages[$slug] : null;
?>
<div class="main-wrap">
  <div class="content-panel">
    <?php if (!$page || empty($page['published'])): ?>
      <div class="content-box center-text"><h1 class="section-title">Page Not Found</h1><p>This page is not published yet.</p></div>
    <?php else: ?>
      <div class="news-banner"><h1 class="news-banner-text"><?= htmlspecialchars($page['title'] ?? 'Page') ?></h1></div>
      <div class="vinyl-divider center-divider"></div>
      <?php
        $image = trim((string)($page['image'] ?? ''));
        $imageSrc = '';
        if ($image !== '') {
          if (preg_match('#^https?://#i', $image) || str_starts_with($image, '/')) $imageSrc = $image;
          else $imageSrc = 'img/content/' . ltrim($image, '/');
        }
      ?>
      <div class="neon-card news-card" style="text-align:left;">
        <?php if ($imageSrc): ?><img src="<?= htmlspecialchars($imageSrc) ?>" alt="<?= htmlspecialchars($page['title'] ?? 'Page image') ?>" style="max-width:260px;border:2px solid #4ff;border-radius:8px;margin-bottom:12px;"><?php endif; ?>
        <?php if (!empty($page['summary'])): ?><p class="about-text"><?= nl2br(htmlspecialchars($page['summary'])) ?></p><?php endif; ?>
        <div class="about-text"><?= $page['content'] ?? '' ?></div>
        <?php if (!empty($page['button_text']) && !empty($page['button_url'])): ?>
          <p style="margin-top:18px;"><a href="<?= htmlspecialchars($page['button_url']) ?>" class="button radio-static"><?= htmlspecialchars($page['button_text']) ?></a></p>
        <?php endif; ?>
      </div>
    <?php endif; ?>
    <div class="vinyl-divider center-divider"></div>
    <div class="content-box center-text"><a href="index.php" class="button radio-static">⬅ Back to Home</a></div>
    <?php include __DIR__ . '/includes/footer.php'; ?>
  </div>
</div>
