<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>@yield('title', 'NetGesCon') - {{ config('app.name', 'Laravel') }}</title>

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.bunny.net">
    <link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" />

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUa1y9lwNwQG0JI2V7B8JJwdq7YJ7ZJw3B8h9hXzpZ7E6F5v3Xp7a8rBQE4x" crossorigin="anonymous">

    <!-- FontAwesome (per icone menu) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-papm6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q6Q==" crossorigin="anonymous" referrerpolicy="no-referrer" />

    <!-- NetGesCon Universal Styles -->
    <style>
        :root {
            --netgescon-primary: #2563eb;
            --netgescon-secondary: #64748b;
            --netgescon-success: #059669;
            --netgescon-warning: #d97706;
            --netgescon-danger: #dc2626;
            --netgescon-info: #0284c7;
            --netgescon-dark: #1f2937;
            --netgescon-light: #f8fafc;
        }

        body {
            font-family: 'Figtree', sans-serif;
            background-color: var(--netgescon-light);
        }

        .main-content {
            margin-left: 280px;
            transition: margin-left 0.3s ease;
            min-height: 100vh;
            padding: 0;
        }

        .main-content.sidebar-collapsed {
            margin-left: 0;
        }

        .top-navbar {
            background: linear-gradient(135deg, var(--netgescon-primary) 0%, var(--netgescon-info) 100%);
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            z-index: 999;
        }

        .sidebar-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 999;
            display: none;
        }

        .sidebar-overlay.show {
            display: block;
        }

        @media (max-width: 768px) {
            .main-content {
                margin-left: 0;
            }
        }

        .breadcrumb-netgescon {
            background: white;
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1.5rem;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .card-netgescon {
            border: none;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }

        .card-netgescon:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 15px rgba(0,0,0,0.1);
        }

        .btn-netgescon-primary {
            background: linear-gradient(135deg, var(--netgescon-primary) 0%, var(--netgescon-info) 100%);
            border: none;
            border-radius: 8px;
            padding: 0.5rem 1.5rem;
            color: white;
            transition: all 0.2s ease;
        }

        .btn-netgescon-primary:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
            color: white;
        }

        .table-netgescon {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
        }

        .table-netgescon th {
            background: linear-gradient(135deg, var(--netgescon-dark) 0%, var(--netgescon-secondary) 100%);
            color: white;
            font-weight: 600;
            border: none;
            padding: 1rem 0.75rem;
        }

        .table-netgescon td {
            padding: 0.75rem;
            border-color: #e5e7eb;
            vertical-align: middle;
        }

        .table-netgescon tbody tr:hover {
            background-color: #f8fafc;
        }
    </style>

    <!-- Scripts -->
    @vite(['resources/css/app.css', 'resources/js/app.js'])

    @stack('styles')
