<?php
require_once __DIR__ . '/auth-check.php';
require_once __DIR__ . '/../includes/functions.php';

$gallery = getData('gallery');
$selectedYear = $_GET['year'] ?? date('Y');

// Handle upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_FILES['photos'])) {
    $uploaded = 0;
    $yearDir = UPLOADS_PATH . '/gallery/' . $selectedYear . '/';
    if (!is_dir($yearDir)) mkdir($yearDir, 0755, true);

    foreach ($_FILES['photos']['tmp_name'] as $i => $tmpName) {
        if (empty($tmpName)) continue;
        $ext = pathinfo($_FILES['photos']['name'][$i], PATHINFO_EXTENSION);
        $filename = $selectedYear . '_' . time() . '_' . $i . '.' . $ext;
        $dest = $yearDir . $filename;
        if (move_uploaded_file($tmpName, $dest)) {
            if (!isset($gallery[$selectedYear])) $gallery[$selectedYear] = [];
            $gallery[$selectedYear][] = $filename;
            $uploaded++;
        }
    }
    if ($uploaded > 0) {
        saveData('gallery', $gallery);
        setFlash('success', $uploaded . ' photo(s) uploaded');
    }
    header('Location: gallery.php?year=' . $selectedYear);
    exit;
}

// Handle delete
if (!empty($_GET['delete']) && !empty($_GET['year'])) {
    $delYear = $_GET['year'];
    $delFile = $_GET['delete'];
    @unlink(UPLOADS_PATH . '/gallery/' . $delYear . '/' . $delFile);
    if (isset($gallery[$delYear])) {
        $gallery[$delYear] = array_values(array_filter($gallery[$delYear], fn($f) => $f !== $delFile));
        saveData('gallery', $gallery);
    }
    setFlash('success', 'Photo deleted');
    header('Location: gallery.php?year=' . $delYear);
    exit;
}

$pageTitle = 'Gallery';
include __DIR__ . '/sidebar.php';
?>

<div class="admin-header">
    <h1 class="admin-title">Gallery</h1>
</div>

<!-- Upload Form -->
<div class="admin-card mb-4">
    <h3 style="font-family:var(--font-heading);font-size:0.9rem;text-transform:uppercase;color:var(--text);margin-bottom:1rem;">Upload Photos</h3>
    <form method="POST" enctype="multipart/form-data" class="row g-3 align-items-end">
        <div class="col-auto">
            <label style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">Year</label>
            <select name="year_select" class="form-select" onchange="window.location='gallery.php?year='+this.value" style="background:var(--bg2);color:var(--text);border-color:var(--border);font-size:0.85rem;">
                <?php for ($y = 2026; $y >= 2020; $y--): ?>
                <option value="<?php echo $y; ?>" <?php echo $selectedYear == $y ? 'selected' : ''; ?>><?php echo $y; ?></option>
                <?php endfor; ?>
            </select>
        </div>
        <div class="col-auto">
            <label style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">Photos</label>
            <input type="file" name="photos[]" class="form-control" accept="image/*" multiple required style="background:var(--bg2);color:var(--text);border-color:var(--border);font-size:0.8rem;">
        </div>
        <div class="col-auto">
            <button type="submit" class="btn-admin-primary" style="font-size:0.8rem;padding:0.5rem 1rem;">Upload</button>
        </div>
    </form>
</div>

<!-- Photo Grid -->
<div class="admin-card">
    <h3 style="font-family:var(--font-heading);font-size:0.9rem;text-transform:uppercase;color:var(--text);margin-bottom:1rem;"><?php echo e($selectedYear); ?> Photos</h3>
    <?php if (!empty($gallery[$selectedYear])): ?>
    <div class="row g-3">
        <?php foreach ($gallery[$selectedYear] as $img):
            $imgPath = UPLOADS_URL . '/gallery/' . $selectedYear . '/' . $img;
        ?>
        <div class="col-lg-3 col-md-4 col-sm-6">
            <div style="position:relative;border-radius:6px;overflow:hidden;border:1px solid var(--border);">
                <img src="<?php echo $imgPath; ?>" alt="" style="width:100%;height:150px;object-fit:cover;display:block;">
                <a href="gallery.php?year=<?php echo e($selectedYear); ?>&delete=<?php echo e($img); ?>" class="btn-admin-outline" style="position:absolute;top:0.5rem;right:0.5rem;font-size:0.65rem;padding:0.2rem 0.4rem;color:var(--danger);border-color:var(--danger);background:rgba(0,0,0,0.5);" onclick="return confirm('Delete this photo?');">✕</a>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php else: ?>
    <p style="color:var(--muted);text-align:center;padding:2rem;">No photos for <?php echo e($selectedYear); ?>. Upload some photos above.</p>
    <?php endif; ?>
</div>

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