<?php
$pageTitle = 'ফটো গ্যালারি';
require_once 'header.php';
$db = getDB();

$page = max(1, intval($_GET['page'] ?? 1));
$limit = 24;
$offset = ($page - 1) * $limit;
$total = $db->query("SELECT COUNT(*) FROM galleries g JOIN couples c ON g.couple_id=c.id WHERE c.status='approved'")->fetchColumn();
$totalPages = ceil($total / $limit);

$photos = $db->prepare("SELECT g.*, c.boy_name, c.girl_name FROM galleries g JOIN couples c ON g.couple_id=c.id WHERE c.status='approved' ORDER BY g.created_at DESC LIMIT ? OFFSET ?");
$photos->bindValue(1, $limit, PDO::PARAM_INT);
$photos->bindValue(2, $offset, PDO::PARAM_INT);
$photos->execute();
$photos = $photos->fetchAll();

// Also get couple main photos
$couplePhotos = $db->prepare("SELECT id, boy_name, girl_name, photo FROM couples WHERE status='approved' AND photo IS NOT NULL ORDER BY created_at DESC LIMIT ? OFFSET ?");
$couplePhotos->bindValue(1, $limit, PDO::PARAM_INT);
$couplePhotos->bindValue(2, $offset, PDO::PARAM_INT);
$couplePhotos->execute();
$couplePhotos = $couplePhotos->fetchAll();
?>

<section class="section" style="padding-top:120px;">
  <div class="container">
    <h2 class="section-title">📸 ফটো গ্যালারি</h2>
    <p style="text-align:center;color:var(--text-muted);margin-bottom:40px;">মোট <?= bnNum($total) ?> টি ছবি</p>

    <?php if ($photos || $couplePhotos): ?>
    <div class="masonry">
      <?php foreach ($couplePhotos as $cp): ?>
      <div class="masonry-item" data-lightbox="<?= UPLOAD_URL . e($cp['photo']) ?>">
        <img data-src="<?= UPLOAD_URL . e($cp['photo']) ?>" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Crect fill='%231a1a2e' width='300' height='300'/%3E%3C/svg%3E" alt="<?= e($cp['boy_name']) ?> ❤ <?= e($cp['girl_name']) ?>">
        <div class="masonry-overlay">
          <div style="color:#fff;text-align:center;">
            <i class="fas fa-search-plus" style="font-size:1.5rem;"></i>
            <p style="font-size:.85rem;margin-top:4px;"><?= e($cp['boy_name']) ?> ❤ <?= e($cp['girl_name']) ?></p>
          </div>
        </div>
      </div>
      <?php endforeach; ?>
      <?php foreach ($photos as $p): ?>
      <div class="masonry-item" data-lightbox="<?= UPLOAD_URL . e($p['image_path']) ?>">
        <img data-src="<?= UPLOAD_URL . e($p['image_path']) ?>" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Crect fill='%231a1a2e' width='300' height='300'/%3E%3C/svg%3E" alt="<?= e($p['boy_name']) ?>">
        <div class="masonry-overlay">
          <div style="color:#fff;text-align:center;">
            <i class="fas fa-search-plus" style="font-size:1.5rem;"></i>
            <p style="font-size:.85rem;margin-top:4px;"><?= e($p['boy_name']) ?> ❤ <?= e($p['girl_name']) ?></p>
          </div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>

    <!-- Pagination -->
    <?php if ($totalPages > 1): ?>
    <div class="pagination">
      <?php if ($page > 1): ?><a href="?page=<?= $page-1 ?>">« আগে</a><?php endif; ?>
      <?php for ($i = 1; $i <= $totalPages; $i++): ?>
        <?php if ($i == $page): ?><span class="active"><?= bnNum($i) ?></span>
        <?php else: ?><a href="?page=<?= $i ?>"><?= bnNum($i) ?></a><?php endif; ?>
      <?php endfor; ?>
      <?php if ($page < $totalPages): ?><a href="?page=<?= $page+1 ?>">পরে »</a><?php endif; ?>
    </div>
    <?php endif; ?>

    <?php else: ?>
    <div class="glass" style="text-align:center;padding:48px;">
      <p style="font-size:3rem;">📷</p>
      <p style="color:var(--text-muted);">এখনো কোনো ছবি নেই।</p>
    </div>
    <?php endif; ?>
  </div>
</section>

<?php require_once 'footer.php'; ?>
