@include('layouts._partials.error')

<!DOCTYPE html>
<html lang="es">
<head>
    @include('layouts._partials.head')
    @livewireStyles
</head>

<body id="page-top" class="sidebar-toggled">
<div id="wrapper">

    @include('layouts._partials.menu')

    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            @include('layouts._partials.header')
            @yield('content')
        </div>

        @include('layouts._partials.footer')
    </div>
</div>

{{-- JS base --}}
@include('layouts._partials.scripts')

{{-- Livewire --}}
@livewireScripts

{{-- Config de Reverb inyectada en runtime para que funcione en producción --}}
<script>
window.reverbConfig = {
    key:      '{{ config('reverb.apps.apps.0.key') }}',
    host:     '{{ env('REVERB_HOST', parse_url(config('app.url'), PHP_URL_HOST)) }}',
    port:     {{ env('REVERB_PORT', 8080) }},
    scheme:   '{{ env('REVERB_SCHEME', 'https') }}',
    forceTLS: {{ env('REVERB_SCHEME', 'http') === 'https' ? 'true' : 'false' }},
};
</script>

{{-- Vite (Echo/Reverb) --}}
@vite(['resources/js/app.js'])

{{-- scripts por vista --}}
@yield('scripts')

@stack('scripts')

<script>
document.addEventListener('livewire:init', () => {

    Livewire.on('swal', (data, ...payload) => {

        // 🔥 compatibilidad Livewire viejo y nuevo
        if (Array.isArray(data)) {
            data = data[0];
        }

        if (!data && payload.length) {
            data = payload[0];
        }

        if (!data || !data.text) {
            console.warn('SweetAlert disparado sin datos', data);
            return;
        }

        Swal.fire({
            icon: data.icon ?? 'info',
            title: data.title ?? 'Aviso',
            text: data.text,
            timer: data.timer ?? null,
            showConfirmButton: data.confirm ?? true,
        });

        if (data.reload) {
            setTimeout(() => location.reload(), data.reload);
        }

        if (data.closeModal) {
            $('#exampleModal').modal('hide');
        }
    });

    Livewire.on('open-prospecto-modal', () => {
        $('#exampleModal').modal('show');
    });

});

window.addEventListener('close-modal', () => {
    const modal = bootstrap.Modal.getInstance(
        document.getElementById('logoutSessionsModal')
    );
    modal?.hide();
});

document.addEventListener("DOMContentLoaded", function () {

    function toggleSidebarByScreen() {
        if (window.innerWidth < 768) {
            document.body.classList.add("sidebar-toggled");
            document.querySelector(".sidebar").classList.add("toggled");
        } else {
            document.body.classList.remove("sidebar-toggled");
            document.querySelector(".sidebar").classList.remove("toggled");
        }
    }

    toggleSidebarByScreen();
    window.addEventListener("resize", toggleSidebarByScreen);

});
</script>

@stack('modals')

</body>
</html>
