@extends('front.layout.pages-layout')
@section('pageTitle', $pageTitle)
@section('meta_tags') {!! SEO::generate() !!} @endsection

@section('content')

<section class="section py-5">
    <div class="container">
        <!-- Header -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="d-flex justify-content-between align-items-center flex-wrap">
                    <h3 class="mb-0">{{ $album->title }}</h3>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb bg-transparent mb-0 px-0">
                            <li class="breadcrumb-item"><a href="{{ url('/') }}">Home</a></li>
                            <li class="breadcrumb-item"><a href="{{ route('photo_gallery') }}">Photo Gallery</a></li>
                            <li class="breadcrumb-item active" aria-current="page">{{ $album->title }}</li>
                        </ol>
                    </nav>
                </div>
                <hr>
            </div>
        </div>

        <div id="photo-loading" class="text-center py-5">
            <div class="spinner-border text-primary" role="status" style="width: 3rem; height: 3rem;">
                <span class="visually-hidden">Loading...</span>
            </div>
        </div>
        <!-- Photo Grid -->
        @if($photos->count())
        <!-- Photo Grid -->
        <div id="photo-grid" class="row" data-masonry='{"itemSelector": ".masonry-item", "percentPosition": true }'>
            @foreach($photos as $photo)
            <div class="col-6 col-md-4 col-lg-3 mb-4 masonry-item">
                <a href="{{ asset('images/albums/' . $photo->photo_path) }}" class="glightbox" data-gallery="album-gallery">
                    <img src="{{ asset('images/albums/' . $photo->photo_path) }}" class="img-fluid card-radius w-100" alt="Photo">
                </a>
            </div>
            @endforeach
        </div>


        @else
        <div class="text-center py-5">
            <p class="text-muted">No photos found in this album.</p>
        </div>
        @endif
    </div>
</section>

@endsection
@push('scripts')
<script>
    document.addEventListener("DOMContentLoaded", function () {
        const loader = document.getElementById('photo-loading');
        const gridContainer = document.getElementById('photo-grid-container');
        const grid = document.getElementById('photo-grid');

        if (!grid || grid.children.length === 0) {
            // No photos — hide loader and show empty state if any
            loader.style.display = 'none';
            return;
        }

        imagesLoaded(grid, function () {
            new Masonry(grid, {
                itemSelector: '.masonry-item',
                percentPosition: true,
                columnWidth: '.masonry-item'
            });

            GLightbox({ selector: '.glightbox' });

            loader.style.display = 'none';
            gridContainer.style.display = 'block';
        });
    });
</script>
@endpush
