{{-- 
========================================
MENU CONTABILITÀ AVANZATO NETGESCON
Sistema completo partita doppia multi-gestione
========================================
--}}
@php
    $stats = App\Helpers\SidebarStatsHelper::getStats();
    $contabilitaStats = $stats['contabilita'] ?? [
        'rate_scadute' => 0, 
        'incassi_mese' => 0, 
        'movimenti_mese' => 0,
        'saldi_non_quadrati' => 0,
        'f24_in_scadenza' => 0,
        'riconciliazioni_pending' => 0
    ];
@endphp

<li class="nav-item mb-1">
    <a class="nav-link d-flex align-items-center justify-content-between text-dark {{ request()->routeIs('admin.contabilita.*', 'admin.rate.*', 'admin.bilanci.*', 'admin.registrazioni.*', 'admin.riconciliazioni.*', 'admin.millesimi.*') ? 'active bg-warning text-dark fw-bold' : '' }}" 
       data-bs-toggle="collapse" href="#contabilitaMenu" role="button"
       aria-expanded="{{ request()->routeIs('admin.contabilita.*', 'admin.rate.*', 'admin.bilanci.*', 'admin.registrazioni.*', 'admin.riconciliazioni.*', 'admin.millesimi.*') ? 'true' : 'false' }}"
       aria-controls="contabilitaMenu">
        <div class="d-flex align-items-center">
            <i class="fas fa-calculator me-2"></i>
            <span>Contabilità</span>
            @php
                $totalBadges = ($contabilitaStats['rate_scadute'] ?? 0) + 
                               ($contabilitaStats['saldi_non_quadrati'] ?? 0) + 
                               ($contabilitaStats['f24_in_scadenza'] ?? 0) + 
                               ($contabilitaStats['riconciliazioni_pending'] ?? 0);
            @endphp
            @if($totalBadges > 0)
                {!! App\Helpers\SidebarStatsHelper::getBadge($totalBadges, 'danger') !!}
            @endif
        </div>
        <i class="fas fa-chevron-down"></i>
    </a>
    <div class="collapse {{ request()->routeIs('admin.contabilita.*', 'admin.rate.*', 'admin.bilanci.*', 'admin.registrazioni.*', 'admin.riconciliazioni.*', 'admin.millesimi.*') ? 'show' : '' }}" id="contabilitaMenu">
        <ul class="nav nav-pills flex-column ms-3">
            
            {{-- Dashboard Contabile --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.contabilita.index') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.contabilita.index') }}">
                    <i class="fas fa-tachometer-alt me-2"></i>Dashboard Contabile
                    @if($contabilitaStats['saldi_non_quadrati'] > 0)
                        {!! App\Helpers\SidebarStatsHelper::getBadge($contabilitaStats['saldi_non_quadrati'], 'warning') !!}
                    @endif
                </a>
            </li>

            {{-- Maschera Unica di Registrazione --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.registrazioni.create') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.registrazioni.create') }}">
                    <i class="fas fa-edit me-2"></i>Registrazione Unica
                    <small class="text-success ms-1">NEW</small>
                </a>
            </li>

            {{-- Libro Giornale --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.libro-giornale.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.libro-giornale.index') }}">
                    <i class="fas fa-book me-2"></i>Libro Giornale
                </a>
            </li>

            {{-- Piano dei Conti --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.piano-conti.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.piano-conti.index') }}">
                    <i class="fas fa-sitemap me-2"></i>Piano dei Conti
                </a>
            </li>

            {{-- Rate e Incassi --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.rate.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.rate.index') }}">
                    <i class="fas fa-receipt me-2"></i>Rate e Incassi
                    @if($contabilitaStats['rate_scadute'] > 0)
                        {!! App\Helpers\SidebarStatsHelper::getBadge($contabilitaStats['rate_scadute'], 'danger') !!}
                        <small class="text-danger">scadute</small>
                    @endif
                </a>
            </li>

            {{-- Bilanci e Report --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.bilanci.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.bilanci.index') }}">
                    <i class="fas fa-chart-line me-2"></i>Bilanci e Report
                </a>
            </li>

            {{-- Riconciliazione Bancaria --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.riconciliazioni.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.riconciliazioni.index') }}">
                    <i class="fas fa-university me-2"></i>Riconciliazione Bancaria
                    @if($contabilitaStats['riconciliazioni_pending'] > 0)
                        {!! App\Helpers\SidebarStatsHelper::getBadge($contabilitaStats['riconciliazioni_pending'], 'warning') !!}
                        <small class="text-warning">da fare</small>
                    @endif
                </a>
            </li>

            {{-- Gestione Millesimi --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.millesimi.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.millesimi.index') }}">
                    <i class="fas fa-percentage me-2"></i>Gestione Millesimi
                </a>
            </li>

            {{-- Protocolli --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.protocolli.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.protocolli.index') }}">
                    <i class="fas fa-list-ol me-2"></i>Protocolli
                </a>
            </li>

            {{-- Fiscale e F24 --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.fiscale.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.fiscale.index') }}">
                    <i class="fas fa-file-invoice-dollar me-2"></i>Fiscale e F24
                    @if($contabilitaStats['f24_in_scadenza'] > 0)
                        {!! App\Helpers\SidebarStatsHelper::getBadge($contabilitaStats['f24_in_scadenza'], 'danger') !!}
                        <small class="text-danger">in scadenza</small>
                    @endif
                </a>
            </li>

            {{-- Scadenzario --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.scadenzario.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.scadenzario.index') }}">
                    <i class="fas fa-calendar-alt me-2"></i>Scadenzario
                </a>
            </li>

            {{-- Backup e Restore --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.backup-contabile.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.backup-contabile.index') }}">
                    <i class="fas fa-download me-2"></i>Backup Contabile
                </a>
            </li>

            {{-- Audit Trail --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.audit.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.audit.index') }}">
                    <i class="fas fa-history me-2"></i>Audit Trail
                </a>
            </li>

            {{-- Importazione Dati --}}
            <li class="nav-item">
                <a class="nav-link text-dark {{ request()->routeIs('admin.importazioni.*') ? 'active bg-light' : '' }}" 
                   href="{{ route('admin.importazioni.index') }}">
                    <i class="fas fa-file-import me-2"></i>Importazione Dati
                </a>
            </li>

            {{-- Statistiche Avanzate --}}
            <li class="nav-item">
                <a class="nav-link text-dark" href="#" onclick="alert('Funzione in sviluppo: Statistiche Avanzate')">
                    <i class="fas fa-chart-pie me-2"></i>Statistiche Avanzate
                    @if($contabilitaStats['incassi_mese'] > 0)
                        <small class="text-success">€{{ number_format($contabilitaStats['incassi_mese'], 0, ',', '.') }}</small>
                    @endif
                </a>
            </li>

        </ul>
    </div>
</li>
