<?php
session_start();
require __DIR__ . '/includes/admin_lib.php';
include __DIR__ . '/includes/admin_header.php';

$pollFile = __DIR__ . '/../data/poll.json';
$votesFile = __DIR__ . '/../data/poll_votes.json';

$poll = smurf_read_json($pollFile, ['question'=>'', 'options'=>[], 'show_on_home'=>1]);
$votes = smurf_read_json($votesFile, []);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (!$CAN_EDIT) die("<h1>Access Denied</h1><p>You do not have permission to edit the poll.</p>");

  $question = trim($_POST['question'] ?? '');
  $optionsRaw = trim($_POST['options'] ?? '');
  $options = array_values(array_filter(array_map('trim', preg_split("/\R+/", $optionsRaw))));
  $poll = ['question' => $question, 'options' => $options, 'show_on_home' => (isset($_POST['show_on_home']) ? 1 : 0)];

  smurf_write_json($pollFile, $poll);

  // reset votes if requested
  if (!empty($_POST['reset_votes'])) {
    smurf_write_json($votesFile, new stdClass());
    $votes = [];
  } else {
    $votes = smurf_read_json($votesFile, []);
  }

  $saved = true;
}

$optionsText = implode("\n", $poll['options'] ?? []);
?>
<section class="content-card">
  <h1 class="admin-title">📊 Poll</h1>
  <p style="opacity:.8;">Poll data: <code>/data/poll.json</code> • Votes: <code>/data/poll_votes.json</code></p>

  <?php if (!empty($saved)): ?>
    <div class="msg success">Saved.</div>
  <?php endif; ?>

  <form method="POST" class="form" style="text-align:left;">
    <label>Question</label>
    <input class="input-field" name="question" value="<?= smurf_h($poll['question'] ?? '') ?>" <?= $CAN_EDIT ? '' : 'disabled' ?>>

    <label>Options (one per line)</label>
    <textarea class="input-field" name="options" rows="8" <?= $CAN_EDIT ? '' : 'disabled' ?>><?= smurf_h($optionsText) ?></textarea>

    <?php if ($CAN_EDIT): ?>

      <label style="display:flex;align-items:center;gap:8px;margin-top:10px;">
        <input type="checkbox" name="show_on_home" value="1" <?= !empty($poll['show_on_home']) ? 'checked' : '' ?>>
        Show poll on Home Page
      </label>

      <label style="display:flex;align-items:center;gap:8px;margin-top:10px;">
        <input type="checkbox" name="reset_votes" value="1">
        Reset votes
      </label>

      <button class="admin-btn primary" type="submit" style="margin-top:12px;">Save Poll</button>
    <?php else: ?>
      <p style="opacity:.7;margin-top:10px;">View-only mode — editing disabled.</p>
    <?php endif; ?>
  </form>

  <div class="smurf-divider"></div>

  <h3 style="margin:0 0 10px;">Current vote totals</h3>
  <?php if (empty($poll['options'])): ?>
    <p>No options set yet.</p>
  <?php else: ?>
    <div class="content-card" style="text-align:left;">
      <?php foreach (($poll['options'] ?? []) as $opt): 
        $count = (int)($votes[$opt] ?? 0);
      ?>
        <p style="margin:6px 0;"><strong><?= smurf_h($opt) ?>:</strong> <?= $count ?></p>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</section>

<?php include __DIR__ . '/includes/admin_footer.php'; ?>
