<?php
session_start();
require __DIR__ . '/includes/admin_lib.php';
include __DIR__ . '/includes/admin_header.php';

$djsFile = __DIR__ . '/../data/djs.json';
$djs = smurf_read_json($djsFile, []);

$action = $_GET['action'] ?? '';
$i = isset($_GET['i']) ? (int)$_GET['i'] : -1;

if ($action === 'delete' && $i >= 0) {
  if (!$CAN_EDIT) die("<h1>Access Denied</h1><p>You do not have permission to edit DJs.</p>");
  if (isset($djs[$i])) {
    array_splice($djs, $i, 1);
    smurf_write_json($djsFile, $djs);
  }
  header("Location: /admin/djs.php");
  exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (!$CAN_EDIT) die("<h1>Access Denied</h1><p>You do not have permission to edit DJs.</p>");
  $name  = trim($_POST['name'] ?? '');
  $show  = trim($_POST['show'] ?? '');
  $image = trim($_POST['image'] ?? '');
  $bio   = trim($_POST['bio'] ?? '');

  if ($name !== '' && $image !== '') {
    $djs[] = ['name'=>$name,'show'=>$show,'image'=>$image,'bio'=>$bio];
    smurf_write_json($djsFile, $djs);
    $saved = true;
  }
}
?>
<section class="content-card">
  <h1 class="admin-title">🎙️ DJs</h1>
  <p style="opacity:.8;">Front-end reads: <code>/data/djs.json</code> • Images expected in <code>/img/djs/</code></p>

  <?php if (!empty($saved)): ?>
    <div class="msg success">Added.</div>
  <?php endif; ?>

  <div class="admin-wrap" style="margin-top:10px;">
    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;">Add DJ</h3>
      <form method="POST" class="form">
        <label>Name</label>
        <input class="input-field" name="name" required <?= $CAN_EDIT ? '' : 'disabled' ?>>

        <label>Show</label>
        <input class="input-field" name="show" <?= $CAN_EDIT ? '' : 'disabled' ?>>

        <label>Image filename (in /img/djs/)</label>
        <input class="input-field" name="image" placeholder="dj1.png" required <?= $CAN_EDIT ? '' : 'disabled' ?>>

        <label>Bio</label>
        <textarea class="input-field" name="bio" rows="6" <?= $CAN_EDIT ? '' : 'disabled' ?>></textarea>

        <?php if ($CAN_EDIT): ?>
          <button class="admin-btn primary" type="submit" style="margin-top:12px;">Add DJ</button>
        <?php else: ?>
          <p style="opacity:.7;margin-top:10px;">View-only mode — editing disabled.</p>
        <?php endif; ?>
      </form>
    </section>

    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;">Current DJs</h3>

      <?php if (empty($djs)): ?>
        <p>No DJs yet.</p>
      <?php else: ?>
        <?php foreach ($djs as $idx => $dj): ?>
          <div class="content-card" style="text-align:left;">
            <div style="display:flex;justify-content:space-between;gap:10px;align-items:center;">
              <strong><?= smurf_h($dj['name'] ?? '') ?></strong>
              <?php if ($CAN_EDIT): ?>
                <a class="admin-btn" href="/admin/djs.php?action=delete&i=<?= $idx ?>" onclick="return confirm('Delete this DJ?');">Delete</a>
              <?php endif; ?>
            </div>
            <p style="margin:8px 0 0;opacity:.9;"><strong>Show:</strong> <?= smurf_h($dj['show'] ?? '') ?></p>
            <p style="margin:4px 0 0;opacity:.9;"><strong>Image:</strong> <?= smurf_h($dj['image'] ?? '') ?></p>
            <?php if (!empty($dj['bio'])): ?>
              <p style="margin:8px 0 0;white-space:pre-wrap;"><?= smurf_h($dj['bio']) ?></p>
            <?php endif; ?>
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
    </section>
  </div>
</section>

<?php include __DIR__ . '/includes/admin_footer.php'; ?>
