<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Photo Gallery';
$pdo = getDB();
$stmt = $pdo->query("SELECT id, image FROM gallery WHERE is_active = 1 ORDER BY id DESC");
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$placeholder = $assetBase . 'assets/images/placeholder-product.png';
require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content gallery-page">
    <div class="container gallery-page-inner">
        <h1 class="gallery-page-title">Photo Gallery</h1>
        <?php if (empty($items)): ?>
        <p class="no-items">No gallery images yet.</p>
        <?php else: ?>
        <div class="gallery-grid">
            <?php foreach ($items as $item):
                $imgSrc = !empty($item['image']) ? $assetBase . UPLOAD_URL . 'gallery/' . rawurlencode($item['image']) : $placeholder;
            ?>
            <div class="gallery-item">
                <a href="<?php echo htmlspecialchars($imgSrc); ?>" class="gallery-link" data-lightbox="gallery" target="_blank" rel="noopener">
                    <img src="<?php echo htmlspecialchars($imgSrc); ?>" alt="" loading="lazy" onerror="this.src='<?php echo htmlspecialchars($placeholder); ?>'">
                </a>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
