<?php
require_once __DIR__ . '/config.php';
requireAdmin();
$pdo = getDB();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_POST['delete_id'])) {
        $pdo->prepare("DELETE FROM gallery WHERE id = ?")->execute([(int)$_POST['delete_id']]);
    } elseif (!empty($_FILES['image']['name']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        if (in_array($ext, ['jpg','jpeg','png','gif','webp'])) {
            $dir = dirname(__DIR__) . '/uploads/gallery/';
            if (!is_dir($dir)) @mkdir($dir, 0755, true);
            $name = 'g' . time() . '.' . $ext;
            if (move_uploaded_file($_FILES['image']['tmp_name'], $dir . $name)) {
                $pdo->prepare("INSERT INTO gallery (title, image, sort_order) VALUES ('', ?, 0)")->execute([$name]);
            }
        }
    }
    header('Location: gallery.php');
    exit;
}
$items = $pdo->query("SELECT * FROM gallery ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC);
$pageTitle = 'Photo Gallery';
require_once __DIR__ . '/includes/header.php';
?>
<div class="admin-page admin-gallery-page">
    <h1>Photo Gallery</h1>
    <p class="admin-gallery-lead">Upload images only. Newest first on the site.</p>

    <section class="admin-gallery-upload-card">
        <h2 class="admin-gallery-upload-title">Add image</h2>
        <form method="post" enctype="multipart/form-data" class="admin-gallery-upload-form">
            <label class="admin-gallery-file-label">
                <span class="admin-gallery-file-text">Choose image</span>
                <input type="file" name="image" accept="image/*" required class="admin-gallery-file-input">
            </label>
            <p class="muted" style="margin:6px 0 0; font-size:0.85rem;">Recommended: <strong>1200×800</strong> (3:2) or larger. Keep file size reasonable for fast loading.</p>
            <button type="submit" class="btn admin-gallery-add-btn">Add image</button>
        </form>
    </section>

    <section class="admin-gallery-list">
        <h2 class="admin-gallery-list-title">Gallery images <span class="admin-gallery-count">(<?php echo count($items); ?>)</span></h2>
        <?php if (empty($items)): ?>
        <div class="admin-gallery-empty">
            <p>No images yet. Upload one above.</p>
        </div>
        <?php else: ?>
        <div class="gallery-admin-grid">
            <?php foreach ($items as $i): ?>
            <div class="gallery-admin-item">
                <div class="gallery-admin-img-wrap">
                    <img src="<?php echo htmlspecialchars(ADMIN_SITE_ROOT . '/' . UPLOAD_URL . 'gallery/' . $i['image']); ?>" alt="Gallery" loading="lazy">
                </div>
                <form method="post" class="gallery-admin-actions" onsubmit="return confirm('Delete this image?');">
                    <input type="hidden" name="delete_id" value="<?php echo (int)$i['id']; ?>">
                    <button type="submit" class="btn btn-secondary gallery-admin-delete">Delete</button>
                </form>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </section>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
