<!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>

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

    <!-- FontAwesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

    <!-- NetGesCon Clean Styles -->
    <style>
        :root {
            --netgescon-primary: #2563eb;
            --netgescon-secondary: #64748b;
            --netgescon-light: #f8fafc;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: var(--netgescon-light);
        }

        /* Layout orizzontale integrato */
        .navbar-top {
            background: linear-gradient(135deg, var(--netgescon-primary) 0%, #1d4ed8 100%);
            color: white;
            padding: 0.75rem;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
            min-height: 85px; /* Ridotto per essere più compatto */
        }

        .main-layout {
            display: flex;
            min-height: calc(100vh - 85px);
            margin-top: 85px; /* Spazio per header fisso ridotto */
            transition: all 0.3s ease;
        }

        .sidebar-integrated {
            width: 280px;
            background: #fbbf24; /* giallo come nell'originale */
            border-right: 4px solid var(--netgescon-primary);
            min-height: calc(100vh - 85px);
            position: fixed;
            top: 85px;
            left: 0;
            overflow-y: auto;
            transition: all 0.3s ease;
        }

        .sidebar-integrated.collapsed {
            width: 0;
            overflow: hidden;
        }

        .content-area {
            flex: 1;
            padding: 20px;
            background-color: var(--netgescon-light);
            margin-left: 280px; /* Spazio per sidebar fissa */
            transition: all 0.3s ease;
            overflow-y: auto;
            max-height: calc(100vh - 120px);
        }

        .sidebar-integrated.collapsed + .content-area {
            margin-left: 0; /* Quando sidebar è nascosta */
        }

        .toggle-sidebar {
            background: rgba(255,255,255,0.2) !important;
            border: 1px solid rgba(255,255,255,0.3) !important;
            color: white !important;
            margin-right: 8px;
        }

        .toggle-sidebar:hover {
            background: rgba(255,255,255,0.3) !important;
            color: white !important;
        }

        /* Dark Mode styles */
        .dark-mode {
            background-color: #1a1a1a !important;
            color: #ffffff !important;
        }

        .dark-mode .navbar-top {
            background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%) !important;
        }

        .dark-mode .sidebar-integrated {
            background: #374151 !important; /* grigio scuro invece del giallo */
            border-right-color: #6b7280 !important;
        }

        .dark-mode .sidebar-integrated .border-bottom {
            border-color: #6b7280 !important;
        }

        .dark-mode .content-area {
            background-color: #1a1a1a !important;
        }

        .dark-mode .card {
            background-color: #2d2d2d !important;
            border-color: #444 !important;
            color: #ffffff !important;
        }

        .dark-mode .nav-link {
            color: #ffffff !important;
        }

        .dark-mode .nav-link:hover {
            background-color: rgba(255,255,255,0.1) !important;
        }

        .dark-mode .nav-link.active {
            background-color: #3b82f6 !important;
            color: #ffffff !important;
        }

        .dark-mode .text-dark {
            color: #ffffff !important;
        }

        .dark-mode .text-muted {
            color: #aaaaaa !important;
        }

        .dark-mode .border-primary {
            border-color: #6b7280 !important;
        }

        .dark-mode .bg-primary {
            background-color: #3b82f6 !important;
        }

        @media (max-width: 768px) {
            .main-layout {
                flex-direction: column;
            }
            
            .sidebar-integrated {
                width: 100%;
                order: 2;
            }
            
            .content-area {
                order: 1;
            }
        }
    </style>

    @stack('styles')
    
    {{-- CSS Personalizzato Utente --}}
    @auth
        <style id="user-theme-css">
            {!! \App\Helpers\ThemeHelper::generateCustomCSS() !!}
        </style>
    @endauth
