<div class="gallery-container">
    <div class="gallery-header">
        <h1>Gallery</h1>
        <div class="gallery-actions">
            <?php if (Session::userIsLoggedIn()): ?>
                <a href="<?php echo Config::get('URL'); ?>gallery/my" class="gallery-btn gallery-btn-secondary">My Images</a>
                <a href="<?php echo Config::get('URL'); ?>gallery/upload" class="gallery-btn gallery-btn-primary">Upload Image</a>
            <?php endif; ?>
        </div>
    </div>

    <?php if (empty($this->images)): ?>
        <div class="gallery-empty">
            <div class="gallery-empty-icon">
                <svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                    <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                    <circle cx="8.5" cy="8.5" r="1.5"></circle>
                    <polyline points="21 15 16 10 5 21"></polyline>
                </svg>
            </div>
            <h3>No images yet</h3>
            <p>Be the first to share an image!</p>
            <?php if (Session::userIsLoggedIn()): ?>
                <a href="<?php echo Config::get('URL'); ?>gallery/upload" class="gallery-btn gallery-btn-primary" style="margin-top: 16px;">Upload Image</a>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <div class="gallery-grid">
            <?php foreach ($this->images as $image): ?>
                <a href="<?php echo Config::get('URL'); ?>gallery/view/<?php echo $image->id; ?>" class="gallery-item">
                    <img src="<?php echo Config::get('URL'); ?>gallery/image/<?php echo $image->id; ?>/thumb"
                         alt="<?php echo htmlspecialchars($image->title); ?>"
                         loading="lazy">
                    <div class="gallery-item-overlay">
                        <span class="gallery-item-title"><?php echo htmlspecialchars($image->title); ?></span>
                        <span class="gallery-item-author">by <?php echo htmlspecialchars($image->user_name); ?></span>
                    </div>
                </a>
            <?php endforeach; ?>
        </div>

        <?php
        $total_pages = ceil($this->total_images / $this->per_page);
        if ($total_pages > 1):
        ?>
            <div class="gallery-pagination">
                <?php if ($this->current_page > 1): ?>
                    <a href="<?php echo Config::get('URL'); ?>gallery/index/<?php echo $this->current_page - 1; ?>" class="gallery-btn gallery-btn-secondary">Previous</a>
                <?php endif; ?>

                <span class="gallery-pagination-info">Page <?php echo $this->current_page; ?> of <?php echo $total_pages; ?></span>

                <?php if ($this->current_page < $total_pages): ?>
                    <a href="<?php echo Config::get('URL'); ?>gallery/index/<?php echo $this->current_page + 1; ?>" class="gallery-btn gallery-btn-secondary">Next</a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>
