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


if (!$CAN_VIEW) { http_response_code(403); echo "<h1>Access Denied</h1>"; exit; }

$targetDir = __DIR__ . '/../img/content';
@mkdir($targetDir, 0775, true);

$msg = trim((string)($_GET['msg'] ?? ''));
if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['upload']) && $CAN_EDIT) {
  if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    $msg = "Upload failed.";
  } else {
    $name = basename((string)$_FILES['file']['name']);
    $name = preg_replace('/[^a-zA-Z0-9._-]/', '_', $name);
    $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
    $allowed = ['png','jpg','jpeg','gif','webp'];
    if (!in_array($ext, $allowed, true)) {
      smurf_redirect_self(['msg'=>'Only images allowed: ' . implode(', ', $allowed)]);
    } else {
      $dest = $targetDir . '/' . $name;
      if (move_uploaded_file($_FILES['file']['tmp_name'], $dest)) {
        smurf_redirect_self(['msg'=>'Uploaded: ' . $name]);
      } else {
        smurf_redirect_self(['msg'=>'Could not save file.']);
      }
    }
  }
}

$files = [];
foreach (glob($targetDir . '/*.*') as $f) {
  $ext = strtolower(pathinfo($f, PATHINFO_EXTENSION));
  if (in_array($ext, ['png','jpg','jpeg','gif','webp'], true)) $files[] = basename($f);
}
sort($files);
include __DIR__ . '/includes/admin_header.php';
?>
<section class="content-card">
  <h1 class="admin-title">🖼️ Media Library</h1>
  <p style="opacity:.85;">Upload images to <code>/img/content/</code> and copy the filename to use in News / Pages / DJs / Friends.</p>

  <div style="margin:10px 0 18px;">
    <a class="admin-btn" href="/admin/index.php">Back</a>
  </div>

  <?php if ($msg): ?><div class="content-card" style="border-color:rgba(255,215,120,.55);"><?= htmlspecialchars($msg) ?></div><?php endif; ?>

  <?php if ($CAN_EDIT): ?>
  <div class="content-card" style="text-align:left;">
    <form method="post" action="/admin/media.php" enctype="multipart/form-data" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
      <div style="flex:1;min-width:280px;">
        <label class="admin-label">Choose image</label>
        <input class="admin-input" type="file" name="file" accept="image/*" required>
      </div>
      <button class="admin-btn" type="submit" name="upload" value="1">Upload</button>
    </form>
  </div>
  <?php endif; ?>

  <div class="content-card" style="text-align:left;">
    <h3 style="margin-top:0;">Images</h3>
    <?php if (empty($files)): ?>
      <p>No images yet.</p>
    <?php else: ?>
      <div class="friends-grid" style="grid-template-columns:repeat(auto-fit,minmax(220px,1fr));">
        <?php foreach ($files as $f): ?>
          <div class="content-card" style="text-align:center;">
            <img src="/img/content/<?= htmlspecialchars($f) ?>" alt="" style="width:180px;height:120px;object-fit:cover;border-radius:12px;border:1px solid rgba(255,215,120,.35);">
            <div style="margin-top:10px;word-break:break-all;opacity:.9;"><?= htmlspecialchars($f) ?></div>
            <button class="admin-btn" type="button" onclick="navigator.clipboard.writeText('<?= htmlspecialchars($f, ENT_QUOTES) ?>'); this.textContent='Copied!'; setTimeout(()=>this.textContent='Copy filename',900);">Copy filename</button>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>
</section>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
