<?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
    $notice = '';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      $settings['site_status'] = (string) ($_POST['site_status'] ?? 'live');
      $settings['submissions_enabled'] = isset($_POST['submissions_enabled']);
      $settings['current_clue'] = max(1, (int) ($_POST['current_clue'] ?? 1));
      $settings['winner_announced'] = isset($_POST['winner_announced']);
      $settings['announcement_enabled'] = isset($_POST['announcement_enabled']);
      $settings['announcement_text'] = (string) ($_POST['announcement_text'] ?? '');
      $settings['timer_enabled'] = isset($_POST['timer_enabled']);
      $settings['timer_duration'] = max(0, (int) ($_POST['timer_duration'] ?? 900));
      $settings['manual_override'] = isset($_POST['manual_override']);
      $settings['event_name'] = (string) ($_POST['event_name'] ?? $settings['event_name']);
      $settings['headline'] = (string) ($_POST['headline'] ?? $settings['headline']);
      $settings['admin_username'] = (string) ($_POST['admin_username'] ?? ($settings['admin_username'] ?? 'admin'));
      if (!empty($_POST['admin_password'])) {
        $settings['admin_password_hash'] = password_hash((string) $_POST['admin_password'], PASSWORD_DEFAULT);
      }
      if (isset($_POST['start_timer'])) {
        $settings['timer_start'] = time();
      }
      if (isset($_POST['clear_winner'])) {
        $settings['winner_locked'] = false;
        $settings['winner_name'] = '';
        $settings['winner_time'] = 0;
        $settings['winner_announced'] = false;
      }
      if (isset($_POST['kicker_on'])) {
        $settings['kicker_active'] = true;
      }
      if (isset($_POST['kicker_off'])) {
        $settings['kicker_active'] = false;
      }
      save_settings($settings);
      log_event('Site control updated by admin.');
      $notice = 'Settings saved.';
    }
    ?>
    <div class="card">
      <h1>Site Control</h1>
      <?php if ($notice): ?><div class="banner success"><?= esc($notice) ?></div><?php endif; ?>
      <form method="post">
        <div class="two-col">
          <div class="panel">
            <h3>Event Status</h3>
            <label>Site status</label>
            <select name="site_status">
              <?php foreach (['live' => 'LIVE', 'coming_soon' => 'COMING SOON', 'offline' => 'OFFLINE'] as $value => $label): ?>
                <option value="<?= esc($value) ?>" <?= ($settings['site_status'] ?? 'live') === $value ? 'selected' : '' ?>><?= esc($label) ?></option>
              <?php endforeach; ?>
            </select>
            <label>Event name</label>
            <input type="text" name="event_name" value="<?= esc((string) ($settings['event_name'] ?? '')) ?>">
            <label>Headline</label>
            <input type="text" name="headline" value="<?= esc((string) ($settings['headline'] ?? '')) ?>">
            <label><input type="checkbox" name="submissions_enabled" <?= !empty($settings['submissions_enabled']) ? 'checked' : '' ?>> Enable submissions</label><br>
            <label><input type="checkbox" name="winner_announced" <?= !empty($settings['winner_announced']) ? 'checked' : '' ?>> Show winner banner</label><br>
            <label><input type="checkbox" name="manual_override" <?= !empty($settings['manual_override']) ? 'checked' : '' ?>> Manual override (force submissions open)</label><br>
            <label>Current clue number</label>
            <input type="number" min="1" name="current_clue" value="<?= (int) ($settings['current_clue'] ?? 1) ?>">
          </div>
          <div class="panel">
            <h3>Announcements & Timer</h3>
            <label><input type="checkbox" name="announcement_enabled" <?= !empty($settings['announcement_enabled']) ? 'checked' : '' ?>> Enable announcement bar</label>
            <label>Announcement text</label>
            <textarea name="announcement_text"><?= esc((string) ($settings['announcement_text'] ?? '')) ?></textarea>
            <label><input type="checkbox" name="timer_enabled" <?= !empty($settings['timer_enabled']) ? 'checked' : '' ?>> Enable timer</label>
            <label>Timer duration (seconds)</label>
            <input type="number" min="0" name="timer_duration" value="<?= (int) ($settings['timer_duration'] ?? 900) ?>">
            <div class="toolbar">
              <button type="submit" name="start_timer" value="1">Start Timer</button>
              <button type="submit" class="warn" name="kicker_on" value="1">Activate Kicker</button>
              <button type="submit" class="secondary" name="kicker_off" value="1">Disable Kicker</button>
              <button type="submit" class="danger" name="clear_winner" value="1">Clear Winner</button>
            </div>
          </div>
        </div>

        <div class="panel" style="margin-top:18px">
          <h3>Admin Credentials</h3>
          <label>Username</label>
          <input type="text" name="admin_username" value="<?= esc((string) ($settings['admin_username'] ?? 'admin')) ?>">
          <label>New password</label>
          <input type="password" name="admin_password" placeholder="Leave blank to keep current password">
        </div>

        <div class="toolbar"><button type="submit">Save All Settings</button></div>
      </form>
    </div>

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