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

$dataFile = __DIR__ . '/../data/djs.json';
$djs = smurf_read_json($dataFile, []);
if (!is_array($djs)) $djs = [];

$action = $_GET['action'] ?? '';
$i = isset($_GET['i']) ? (int)$_GET['i'] : -1;
$edit = ($action === 'edit' && $i >= 0 && isset($djs[$i])) ? $djs[$i] : ['name'=>'','show'=>'','image'=>'','bio'=>''];

if ($action === 'delete' && $i >= 0) {
  if (!$CAN_EDIT) smurf_admin_denied('You do not have permission to edit DJs.');
  if (isset($djs[$i])) {
    array_splice($djs, $i, 1);
    smurf_write_json($dataFile, array_values($djs));
  }
  smurf_redirect_self([], ['action','i']);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (!$CAN_EDIT) smurf_admin_denied('You do not have permission to edit DJs.');
  $mode = $_POST['mode'] ?? 'create';
  $editIndex = (int)($_POST['edit_index'] ?? -1);
  $name  = trim($_POST['name'] ?? '');
  $show  = trim($_POST['show'] ?? '');
  $image = trim($_POST['image'] ?? '');
  $bio   = trim($_POST['bio'] ?? '');
  if ($image === '') {
    $uploaded = smurf_handle_upload('image_file', __DIR__ . '/../img/djs');
    if ($uploaded !== '') $image = $uploaded;
  }
  if ($name !== '') {
    $row = ['name'=>$name,'show'=>$show,'image'=>$image,'bio'=>$bio];
    if ($mode === 'update' && isset($djs[$editIndex])) $djs[$editIndex] = $row;
    else $djs[] = $row;
    smurf_write_json($dataFile, array_values($djs));
    smurf_redirect_self(['saved'=>1], ['action','i']);
  }
}
include __DIR__ . '/includes/admin_header.php';
?>
<section class="content-card">
  <h1 class="admin-title">🎙️ DJs</h1>
  <p style="opacity:.8;">Front-end reads: <code>/data/djs.json</code> • Use an external image URL or upload to <code>/img/djs/</code>.</p>
  <?php if (!empty($_GET['saved'])): ?><div class="msg success">Saved.</div><?php endif; ?>

  <div class="admin-wrap stack" style="margin-top:10px;">
    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;"><?= ($action === 'edit') ? 'Edit DJ' : 'Add DJ' ?></h3>
      <form method="POST" enctype="multipart/form-data" class="admin-form-stack">
        <input type="hidden" name="mode" value="<?= ($action === 'edit') ? 'update' : 'create' ?>">
        <input type="hidden" name="edit_index" value="<?= (int)$i ?>">

        <div class="admin-form-grid-3">
          <div><label>Name</label><input class="input-field" name="name" required value="<?= smurf_h($edit['name'] ?? '') ?>" <?= $CAN_EDIT ? '' : 'disabled' ?>></div>
          <div><label>Show</label><input class="input-field" name="show" value="<?= smurf_h($edit['show'] ?? '') ?>" <?= $CAN_EDIT ? '' : 'disabled' ?>></div>
          <div><label>Image Link (optional)</label><input class="input-field" name="image" placeholder="https://example.com/dj.jpg" value="<?= smurf_h($edit['image'] ?? '') ?>" <?= $CAN_EDIT ? '' : 'disabled' ?>></div>
        </div>

        <div>
          <label>Or Upload Image File</label>
          <div class="upload-box"><input type="file" class="admin-file-input" name="image_file" accept="image/*" <?= $CAN_EDIT ? '' : 'disabled' ?>><small class="upload-help">Use the image link above OR choose a file here. If both are filled, the image link is used. Uploaded files save into <code>/img/djs/</code></small></div>
        </div>

        <div>
          <label>Bio</label>
          <textarea class="input-field" name="bio" rows="8" <?= $CAN_EDIT ? '' : 'disabled' ?>><?= smurf_h($edit['bio'] ?? '') ?></textarea>
        </div>

        <?php if ($CAN_EDIT): ?>
          <div class="admin-card-actions">
            <button class="admin-btn primary" type="submit"><?= ($action === 'edit') ? 'Save DJ' : 'Add DJ' ?></button>
            <?php if ($action === 'edit'): ?><a class="admin-btn" href="/admin/djs.php">Cancel</a><?php endif; ?>
          </div>
        <?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: ?>
      <div class="admin-entry-grid">
      <?php foreach ($djs as $idx => $dj): $img = trim((string)($dj['image'] ?? '')); ?>
        <div class="admin-media-card">
          <?php if ($img): ?><img class="admin-media-thumb" src="<?= smurf_h($img) ?>" alt="<?= smurf_h($dj['name'] ?? '') ?>"><?php else: ?><div class="admin-media-placeholder">No DJ image yet</div><?php endif; ?>
          <div class="admin-media-meta">
            <h3><?= smurf_h($dj['name'] ?? '') ?></h3>
            <?php if (!empty($dj['show'])): ?><div class="admin-media-subtitle"><?= smurf_h($dj['show']) ?></div><?php endif; ?>
          </div>
          <div class="admin-media-body"><?= nl2br(smurf_h($dj['bio'] ?? '')) ?></div>
          <div class="admin-card-actions">
            <a class="admin-btn" href="/admin/djs.php?action=edit&i=<?= $idx ?>">Edit</a>
            <?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>
        </div>
      <?php endforeach; ?>
      </div>
      <?php endif; ?>
    </section>
  </div>
</section>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>