<?php
require_once __DIR__ . '/../includes/functions.php';
requireAdmin();
require __DIR__ . '/includes/ui.php';
$pageTitle = 'Gallery';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrfVerify($_POST['csrf_token'] ?? null)) {
$action = $_POST['action'] ?? '';
if ($action === 'save') {
$id = (int) ($_POST['id'] ?? 0);
$imagePath = handleUpload('image', 'gallery');
$caption = trim($_POST['caption'] ?? '');
$sortOrder = (int) ($_POST['sort_order'] ?? 0);
$isActive = isset($_POST['is_active']) ? 1 : 0;
if ($id > 0) {
if ($imagePath) {
db()->prepare('UPDATE gallery_items SET caption=?,sort_order=?,is_active=?,image_path=? WHERE id=?')->execute([$caption, $sortOrder, $isActive, $imagePath, $id]);
} else {
db()->prepare('UPDATE gallery_items SET caption=?,sort_order=?,is_active=? WHERE id=?')->execute([$caption, $sortOrder, $isActive, $id]);
}
} elseif ($imagePath) {
db()->prepare('INSERT INTO gallery_items (image_path,caption,sort_order,is_active) VALUES (?,?,?,?)')->execute([$imagePath, $caption, $sortOrder, $isActive]);
} else {
flash('error', 'Please choose an image to upload for a new gallery item.');
redirect('gallery.php');
}
flash('success', 'Gallery item saved.');
} elseif ($action === 'delete') {
db()->prepare('DELETE FROM gallery_items WHERE id = ?')->execute([(int) ($_POST['id'] ?? 0)]);
flash('success', 'Gallery item deleted.');
}
redirect('gallery.php');
}
require __DIR__ . '/includes/admin-header.php';
adminFlashBox();
$items = db()->query('SELECT * FROM gallery_items ORDER BY sort_order ASC, id ASC')->fetchAll();
$editId = (int) ($_GET['edit'] ?? 0);
$editing = null;
foreach ($items as $i) { if ((int) $i['id'] === $editId) $editing = $i; }
?>
<div class="grid lg:grid-cols-3 gap-6">
<div class="lg:col-span-2 grid sm:grid-cols-2 gap-4">
<?php foreach ($items as $i): ?>
<div class="bg-white rounded-2xl border border-gray-100 overflow-hidden">
<img src="../<?= e($i['image_path']) ?>" class="w-full h-32 object-cover" />
<div class="p-3 flex items-center justify-between gap-2">
<p class="text-xs font-medium truncate flex-1"><?= e($i['caption']) ?></p>
<a href="?edit=<?= (int) $i['id'] ?>" class="text-primary text-xs font-semibold shrink-0">Edit</a>
<form method="post" onsubmit="return confirm('Delete?');" class="shrink-0">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= (int) $i['id'] ?>" />
<button class="text-red-600 text-xs font-semibold">Del</button>
</form>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="bg-white rounded-2xl border border-gray-100 p-6 h-fit">
<h2 class="font-heading font-semibold mb-4"><?= $editing ? 'Edit Photo' : 'Add Photo' ?></h2>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="save" />
<input type="hidden" name="id" value="<?= (int) ($editing['id'] ?? 0) ?>" />
<label class="block mb-4">
<span class="block text-sm font-medium text-gray-700 mb-1.5">Photo <?= $editing ? '' : '(required)' ?></span>
<?php if (!empty($editing['image_path'])): ?><img src="../<?= e($editing['image_path']) ?>" class="h-20 mb-2 rounded-lg" /><?php endif; ?>
<input type="file" name="image" accept="image/*" <?= $editing ? '' : 'required' ?> class="text-sm" />
</label>
<?php inputField('Caption', 'caption', $editing['caption'] ?? ''); ?>
<?php numberField('Sort Order', 'sort_order', $editing['sort_order'] ?? 0); ?>
<?php checkboxField('Active', 'is_active', $editing ? !empty($editing['is_active']) : true); ?>
<button class="btn-primary-brand bg-primary text-white px-5 py-2.5 rounded-lg font-semibold w-full"><?= $editing ? 'Update' : 'Add Photo' ?></button>
<?php if ($editing): ?><a href="gallery.php" class="block text-center text-sm text-gray-500 mt-3">Cancel edit</a><?php endif; ?>
</form>
</div>
</div>
<?php require __DIR__ . '/includes/admin-footer.php'; ?>