<?php $pageTitle='Poll — Almost Heaven Radio'; include __DIR__ . '/../header.php'; include __DIR__ . '/../menu.php'; $pollFile=__DIR__.'/../data/poll.json'; $votesFile=__DIR__.'/../data/poll_votes.json'; $poll=file_exists($pollFile)?json_decode((string)file_get_contents($pollFile),true):[]; if(!is_array($poll)) $poll=[]; $poll=array_merge(['question'=>'','options'=>[]],$poll); $votes=file_exists($votesFile)?json_decode((string)file_get_contents($votesFile),true):[]; if(!is_array($votes)) $votes=[]; $message=''; if($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['vote_option']) && in_array($_POST['vote_option'], (array)$poll['options'], true)){ $selected=trim((string)$_POST['vote_option']); $votes[$selected]=(int)($votes[$selected] ?? 0)+1; file_put_contents($votesFile, json_encode($votes, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); $message='Thanks for voting!'; }
$total=array_sum(array_map('intval',$votes)); ?>
<main class="content-area"><div class="content-wrapper page-shell"><section class="content-card"><h1>📊 Station Poll</h1><?php if($message): ?><div class="comment-item"><?= htmlspecialchars($message) ?></div><?php endif; ?><?php if(empty($poll['question']) || empty($poll['options'])): ?><p>No poll is live right now.</p><?php else: ?><p><strong><?= htmlspecialchars($poll['question']) ?></strong></p><form method="post" class="d-grid" style="gap:12px;margin-top:14px;"><?php foreach((array)$poll['options'] as $opt): ?><label class="comment-item" style="display:flex;justify-content:space-between;align-items:center;gap:14px;"><span><input type="radio" name="vote_option" value="<?= htmlspecialchars((string)$opt) ?>" required> <span style="margin-left:10px;"><?= htmlspecialchars((string)$opt) ?></span></span><strong><?= (int)($votes[$opt] ?? 0) ?></strong></label><?php endforeach; ?><div><button class="play-button" type="submit">Vote</button></div></form><p class="text-muted" style="margin-top:12px;">Total votes: <?= (int)$total ?></p><?php endif; ?></section></div></main>
<?php include __DIR__ . '/../footer.php'; ?>