</head>
<body>
    {{-- Sidebar dinamica universale --}}
    @include('components.menu.sidebar-dynamic')

    {{-- Overlay per mobile --}}
    <div class="sidebar-overlay d-md-none" id="sidebar-overlay"></div>

    {{-- Contenuto principale --}}
    <div class="main-content" id="main-content">
        {{-- Top Navigation Bar --}}
        <nav class="navbar top-navbar navbar-expand-lg navbar-dark">
            <div class="container-fluid">
                {{-- Mobile menu toggle --}}
                <button class="btn btn-outline-light d-md-none me-2" id="mobile-menu-toggle">
                    <i class="fas fa-bars"></i>
                </button>

                {{-- Breadcrumb --}}
                <div class="navbar-nav me-auto">
                    @hasSection('breadcrumb')
                        @yield('breadcrumb')
                    @else
                        <span class="navbar-text text-white">
                            <i class="fas fa-home me-1"></i>
                            @yield('title', 'Dashboard')
                        </span>
                    @endif
                </div>

                {{-- User actions --}}
                <div class="navbar-nav">
                    {{-- Notifiche --}}
                    <div class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle text-white" href="#" role="button" data-bs-toggle="dropdown">
                            <i class="fas fa-bell"></i>
                            <span class="badge bg-danger rounded-pill">3</span>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><h6 class="dropdown-header">Notifiche</h6></li>
                            <li><a class="dropdown-item" href="#"><i class="fas fa-info-circle text-info me-2"></i>Nuovo pagamento</a></li>
                            <li><a class="dropdown-item" href="#"><i class="fas fa-exclamation-triangle text-warning me-2"></i>Fattura in scadenza</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item text-center" href="#">Vedi tutte</a></li>
                        </ul>
                    </div>

                    {{-- User menu --}}
                    <div class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle text-white" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fas fa-user-circle me-1"></i>
                            {{ auth()->user()->name ?? 'Utente' }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><h6 class="dropdown-header">{{ ucfirst(str_replace('_', ' ', auth()->user()->getRoleNames()->first() ?? 'user')) }}</h6></li>
                            <li><a class="dropdown-item" href="{{ route('profile.edit') }}"><i class="fas fa-user me-2"></i>Profilo</a></li>
                            <li><a class="dropdown-item" href="#"><i class="fas fa-cog me-2"></i>Impostazioni</a></li>
                            @if(auth()->user()->hasRole('super-admin'))
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="{{ route('superadmin.dashboard') }}"><i class="fas fa-crown me-2"></i>SuperAdmin</a></li>
                            <li><a class="dropdown-item text-muted" href="#" title="Impersonifica - Funzionalità in sviluppo"><i class="fas fa-mask me-2"></i>Impersonifica</a></li>
                            @endif
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <form method="POST" action="{{ route('logout') }}" class="d-inline">
                                    @csrf
                                    <button type="submit" class="dropdown-item">
                                        <i class="fas fa-sign-out-alt me-2"></i>Logout
                                    </button>
                                </form>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </nav>

        {{-- Page content --}}
        <div class="container-fluid p-4">
            {{-- Breadcrumb interno --}}
            @hasSection('breadcrumb-internal')
            <div class="breadcrumb-netgescon">
                @yield('breadcrumb-internal')
            </div>
            @endif

            {{-- Alert messages --}}
            @if (session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <i class="fas fa-check-circle me-2"></i>
                    {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @if (session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-circle me-2"></i>
                    {{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @if (session('warning'))
                <div class="alert alert-warning alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    {{ session('warning') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @if ($errors->any())
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-circle me-2"></i>
                    <strong>Errori di validazione:</strong>
                    <ul class="mb-0 mt-2">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            {{-- Main content --}}
            @yield('content')
        </div>
    </div>

    {{-- jQuery --}}
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    {{-- Bootstrap JS --}}
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-OgGKvMK4Xfd3LX2p2E9B6lQTeCHKq6k0N8jbJJLMWKXMZ4Br1RrLEjzz2ZKdG8aH" crossorigin="anonymous"></script>

    {{-- NetGesCon Universal JavaScript --}}
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Gestione mobile menu
            const mobileToggle = document.getElementById('mobile-menu-toggle');
            const sidebar = document.getElementById('sidebar');
            const overlay = document.getElementById('sidebar-overlay');
            const mainContent = document.getElementById('main-content');

            // Toggle mobile sidebar
            if (mobileToggle) {
                mobileToggle.addEventListener('click', function() {
                    sidebar.classList.toggle('show');
                    if (overlay) overlay.classList.toggle('show');
                });
            }

            // Chiudi sidebar cliccando overlay
            if (overlay) {
                overlay.addEventListener('click', function() {
                    sidebar.classList.remove('show');
                    overlay.classList.remove('show');
                });
            }

            // Auto-close mobile menu on route change
            window.addEventListener('beforeunload', function() {
                if (sidebar) sidebar.classList.remove('show');
                if (overlay) overlay.classList.remove('show');
            });

            // Gestione notifiche auto-dismiss
            setTimeout(function() {
                const alerts = document.querySelectorAll('.alert');
                alerts.forEach(function(alert) {
                    const bsAlert = new bootstrap.Alert(alert);
                    setTimeout(function() {
                        bsAlert.close();
                    }, 5000);
                });
            }, 100);
        });

        // Utility functions per l'interfaccia
        function showLoading(element) {
            if (element) {
                element.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>Caricamento...';
                element.disabled = true;
            }
        }

        function hideLoading(element, originalText) {
            if (element) {
                element.innerHTML = originalText;
                element.disabled = false;
            }
        }

        // Conferma eliminazione
        function confirmDelete(message = 'Sei sicuro di voler eliminare questo elemento?') {
            return confirm(message);
        }

        // CSRF token per AJAX
        if (document.querySelector('meta[name="csrf-token"]')) {
            window.csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        }

        // Gestione navigazione unificata dashboard
        $(document).on('click', '.dashboard-nav-link', function(e) {
            e.preventDefault();
            
            const section = $(this).data('section');
            const action = $(this).data('action');
            
            if (action === 'create-stabile') {
                // Invia evento personalizzato per la dashboard
                if (typeof window.showCreateStabileForm === 'function') {
                    window.showCreateStabileForm();
                } else {
                    // Fallback - vai alla pagina normale
                    window.location.href = '{{ route("admin.stabili.create") }}';
                }
            } else if (section) {
                // Invia evento personalizzato per caricare la sezione
                if (typeof window.loadDashboardSection === 'function') {
                    window.loadDashboardSection(section);
                } else {
                    // Reindirizza alla dashboard se non siamo già lì
                    if (!window.location.pathname.includes('/dashboard')) {
                        window.location.href = '{{ route("dashboard") }}';
                    }
                }
            }
        });
    </script>

    @stack('scripts')
</body>
</html>
