<?php
$pageTitle = 'Gallery';
require_once dirname(__DIR__) . '/includes/header.php';

if (isset($_GET['delete'])) {
    $row = db()->query("SELECT image FROM gallery WHERE id=".(int)$_GET['delete'])->fetch_assoc();
    if ($row && $row['image'] && file_exists(SITE_ROOT.'/'.$row['image'])) @unlink(SITE_ROOT.'/'.$row['image']);
    db()->query("DELETE FROM gallery WHERE id=".(int)$_GET['delete']);
    flash('Photo deleted.','success');
    redirect(ADMIN_URL.'/pages/gallery.php');
}

$cat = trim($_GET['cat'] ?? '');
$where = $cat ? "WHERE category='".db()->real_escape_string($cat)."'" : "";
$photos = db()->query("SELECT * FROM gallery $where ORDER BY sort_order,id DESC")->fetch_all(MYSQLI_ASSOC);
$cats = db()->query("SELECT DISTINCT category FROM gallery ORDER BY category")->fetch_all(MYSQLI_ASSOC);
?>
<div class="page-header">
    <h1>Gallery</h1>
    <div class="page-header-actions" style="display:flex;gap:10px;align-items:center">
        <?php if (!empty($cats)): ?>
        <select onchange="location='gallery.php'+(this.value?'?cat='+encodeURIComponent(this.value):'')" class="form-select" style="width:160px;padding:7px 12px">
            <option value="">All Categories</option>
            <?php foreach($cats as $c): ?>
            <option value="<?=e($c['category'])?>" <?=$cat===$c['category']?'selected':''?>><?=e(ucfirst($c['category']))?></option>
            <?php endforeach; ?>
        </select>
        <?php endif; ?>
        <a href="gallery-add.php" class="btn btn-primary"><i class="fas fa-upload"></i> Upload Photos</a>
    </div>
</div>

<?php if (empty($photos)): ?>
<div class="card">
    <div style="padding:60px;text-align:center;color:var(--muted)">
        <i class="fas fa-images" style="font-size:48px;opacity:.2;display:block;margin-bottom:16px"></i>
        <p style="font-size:16px;margin-bottom:16px">No photos yet.</p>
        <a href="gallery-add.php" class="btn btn-primary"><i class="fas fa-upload"></i> Upload First Photo</a>
    </div>
</div>
<?php else: ?>
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:16px">
<?php foreach ($photos as $p): ?>
<div class="card" style="overflow:hidden">
    <div style="position:relative;aspect-ratio:4/3;overflow:hidden;background:#f3f4f6">
        <img src="<?=e(SITE_URL.'/'.$p['image'])?>" alt="<?=e($p['title'])?>"
            style="width:100%;height:100%;object-fit:cover;transition:transform .3s"
            onmouseover="this.style.transform='scale(1.05)'" onmouseout="this.style.transform='scale(1)'">
        <div style="position:absolute;top:8px;right:8px;display:flex;gap:4px">
            <a href="gallery-edit.php?id=<?=$p['id']?>" class="btn btn-sm btn-secondary btn-icon" style="background:rgba(255,255,255,.9)" title="Edit"><i class="fas fa-edit"></i></a>
            <button onclick="confirmDelete('gallery.php?delete=<?=$p['id']?>','this photo')" class="btn btn-sm btn-danger btn-icon" style="opacity:.85" title="Delete"><i class="fas fa-trash"></i></button>
        </div>
    </div>
    <div style="padding:10px 12px">
        <div style="font-weight:600;font-size:13px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap"><?=e($p['title']??'Untitled')?></div>
        <div style="font-size:11px;color:var(--muted);margin-top:2px"><?=e(ucfirst($p['category']))?></div>
    </div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
