<?php require_once __DIR__ . '/../config.php'; require_admin(); $settings = get_settings(); ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin Control Room</title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<div class="admin-shell">
  <aside class="admin-sidebar">
    <div class="brand">
      <img src="../assets/site-logo.png" alt="Logo">
      <div>
        <h2>Control Room</h2>
        <div class="meta">Broadcasturself Admin</div>
      </div>
    </div>
    <nav>
      <a href="index.php">Dashboard</a>
      <a href="site-control.php">Site Control</a>
      <a href="stations.php">Stations</a>
      <a href="schedule.php">Schedule</a>
      <a href="clues.php">Clues</a>
      <a href="answers.php">Answers</a>
      <a href="submissions.php">Submissions</a>
      <a href="dj-control.php">DJ Control</a>
      <a href="network.php">Network Sync</a>
      <a href="listeners.php">Listener Panel</a>
      <a href="poll.php">Poll Control</a>
      <a href="logout.php">Logout</a>
    </nav>
  </aside>
  <main class="admin-main">
    <?php
    $clues = get_clues();
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      if (isset($_POST['add_clue'])) {
        $clues[] = [
          'label' => (string) ($_POST['label'] ?? ''),
          'reveal_date' => (string) ($_POST['reveal_date'] ?? ''),
          'teaser' => (string) ($_POST['teaser'] ?? ''),
          'body' => (string) ($_POST['body'] ?? ''),
          'enabled' => isset($_POST['enabled']),
        ];
      }
      if (isset($_POST['delete_index'])) {
        $deleteIndex = (int) $_POST['delete_index'];
        if (isset($clues[$deleteIndex])) {
          unset($clues[$deleteIndex]);
        }
      }
      save_clues($clues);
      header('Location: clues.php');
      exit;
    }
    ?>
    <div class="card">
      <h1>Clues</h1>
      <form method="post">
        <div class="two-col">
          <div><label>Label</label><input type="text" name="label" required></div>
          <div><label>Reveal date</label><input type="date" name="reveal_date"></div>
        </div>
        <label>Teaser</label><input type="text" name="teaser">
        <label>Body</label><textarea name="body" required></textarea>
        <label><input type="checkbox" name="enabled" checked> Enabled</label>
        <div class="toolbar"><button type="submit" name="add_clue" value="1">Add Clue</button></div>
      </form>
    </div>

    <div class="card table-wrap">
      <table>
        <thead><tr><th>Label</th><th>Reveal</th><th>Teaser</th><th>Enabled</th><th></th></tr></thead>
        <tbody>
        <?php foreach ($clues as $i => $clue): ?>
          <tr>
            <td><?= esc((string) ($clue['label'] ?? '')) ?></td>
            <td><?= esc((string) ($clue['reveal_date'] ?? '')) ?></td>
            <td><?= esc((string) ($clue['teaser'] ?? '')) ?></td>
            <td><?= !empty($clue['enabled']) ? 'Yes' : 'No' ?></td>
            <td>
              <form method="post" class="inline">
                <input type="hidden" name="delete_index" value="<?= $i ?>">
                <button type="submit" class="danger">Delete</button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>

  </main>
</div>
</body>
</html>
