
<?php
session_start(); require __DIR__ . '/includes/header.php';
$promoFile = __DIR__ . '/../data/promo.json'; $entriesFile = __DIR__ . '/../data/promo_entries.txt'; $settingsFile = __DIR__ . '/../data/settings.json';
$promo = admin_read_json($promoFile, ['enabled'=>false,'pinned'=>false,'title'=>'','start'=>'','end'=>'','prizes'=>[],'description'=>'']);
$settings = admin_read_json($settingsFile, oldies_read_settings());
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $CAN_EDIT) {
  if (isset($_POST['save_promo'])) { $promo['enabled']=!empty($_POST['enabled']); $promo['pinned']=!empty($_POST['pinned']); $promo['title']=trim($_POST['title'] ?? ''); $promo['start']=trim($_POST['start'] ?? ''); $promo['end']=trim($_POST['end'] ?? ''); $promo['description']=trim($_POST['description'] ?? ''); $promo['prizes']=array_values(array_filter(array_map('trim', preg_split('/
?
/', trim((string)($_POST['prizes'] ?? '')))))); $settings['events_show_in_nav']=!empty($_POST['events_show_in_nav']); admin_write_json($promoFile,$promo); admin_write_json($settingsFile,$settings); admin_redirect_self(['saved'=>1]); }
  if (isset($_POST['clear_entries'])) { file_put_contents($entriesFile, ''); admin_redirect_self(['cleared'=>1]); }
}
$entries = file_exists($entriesFile) ? array_reverse(array_filter(array_map('trim', file($entriesFile, FILE_IGNORE_NEW_LINES)))) : [];
?>
<section class="content-card"><h1 class="admin-title">🎉 Events & Promotions</h1><?php if (!empty($_GET['saved'])): ?><div class="msg success">Promotion saved.</div><?php endif; ?><?php if (!empty($_GET['cleared'])): ?><div class="msg success">Entries cleared.</div><?php endif; ?>
<div class="page-builder-grid">
<section class="content-card" style="text-align:left;"><form method="post" class="form"><input type="hidden" name="save_promo" value="1"><label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="enabled" <?= !empty($promo['enabled'])?'checked':'' ?>> Enable promotion</label><label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="pinned" <?= !empty($promo['pinned'])?'checked':'' ?>> Pin on homepage later</label><label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="events_show_in_nav" <?= !empty($settings['events_show_in_nav'])?'checked':'' ?>> Show Events in navigation</label><label>Title</label><input class="input-field" name="title" value="<?= admin_h($promo['title'] ?? '') ?>"><label>Start</label><input class="input-field" name="start" value="<?= admin_h($promo['start'] ?? '') ?>" placeholder="YYYY-MM-DD HH:MM"><label>End</label><input class="input-field" name="end" value="<?= admin_h($promo['end'] ?? '') ?>" placeholder="YYYY-MM-DD HH:MM"><label>Description</label><textarea class="input-field" name="description" rows="6"><?= admin_h($promo['description'] ?? '') ?></textarea><label>Prizes (one per line)</label><textarea class="input-field" name="prizes" rows="5"><?= admin_h(implode("
", $promo['prizes'] ?? [])) ?></textarea><button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>>Save Promotion</button></form></section>
<section class="content-card" style="text-align:left;"><h3 style="margin-top:0;">Current entries</h3><?php if (empty($entries)): ?><p>No promotion entries yet.</p><?php else: foreach ($entries as $line): $parts = explode('|', $line); ?><div class="nav-link-row"><div><strong><?= admin_h($parts[1] ?? 'Entry') ?></strong> — <?= admin_h($parts[2] ?? '') ?></div><div><?= admin_h($parts[3] ?? '') ?></div><div><?= admin_h($parts[4] ?? '') ?></div></div><?php endforeach; endif; ?><?php if ($CAN_EDIT): ?><form method="post" onsubmit="return confirm('Clear all entries?');"><input type="hidden" name="clear_entries" value="1"><button class="admin-btn" type="submit">Clear Entries</button></form><?php endif; ?></section>
</div></section>
<?php include __DIR__ . '/includes/footer.php'; ?>