</head>
<body>
    {{-- Header superiore --}}
    <div class="navbar-top">
        <div class="d-flex justify-content-between align-items-center">
            <div class="d-flex align-items-center">
                <button id="toggle-sidebar" class="btn btn-outline-light btn-sm toggle-sidebar me-3" title="Nascondi/Mostra Menu">
                    <i class="fas fa-bars"></i>
                </button>
                <h4 class="mb-0">🏢 NetGesCon</h4>
            </div>
            
            <!-- Info Stabile e Gestione Centralizzati -->
            <div class="d-flex align-items-center gap-4">
                <div class="text-center">
                    <div class="fw-bold">Condominio Roma Centro</div>
                    <div class="small opacity-75">CF: 12345678901 | Saldo: €12.350,00</div>
                </div>
                <div class="text-center">
                    <span class="badge bg-success px-3 py-2">ORDINARIA</span>
                    <div class="small">2025/Ord.</div>
                </div>
                <div class="text-center">
                    <div class="small">Prossime Scadenze</div>
                    <button class="btn btn-warning btn-sm" onclick="openCalendar()">
                        <i class="fas fa-exclamation-triangle me-1"></i>3 in scadenza
                    </button>
                </div>
            </div>
            
            <!-- Controlli Utente -->
            <div class="d-flex align-items-center gap-2">
                <span class="small opacity-75">Gestione Condominiale</span>
                <div class="vr opacity-50"></div>
                <button class="btn btn-outline-light btn-sm" onclick="openNoteModal()" title="Note e Chiamate">
                    <i class="fas fa-sticky-note me-1"></i>Note
                </button>
                <button class="btn btn-outline-light btn-sm" onclick="openRubrica()" title="Rubrica">
                    <i class="fas fa-address-book me-1"></i>Rubrica
                </button>
                <button class="btn btn-outline-light btn-sm" onclick="toggleDarkMode()" title="Dark Mode">
                    <i class="fas fa-moon me-1"></i>Dark
                </button>
                <div class="vr opacity-50"></div>
                <span class="text-light small">
                    <i class="fas fa-user-circle me-1"></i>
                    {{ auth()->user()->name ?? 'Amministratore' }}
                </span>
                <form method="POST" action="{{ route('logout') }}" class="d-inline">
                    @csrf
                    <button type="submit" class="btn btn-outline-light btn-sm" title="Logout">
                        <i class="fas fa-sign-out-alt"></i>
                    </button>
                </form>
            </div>
        </div>
    </div>

    {{-- Layout principale orizzontale --}}
    <div class="main-layout">
        {{-- Sidebar integrata --}}
        <div class="sidebar-integrated">
            @include('components.menu.sidebar')
        </div>

        {{-- Area contenuto --}}
        <div class="content-area">
            {{-- 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

            {{-- Content della pagina --}}
            @yield('content')

            {{-- Footer --}}
            <footer class="mt-4 pt-3 border-top">
                <div class="row">
                    <div class="col-md-6">
                        <small class="text-muted">
                            © {{ date('Y') }} NetGesCon - Gestione Condominiale
                            <span class="ms-2">v2.1.0</span>
                        </small>
                    </div>
                    <div class="col-md-6 text-end">
                        <small class="text-muted">
                            <a href="#" class="text-decoration-none text-muted me-2">Supporto</a>
                            <a href="#" class="text-decoration-none text-muted me-2">Contatti</a>
                            <a href="#" class="text-decoration-none text-muted">www.netgescon.it</a>
                        </small>
                    </div>
                </div>
            </footer>
        </div>
    </div>

    {{-- Mobile sidebar --}}
    <div class="offcanvas offcanvas-start bg-warning" tabindex="-1" id="mobile-sidebar" aria-labelledby="mobileSidebarLabel">
        <div class="offcanvas-header border-bottom border-primary">
            <h5 class="offcanvas-title" id="mobileSidebarLabel">Menu</h5>
            <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
        </div>
        <div class="offcanvas-body p-0">
            @include('components.menu.sidebar')
        </div>
    </div>

    {{-- Mobile menu button --}}
    <button class="btn btn-primary d-md-none position-fixed" style="top: 20px; left: 20px; z-index: 1001;" 
            type="button" data-bs-toggle="offcanvas" data-bs-target="#mobile-sidebar">
        <i class="fas fa-bars"></i>
    </button>

    {{-- Modal Note e Chiamate --}}
    <div class="modal fade" id="noteModal" tabindex="-1" aria-labelledby="noteModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="noteModalLabel">
                        <i class="fas fa-sticky-note me-2"></i>Note, Chiamate e TODO
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h6><i class="fas fa-phone text-success me-2"></i>Chiamate Recenti</h6>
                            <div class="list-group mb-3">
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between">
                                        <div>
                                            <strong>Mario Rossi</strong> - Apt. 12
                                            <br><small class="text-muted">Problema riscaldamento</small>
                                        </div>
                                        <small class="text-muted">10:30</small>
                                    </div>
                                </div>
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between">
                                        <div>
                                            <strong>Ditta Elettrica</strong>
                                            <br><small class="text-muted">Preventivo lavori</small>
                                        </div>
                                        <small class="text-muted">Ieri</small>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <h6><i class="fas fa-tasks text-warning me-2"></i>TODO Urgenti</h6>
                            <div class="list-group mb-3">
                                <div class="list-group-item">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="todo1">
                                        <label class="form-check-label" for="todo1">
                                            Raccomandata Agenzia Entrate
                                        </label>
                                    </div>
                                </div>
                                <div class="list-group-item">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="todo2">
                                        <label class="form-check-label" for="todo2">
                                            Controllo polizza assicurazione
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-12">
                            <h6><i class="fas fa-edit text-primary me-2"></i>Nuova Nota</h6>
                            <form>
                                <div class="mb-3">
                                    <label for="noteText" class="form-label">Testo della nota</label>
                                    <textarea class="form-control" id="noteText" rows="3" placeholder="Inserisci la tua nota..."></textarea>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <label for="noteType" class="form-label">Tipo</label>
                                        <select class="form-select" id="noteType">
                                            <option value="nota">Nota</option>
                                            <option value="chiamata">Chiamata</option>
                                            <option value="todo">TODO</option>
                                            <option value="promemoria">Promemoria</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="notePriority" class="form-label">Priorità</label>
                                        <select class="form-select" id="notePriority">
                                            <option value="normale">Normale</option>
                                            <option value="alta">Alta</option>
                                            <option value="urgente">Urgente</option>
                                        </select>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Chiudi</button>
                    <button type="button" class="btn btn-primary">Salva Nota</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <!-- NetGesCon Script -->
    <script>
        // Toggle sidebar
        document.getElementById('toggle-sidebar').addEventListener('click', function() {
            const sidebar = document.querySelector('.sidebar-integrated');
            const icon = this.querySelector('i');
            
            sidebar.classList.toggle('collapsed');
            
            if (sidebar.classList.contains('collapsed')) {
                icon.className = 'fas fa-chevron-right';
                this.title = 'Mostra Menu';
            } else {
                icon.className = 'fas fa-bars';
                this.title = 'Nascondi Menu';
            }
        });

        // Note e chiamate modal
        function openNoteModal() {
            const noteModal = new bootstrap.Modal(document.getElementById('noteModal'));
            noteModal.show();
        }

        // Calendario e scadenze
        function openCalendar() {
            alert('📅 Apertura Calendario NetGesCon\n\n' +
                  '🔸 Prossime Scadenze:\n' +
                  '• Rata Dicembre 2024 - 15/01/2025\n' +
                  '• Assicurazione Condominio - 28/02/2025\n' +
                  '• Revisione Impianti - 15/03/2025\n' +
                  '• Assemblea Ordinaria - 30/03/2025\n\n' +
                  '📝 Funzionalità in sviluppo:\n' +
                  '• Visualizzazione giorno/settimana/mese/anno\n' +
                  '• Sincronizzazione Google/Microsoft Calendar\n' +
                  '• Promemoria automatici\n' +
                  '• Gestione appuntamenti\n' +
                  '• Note e chiamate integrate');
        }

        // Dark Mode
        function toggleDarkMode() {
            document.body.classList.toggle('dark-mode');
            const isDark = document.body.classList.contains('dark-mode');
            
            // Salva la preferenza
            localStorage.setItem('darkMode', isDark);
            
            // Cambia icona
            const darkButtons = document.querySelectorAll('[onclick="toggleDarkMode()"] i');
            darkButtons.forEach(btn => {
                btn.className = isDark ? 'fas fa-sun' : 'fas fa-moon';
            });
            
            // Cambia anche il testo del tooltip
            const darkBtns = document.querySelectorAll('[onclick="toggleDarkMode()"]');
            darkBtns.forEach(btn => {
                btn.setAttribute('title', isDark ? 'Light Mode' : 'Dark Mode');
            });
        }

        // Carica preferenza Dark Mode all'avvio
        document.addEventListener('DOMContentLoaded', function() {
            if (localStorage.getItem('darkMode') === 'true') {
                toggleDarkMode();
            }
        });

        document.addEventListener('DOMContentLoaded', function() {
            const noteBtn = document.querySelector('[title="Note e Chiamate"]');
            if (noteBtn) {
                noteBtn.addEventListener('click', function() {
                    const noteModal = new bootstrap.Modal(document.getElementById('noteModal'));
                    noteModal.show();
                });
            }

            // Toggle Dark Mode
            function toggleDarkMode() {
                document.body.classList.toggle('dark-mode');
                
                // Salva preferenza nel localStorage
                const isDark = document.body.classList.contains('dark-mode');
                localStorage.setItem('darkMode', isDark);
                
                // Aggiorna icona
                const darkBtn = document.querySelector('[onclick="toggleDarkMode()"] i');
                if (darkBtn) {
                    darkBtn.className = isDark ? 'fas fa-sun me-1' : 'fas fa-moon me-1';
                }
            }

            // Carica preferenza Dark Mode
            if (localStorage.getItem('darkMode') === 'true') {
                document.body.classList.add('dark-mode');
                const darkBtn = document.querySelector('[onclick="toggleDarkMode()"] i');
                if (darkBtn) {
                    darkBtn.className = 'fas fa-sun me-1';
                }
            }

            // Calendario (placeholder)
            function openCalendar() {
                alert('Calendario Scadenze:\n\n📅 GENNAIO 2025\n• 15/01 - Rata dicembre (3 giorni)\n• 20/01 - Scadenza F24 (8 giorni)\n• 28/01 - Assicurazione (16 giorni)\n\n⚠️ 3 scadenze imminenti');
            }

            // Funzione per aprire le news (placeholder)
            function openNewsPage() {
                window.open('https://www.ansa.it/', '_blank');
            }

            // Rubrica (placeholder)
            const rubriceBtn = document.querySelector('[title="Rubrica"]');
            if (rubriceBtn) {
                rubriceBtn.addEventListener('click', function() {
                    alert('Rubrica - Funzione in sviluppo');
                });
            }

            // Stampa
            const printBtn = document.querySelector('[title="Stampa"]');
            if (printBtn) {
                printBtn.addEventListener('click', function() {
                    window.print();
                });
            }

            // Scadenze
            const scadenzeBtn = document.querySelector('[title="Vedi Scadenze"]');
            if (scadenzeBtn) {
                scadenzeBtn.addEventListener('click', function() {
                    alert('Scadenze:\n• Rata dicembre 2024 - 15/01/2025\n• Assicurazione condominiale - 28/02/2025\n• Revisione impianti - 15/03/2025');
                });
            }

            // Cambio stabile
            const stabileBtn = document.querySelector('[title="Cambia Stabile"]');
            if (stabileBtn) {
                stabileBtn.addEventListener('click', function() {
                    alert('Selezione Stabile - Funzione in sviluppo');
                });
            }
        });
    </script>

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