<?php
include __DIR__ . '/includes/header.php';
require_once __DIR__ . '/includes/content_store.php';

$items = array_values(array_filter(ht_load_event_items(__DIR__), fn($item) => !empty($item['published'])));
$sent = !empty($_GET['sent']);
$error = trim((string)($_GET['err'] ?? ''));
?>
<div class="container content-box mt-5">
  <div class="text-center mb-5">
    <h1>Events &amp; Promotions</h1>
    <p class="subtext">What is happening around Home Time Radio.</p>
  </div>

  <?php if(empty($items)): ?>
    <div class="bg-light p-4 rounded">
      <h3>No active events or promotions right now.</h3>
      <p class="mb-0">Check back soon for the next giveaway, event or special announcement.</p>
    </div>
  <?php else: ?>
    <?php foreach($items as $item): ?>
      <?php $imageSrc = ht_asset_url((string)($item['image'] ?? ''), 'img/content'); ?>
      <div class="bg-light p-4 rounded mb-4">
        <div class="row g-3 align-items-start">
          <?php if($imageSrc !== ''): ?>
            <div class="col-md-auto"><img src="<?= htmlspecialchars($imageSrc) ?>" alt="<?= htmlspecialchars((string)($item['title'] ?? 'Event image')) ?>" style="width:140px;height:180px;object-fit:cover;border-radius:14px;"></div>
          <?php endif; ?>
          <div class="col">
            <h3><?= htmlspecialchars((string)($item['title'] ?? 'Event')) ?></h3>
            <?php if(!empty($item['start']) || !empty($item['end'])): ?><div style="opacity:.75;" class="mb-2"><?= htmlspecialchars((string)($item['start'] ?? '')) ?><?= !empty($item['end']) ? ' → ' . htmlspecialchars((string)$item['end']) : '' ?></div><?php endif; ?>
            <?php if(!empty($item['description'])): ?><p style="white-space:pre-wrap;"><?= htmlspecialchars((string)$item['description']) ?></p><?php endif; ?>
            <?php if(!empty($item['prizes']) && is_array($item['prizes'])): ?><h5>Prizes</h5><ul><?php foreach($item['prizes'] as $p): ?><li><?= htmlspecialchars((string)$p) ?></li><?php endforeach; ?></ul><?php endif; ?>
            <?php if(!empty($item['show_external_link']) && !empty($item['external_url'])): ?><a href="<?= htmlspecialchars((string)($item['external_url'])) ?>" target="_blank" rel="noopener" class="btn btn-dark"><?= htmlspecialchars((string)($item['external_label'] ?: 'Open External Events Site')) ?></a><?php endif; ?>
          </div>
        </div>
        <?php if(!empty($item['show_entry_form'])): ?>
          <hr>
          <h4>Enter this event</h4>
          <?php if($sent && (string)($_GET['event'] ?? '') === (string)($item['title'] ?? '')): ?><div class="alert alert-success">Entry received.</div><?php elseif($error && (string)($_GET['event'] ?? '') === (string)($item['title'] ?? '')): ?><div class="alert alert-danger"><?= htmlspecialchars($error) ?></div><?php endif; ?>
          <form method="post" action="/send_promo_entry.php">
            <input type="hidden" name="event_title" value="<?= htmlspecialchars((string)($item['title'] ?? '')) ?>">
            <div class="row g-3">
              <div class="col-md-6"><input type="text" class="form-control" name="name" placeholder="Your name" required></div>
              <div class="col-md-6"><input type="email" class="form-control" name="email" placeholder="Your email" required></div>
              <div class="col-12"><textarea class="form-control" name="message" rows="4" placeholder="Optional message"></textarea></div>
            </div>
            <button type="submit" class="btn btn-dark mt-3">Submit Entry</button>
          </form>
        <?php endif; ?>
      </div>
    <?php endforeach; ?>
  <?php endif; ?>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>
