<?php require_once __DIR__ . '/config.php'; $settings = get_settings(); ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= esc($settings['event_name'] ?? ($settings['site_name'] ?? 'Broadcasturself')) ?></title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="site-shell">
  <div class="topbar">
    <div class="brand">
      <img src="<?= esc($settings['site_logo'] ?? 'assets/site-logo.png') ?>" alt="Logo">
      <div>
        <h1><?= esc($settings['site_name'] ?? 'Broadcasturself') ?></h1>
        <div class="meta"><?= esc($settings['site_tagline'] ?? 'Live radio event network') ?></div>
      </div>
    </div>
    <div class="live-pill"><span class="live-dot"></span><span><?= strtoupper(esc($settings['site_status'] ?? 'live')) ?></span></div>
  </div>
  <div class="nav">
    <a href="index.php">Home</a>
    <a href="stations.php">Participating Stations</a>
    <a href="schedule.php">Schedule</a>
    <a href="submit.php">Submit Entry</a>
    <a href="poll.php">Live Polls</a>
  </div>
    <?php
    $poll = load_poll();
    $pollMessage = '';
    if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['vote'])) {
      $vote = (string) $_POST['vote'];
      if (in_array($vote, $poll['options'] ?? [], true)) {
        $poll['votes'][] = $vote;
        save_poll($poll);
        $pollMessage = 'Thanks for voting.';
      }
    }
    $counts = [];
    foreach (($poll['votes'] ?? []) as $vote) {
      $counts[$vote] = ($counts[$vote] ?? 0) + 1;
    }
    ?>
    <div class="hero">
      <h2>Live Polls</h2>
      <p>Use live polls for audience interaction, song votes, and future event games.</p>
    </div>
    <?php if ($pollMessage): ?><div class="banner success"><?= esc($pollMessage) ?></div><?php endif; ?>
    <div class="card">
      <h3><?= esc($poll['question'] ?? 'No poll is active right now.') ?></h3>
      <?php if (!empty($poll['options'])): ?>
        <form method="post">
          <div class="toolbar">
            <?php foreach ($poll['options'] as $option): ?>
              <button type="submit" name="vote" value="<?= esc((string) $option) ?>"><?= esc((string) $option) ?></button>
            <?php endforeach; ?>
          </div>
        </form>
        <div class="table-wrap">
          <table>
            <thead><tr><th>Option</th><th>Votes</th></tr></thead>
            <tbody>
              <?php foreach ($poll['options'] as $option): ?>
                <tr><td><?= esc((string) $option) ?></td><td><?= (int) ($counts[$option] ?? 0) ?></td></tr>
              <?php endforeach; ?>
            </tbody>
          </table>
        </div>
      <?php else: ?>
        <p>No poll options have been published yet.</p>
      <?php endif; ?>
    </div>

  <div class="footer-note"><?= esc($settings['footer_note'] ?? 'Powered by Broadcasturself') ?></div>
</div>
</body>
</html>
