<?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
    $rows = get_schedule();
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      if (isset($_POST['add_row'])) {
        $rows[] = [
          'day_key' => (string) ($_POST['day_key'] ?? ''),
          'day_label' => (string) ($_POST['day_label'] ?? ''),
          'time_label' => (string) ($_POST['time_label'] ?? ''),
          'title' => (string) ($_POST['title'] ?? ''),
          'station' => (string) ($_POST['station'] ?? ''),
          'note' => (string) ($_POST['note'] ?? ''),
          'enabled' => true,
        ];
      }
      if (isset($_POST['delete_index'])) {
        $deleteIndex = (int) $_POST['delete_index'];
        if (isset($rows[$deleteIndex])) {
          unset($rows[$deleteIndex]);
        }
      }
      save_schedule($rows);
      header('Location: schedule.php');
      exit;
    }
    ?>
    <div class="card">
      <h1>Schedule</h1>
      <form method="post">
        <div class="two-col">
          <div><label>Day key (YYYY-MM-DD)</label><input type="date" name="day_key" required></div>
          <div><label>Day label</label><input type="text" name="day_label" placeholder="Fri 3 Apr 2026" required></div>
        </div>
        <div class="two-col">
          <div><label>Time label</label><input type="text" name="time_label" placeholder="18:00 - 19:00" required></div>
          <div><label>Title</label><input type="text" name="title" required></div>
        </div>
        <div class="two-col">
          <div><label>Station</label><input type="text" name="station" required></div>
          <div><label>Note</label><input type="text" name="note"></div>
        </div>
        <div class="toolbar"><button type="submit" name="add_row" value="1">Add Schedule Row</button></div>
      </form>
    </div>

    <div class="card table-wrap">
      <table>
        <thead><tr><th>Day</th><th>Time</th><th>Title</th><th>Station</th><th>Note</th><th></th></tr></thead>
        <tbody>
        <?php foreach ($rows as $i => $row): ?>
          <tr>
            <td><?= esc((string) ($row['day_label'] ?? '')) ?></td>
            <td><?= esc((string) ($row['time_label'] ?? '')) ?></td>
            <td><?= esc((string) ($row['title'] ?? '')) ?></td>
            <td><?= esc((string) ($row['station'] ?? '')) ?></td>
            <td><?= esc((string) ($row['note'] ?? '')) ?></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>
