<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Poll Results | AlmostHeaven Radio</title>
  <link rel="stylesheet" href="../css/style.css" />
</head>

<body>
  <?php include('../header.php'); ?>
  <?php include('../menu.php'); ?>

  <main class="main-container">
    <section class="schedule-preview">
      <h2 class="text-warning">Current Poll & Results</h2>

      <?php
        $poll_file = 'poll.json';
        $answers_file = 'poll_answers.json';

        $poll = file_exists($poll_file) ? json_decode(file_get_contents($poll_file), true) : null;
        $answers = file_exists($answers_file) ? json_decode(file_get_contents($answers_file), true) : [];

        // Count votes per option
        $vote_counts = [];
        if ($poll && isset($poll['options']) && is_array($poll['options'])) {
          foreach ($poll['options'] as $opt) {
            $vote_counts[$opt] = 0;
          }
          foreach ($answers as $entry) {
            $opt = $entry['option'];
            if (isset($vote_counts[$opt])) {
              $vote_counts[$opt]++;
            }
          }
        }
      ?>

      <?php if ($poll): ?>
        <div class="dj-slot now">
          <p><strong>Question:</strong> <?= htmlspecialchars($poll['question']) ?></p>
          <ul class="poll-results">
            <?php foreach ($vote_counts as $option => $count): ?>
              <li><?= htmlspecialchars($option) ?> — <strong><?= $count ?> vote<?= $count !== 1 ? 's' : '' ?></strong></li>
            <?php endforeach; ?>
          </ul>
        </div>
      <?php else: ?>
        <div class="dj-slot coming">
          <p>No poll data available.</p>
        </div>
      <?php endif; ?>
    </section>
  </main>

  <?php include('../footer.php'); ?>
</body>
</html>
