<?php
$pageTitle='Events — Almost Heaven Radio';
include __DIR__ . '/header.php';
include __DIR__ . '/menu.php';
require_once __DIR__ . '/includes/media_helpers.php';
$promoFile = __DIR__ . '/data/promo.json';
$promo = file_exists($promoFile) ? json_decode((string)file_get_contents($promoFile), true) : [];
if (!is_array($promo)) $promo = [];
$promo = array_merge([
  'enabled'=>false,'title'=>'','start'=>'','end'=>'','description'=>'','prizes'=>[],
  'image'=>'','show_entry_form'=>true,'show_external_link'=>false,'external_url'=>'','external_label'=>'Open External Events Site','pinned'=>false
], $promo);
$sent = !empty($_GET['sent']);
$error = $_GET['err'] ?? '';
$image = ah_media_src((string)($promo['image'] ?? ''), 'img/content');
?>
<main class="content-area"><div class="content-wrapper page-shell"><section class="content-card"><h1>🎉 Events & Promos</h1><p>What’s happening at Almost Heaven Radio.</p><?php if (empty($promo['enabled'])): ?><p>No promotions running right now. Check back soon.</p><?php else: ?><div class="feature-card" style="margin-top:18px;"><h3><?= htmlspecialchars($promo['title'] ?: 'Promotion') ?><?= !empty($promo['pinned']) ? ' 📌' : '' ?></h3><?php if ($image !== ''): ?><p><img src="<?= htmlspecialchars($image) ?>" alt="Event image" style="max-width:260px;width:100%;height:auto;border-radius:12px;"></p><?php endif; ?><?php if (!empty($promo['start']) || !empty($promo['end'])): ?><p class="text-muted"><strong>Dates:</strong> <?= htmlspecialchars((string)$promo['start']) ?><?= !empty($promo['end']) ? ' to ' . htmlspecialchars((string)$promo['end']) : '' ?></p><?php endif; ?><p><?= nl2br(htmlspecialchars((string)($promo['description'] ?? ''))) ?></p><?php if (!empty($promo['prizes']) && is_array($promo['prizes'])): ?><p><strong>Prizes:</strong> <?= htmlspecialchars(implode(' • ', array_map('strval', $promo['prizes']))) ?></p><?php endif; ?><?php if (!empty($promo['show_external_link']) && !empty($promo['external_url'])): ?><p class="feature-link-row"><a href="<?= htmlspecialchars((string)$promo['external_url']) ?>" target="_blank" rel="noopener"><?= htmlspecialchars((string)($promo['external_label'] ?: 'Open External Events Site')) ?> →</a></p><?php endif; ?></div><?php if (!empty($promo['show_entry_form'])): ?><section class="content-card" style="margin-top:18px;text-align:left;"><h3>🎟️ Enter Now</h3><?php if ($sent): ?><div class="alert alert-success">Entry received.</div><?php elseif ($error): ?><div class="alert alert-danger"><?= htmlspecialchars($error) ?></div><?php endif; ?><form method="post" action="send_promo_entry.php" class="comment-form-home"><div class="comment-form-row"><input type="text" name="name" placeholder="Your name" required><input type="email" name="email" placeholder="Your email" required><textarea name="message" placeholder="Tell us why you should win"></textarea><button type="submit" class="play-button">Submit Entry</button></div></form></section><?php endif; ?><?php endif; ?></section></div></main>
<?php include __DIR__ . '/footer.php'; ?>
