<?php include __DIR__ . '/../header.php'; ?>
<?php include __DIR__ . '/../menu.php'; ?>
<link rel="stylesheet" href="../css/style.css?v=1">

<main class="main-container">
  <section class="admin-card request-form">
    <h2 class="text-warning">🎶 Send a Song Request or Shoutout</h2>
    <form method="post" action="submit_request.php" class="admin-form">
      <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/requests/index.php') ?>">
      <input type="text" name="name" class="admin-input mb-2" placeholder="Your name" required>
      <input type="text" name="song" class="admin-input mb-2" placeholder="Song title or artist" required>
      <textarea name="message" class="admin-textarea mb-2" placeholder="Your message or shoutout" required></textarea>
      <button type="submit" class="btn btn-warning">📨 Send Request</button>
    </form>
  </section>

  <section class="admin-card recent-requests mt-5">
    <h2 class="text-warning">📻 Recent Requests</h2>
    <?php
      $file = __DIR__ . '/../data/requests.json';
      $requests = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
    ?>
    <?php if (!empty($requests)): ?>
      <div class="requests-grid">
        <?php foreach ($requests as $req): ?>
          <?php if (!empty($req['approved'])): ?>
            <div class="request-card mb-3">
              <h4><?= htmlspecialchars($req['name']) ?></h4>
              <p><strong>Song:</strong> <?= htmlspecialchars($req['song']) ?></p>
              <p><?= nl2br(htmlspecialchars($req['message'])) ?></p>
              <em class="text-muted">(<?= htmlspecialchars($req['timestamp']) ?>)</em>
              <?php if (!empty($req['reply'])): ?>
                <p class="reply"><strong>Reply:</strong> <?= htmlspecialchars($req['reply']) ?></p>
              <?php endif; ?>
            </div>
          <?php endif; ?>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <div class="request-card">
        <p>No requests yet. The mountain is quiet…</p>
      </div>
    <?php endif; ?>
  </section>
</main>

<?php include __DIR__ . '/../footer.php'; ?>
