<?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
    $stations = get_stations();
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      if (isset($_POST['add_station'])) {
        $stations[] = [
          'name' => (string) ($_POST['name'] ?? ''),
          'website' => (string) ($_POST['website'] ?? ''),
          'stream' => (string) ($_POST['stream'] ?? ''),
          'logo' => (string) ($_POST['logo'] ?? ''),
          'note' => (string) ($_POST['note'] ?? ''),
          'enabled' => isset($_POST['enabled']),
        ];
      }
      if (isset($_POST['delete_index'])) {
        $deleteIndex = (int) $_POST['delete_index'];
        if (isset($stations[$deleteIndex])) {
          unset($stations[$deleteIndex]);
        }
      }
      save_stations($stations);
      $settings['stations'] = array_values($stations);
      save_settings($settings);
      header('Location: stations.php');
      exit;
    }
    ?>
    <div class="card">
      <h1>Stations</h1>
      <form method="post">
        <div class="two-col">
          <div><label>Station name</label><input type="text" name="name" required></div>
          <div><label>Website</label><input type="url" name="website"></div>
        </div>
        <div class="two-col">
          <div><label>Stream URL</label><input type="url" name="stream"></div>
          <div><label>Logo path</label><input type="text" name="logo" value="assets/stations/"></div>
        </div>
        <label>Note</label><input type="text" name="note">
        <label><input type="checkbox" name="enabled" checked> Enabled</label>
        <div class="toolbar"><button type="submit" name="add_station" value="1">Add Station</button></div>
      </form>
    </div>

    <div class="card table-wrap">
      <table>
        <thead><tr><th>Name</th><th>Website</th><th>Stream</th><th>Enabled</th><th></th></tr></thead>
        <tbody>
        <?php foreach ($stations as $i => $station): ?>
          <tr>
            <td><?= esc((string) ($station['name'] ?? '')) ?></td>
            <td><?= esc((string) ($station['website'] ?? '')) ?></td>
            <td><?= esc((string) ($station['stream'] ?? '')) ?></td>
            <td><?= !empty($station['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>
