@if(($settings?->show_videos ?? true) && $videos->isNotEmpty())
<section class="section {{ $sectionBg ?? '' }}" id="videos">
    <div class="container">
        <div class="d-flex justify-content-between align-items-end mb-5" data-aos="fade-up">
            <div>
                <span class="badge-pill">Videos</span>
                <h2 class="section-title mt-2 mb-0">{!! $settings?->videos_title ?: 'Conoce nuestro colegio' !!}</h2>
                @if($settings?->videos_subtitle)
                    <p class="text-muted mb-0 mt-2">{!! $settings->videos_subtitle !!}</p>
                @endif
            </div>
            <div class="d-flex align-items-center gap-2">
                <button type="button" class="btn btn-outline-primary rounded-circle" style="width:44px;height:44px;" data-bs-target="#videosCarousel" data-bs-slide="prev" aria-label="Anterior">
                    <i class="bi bi-chevron-left"></i>
                </button>
                <button type="button" class="btn btn-outline-primary rounded-circle" style="width:44px;height:44px;" data-bs-target="#videosCarousel" data-bs-slide="next" aria-label="Siguiente">
                    <i class="bi bi-chevron-right"></i>
                </button>
                <a href="{{ route('videos.index') }}" class="btn btn-outline-primary rounded-pill ms-2">Ver todos</a>
            </div>
        </div>

        <div id="videosCarousel" class="carousel slide" data-bs-ride="carousel" data-bs-interval="4000">
            <div class="carousel-inner" id="videosCarouselInner"></div>
        </div>
        <div id="videosCardsSource" class="d-none">
            @foreach($videos as $video)
                <div class="video-card-src">
                    <div class="video-thumb" data-embed="{{ youtube_embed($video->url) }}" data-title="{{ $video->title }}">
                        @if(youtube_thumb($video->url))
                            <img src="{{ youtube_thumb($video->url) }}" alt="{{ $video->title }}">
                        @endif
                        <div class="play-overlay"><i class="bi bi-play-circle-fill"></i></div>
                    </div>
                    <div class="mt-2 fw-semibold">{{ $video->title }}</div>
                    @if($video->category)<div class="text-muted small">{{ $video->category }}</div>@endif
                </div>
            @endforeach
        </div>
    </div>
</section>
@endif

@push('scripts')
<script>
(function () {
    var source = document.getElementById('videosCardsSource');
    var inner = document.getElementById('videosCarouselInner');
    if (!source || !inner) return;

    var cards = Array.prototype.slice.call(source.querySelectorAll('.video-card-src'));
    var currentPer = 0;

    function perSlide() {
        if (window.innerWidth < 576) return 1;
        if (window.innerWidth < 992) return 2;
        return 3;
    }

    function build() {
        var per = perSlide();
        if (per === currentPer && inner.children.length) return;
        currentPer = per;

        var colClass = per === 1 ? 'col-12' : (per === 2 ? 'col-sm-6' : 'col-md-4');

        var arr = cards.slice();
        var rem = arr.length % per;
        if (rem !== 0) {
            for (var i = 0; i < per - rem; i++) {
                arr.push(cards[i % cards.length]);
            }
        }

        inner.innerHTML = '';
        for (var g = 0; g < arr.length; g += per) {
            var item = document.createElement('div');
            item.className = 'carousel-item' + (g === 0 ? ' active' : '');
            var row = document.createElement('div');
            row.className = 'row g-4';
            for (var k = g; k < g + per; k++) {
                var col = document.createElement('div');
                col.className = colClass;
                col.appendChild(arr[k].cloneNode(true));
                row.appendChild(col);
            }
            item.appendChild(row);
            inner.appendChild(item);
        }
    }

    build();
    window.addEventListener('resize', function () {
        clearTimeout(window.__vidsT);
        window.__vidsT = setTimeout(build, 150);
    });
})();
</script>
@endpush
