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

$promoFile = __DIR__ . '/data/promo.json';
$promo = file_exists($promoFile) ? json_decode(file_get_contents($promoFile), true) : [];
if (!is_array($promo)) $promo = [];
$promo = array_merge([
  'enabled'=>false,
  'title'=>'',
  'start'=>'',
  'end'=>'',
  'prizes'=>[],
  'description'=>'',
  'image'=>'',
  'show_entry_form'=>true,
  'show_external_link'=>false,
  'external_url'=>'',
  'external_label'=>'Open Event Link',
], $promo);
$sent = !empty($_GET['sent']);
$error = trim((string)($_GET['err'] ?? ''));
$img = trim((string)$promo['image']);
$eventImage = '';
if ($img !== '') {
  if (preg_match('#^https?://#i', $img) || str_starts_with($img, '/')) $eventImage = $img;
  else $eventImage = 'img/content/' . ltrim($img, '/');
}
?>
<div class="main-wrap">
  <div class="content-panel">
    <div class="news-banner">
      <h1 class="news-banner-text">🎉 Events &amp; Promotions</h1>
    </div>
    <div class="vinyl-divider center-divider"></div>
    <?php if (empty($promo['enabled'])): ?>
      <div class="content-box center-text"><p>No events running right now. Check back soon.</p></div>
    <?php else: ?>
      <div class="neon-card news-card" style="text-align:left;">
        <h2 class="section-title"><?= htmlspecialchars($promo['title'] ?: 'Current Promotion') ?></h2>
        <?php if ($eventImage): ?>
          <img src="<?= htmlspecialchars($eventImage) ?>" alt="<?= htmlspecialchars($promo['title'] ?: 'Event image') ?>" style="max-width:220px;border:2px solid #4ff;border-radius:8px;margin-bottom:12px;">
        <?php endif; ?>
        <?php if (!empty($promo['start']) || !empty($promo['end'])): ?>
          <p><strong style="color:#4ff;">Dates:</strong> <?= htmlspecialchars(trim(($promo['start'] ?? '') . ' - ' . ($promo['end'] ?? ''))) ?></p>
        <?php endif; ?>
        <?php if (!empty($promo['description'])): ?>
          <p class="about-text"><?= nl2br(htmlspecialchars($promo['description'])) ?></p>
        <?php endif; ?>
        <?php if (!empty($promo['prizes']) && is_array($promo['prizes'])): ?>
          <h3 style="color:#4ff;">🏆 Prizes</h3>
          <ul><?php foreach ($promo['prizes'] as $p): ?><li><?= htmlspecialchars($p) ?></li><?php endforeach; ?></ul>
        <?php endif; ?>
        <?php if (!empty($promo['show_external_link']) && !empty($promo['external_url'])): ?>
          <p><a href="<?= htmlspecialchars($promo['external_url']) ?>" target="_blank" class="button radio-static"><?= htmlspecialchars($promo['external_label'] ?: 'Open Event Link') ?></a></p>
        <?php endif; ?>
      </div>
      <?php if (!empty($promo['show_entry_form'])): ?>
        <div class="vinyl-divider center-divider"></div>
        <div class="content-box">
          <?php if ($sent): ?><div class="msg success">Entry received. Good luck!</div><?php endif; ?>
          <?php if ($error !== ''): ?><div class="msg error"><?= htmlspecialchars($error) ?></div><?php endif; ?>
          <h3 class="section-title center-text">Enter Now</h3>
          <form method="post" action="send_promo_entry.php" class="form">
            <input type="text" name="name" class="input-field" placeholder="Your Name" required>
            <input type="email" name="email" class="input-field" placeholder="Your Email" required>
            <textarea name="message" class="input-field" rows="4" placeholder="Optional Message"></textarea>
            <button type="submit" class="button radio-static">Submit Entry</button>
          </form>
        </div>
      <?php endif; ?>
    <?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>
