
<?php
session_start(); require __DIR__ . '/includes/header.php';
$baseDirs = ['content' => __DIR__ . '/../img/content', 'friends' => __DIR__ . '/../img/friends', 'media' => __DIR__ . '/../img/media'];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $CAN_EDIT) {
  $folder = $_POST['folder'] ?? 'content';
  if (isset($baseDirs[$folder])) { admin_handle_upload('media_file', $baseDirs[$folder], ['jpg','jpeg','png','gif','webp','svg']); admin_redirect_self(['uploaded'=>1]); }
}
$items = [];
foreach ($baseDirs as $key => $dir) { if (!is_dir($dir)) continue; foreach (glob($dir . '/*') as $file) { if (!is_file($file)) continue; $items[] = ['folder'=>$key,'name'=>basename($file),'url'=>'/img/' . $key . '/' . basename($file)]; } }
?>
<section class="content-card"><h1 class="admin-title">🖼️ Media Library</h1><?php if (!empty($_GET['uploaded'])): ?><div class="msg success">Upload complete.</div><?php endif; ?><div class="page-builder-grid"><section class="content-card" style="text-align:left;"><h3 style="margin-top:0;">Upload artwork</h3><form method="post" enctype="multipart/form-data" class="form"><label>Folder</label><select class="input-field" name="folder"><?php foreach ($baseDirs as $key => $_): ?><option value="<?= admin_h($key) ?>"><?= admin_h($key) ?></option><?php endforeach; ?></select><label>Select image</label><input class="input-field" type="file" name="media_file" accept="image/*"><button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>>Upload</button></form></section><section class="content-card" style="text-align:left;"><h3 style="margin-top:0;">Saved media paths</h3><div class="media-grid"><?php foreach ($items as $item): ?><div class="media-thumb-wrap"><img class="media-thumb" src="<?= admin_h($item['url']) ?>" alt=""><div><strong><?= admin_h($item['name']) ?></strong><br><code><?= admin_h($item['url']) ?></code></div></div><?php endforeach; ?></div><?php if (!$items): ?><p>No uploaded media yet.</p><?php endif; ?></section></div></section>
<?php include __DIR__ . '/includes/footer.php'; ?>
