@php
    $vActive = request()->routeIs('vouchers.*');
    $usePayrollModule = \App\Models\Setting::get(auth()->user()->company_id, 'use_payroll_module', '0') === '1';
    $useFixedAssetsModule = \App\Models\Setting::get(auth()->user()->company_id, 'use_fixed_assets_module', '0') === '1';
    $useManufacturingModule = \App\Models\Setting::get(auth()->user()->company_id, 'use_manufacturing', '0') === '1';
    $listRoutes = ['coa.*', 'dimensions.*', 'masters.other-names.*', 'masters.currencies.*', 'masters.tax-codes.*', 'masters.price-levels.*', 'masters.opening-balance.*'];
    if (!$usePayrollModule) {
        $listRoutes[] = 'masters.employees.*';
    }
    $listActive = request()->routeIs(...$listRoutes);
    $payrollActive = request()->routeIs('payroll.*') || ($usePayrollModule && request()->routeIs('masters.employees.*'));
    $arActive = request()->routeIs('masters.customers.*');
    $apActive = request()->routeIs('masters.vendors.*');
    $invActive = request()->routeIs('inventory.*');
    $mfgActive = false;
    $fixedAssetsActive = request()->routeIs('fixed-assets.*');
    $bankActive = request()->routeIs('banking.*');
    $companyActive = request()->routeIs('admin.users.*', 'admin.roles.*', 'admin.quickbooks.*', 'admin.company-reset.*', 'admin.budgets.*');
    $qbImportActive = request()->routeIs('admin.quickbooks.*');
    $reportsActive = request()->routeIs('reports.*');
    $settingsActive = request()->routeIs('settings.*', 'subscription', 'admin.fiscal-years.*', 'admin.voucher-numbering.*', 'admin.audit-trail', 'settings.inventory', 'settings.arap');
@endphp

<nav id="topnav" aria-label="Module navigation">
    <div class="topnav-scroll">
        <ul class="topnav-menu">
            <li class="dropdown topnav-item topnav-item--compact">
                <button class="topnav-link dropdown-toggle {{ $vActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-receipt topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Voucher</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown topnav-dropdown--scroll">
                    @foreach([
                        ['cash_payment', 'Cash Payment', 'bi-cash-stack'],
                        ['bank_payment', 'Bank Payment', 'bi-bank'],
                        ['cash_receipt', 'Cash Receipt', 'bi-cash-coin'],
                        ['bank_receipt', 'Bank Receipt', 'bi-bank2'],
                        ['contra', 'Contra', 'bi-arrow-left-right'],
                        ['journal', 'Journal', 'bi-journal-text'],
                    ] as [$type, $typeName, $icon])
                    <li>
                        <a class="dropdown-item {{ request()->route('voucherType') === $type ? 'active' : '' }}"
                           href="{{ route('vouchers.index', $type) }}">
                            <i class="bi {{ $icon }} me-2"></i>{{ $typeName }}
                        </a>
                    </li>
                    @endforeach
                </ul>
            </li>

            <li class="dropdown topnav-item topnav-item--compact">
                <button class="topnav-link dropdown-toggle {{ $listActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-card-list topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">List</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown topnav-dropdown--scroll">
                    <li><a class="dropdown-item {{ request()->routeIs('coa.*') ? 'active' : '' }}" href="{{ route('coa.index') }}"><i class="bi bi-diagram-3 me-2"></i>Chart of Accounts</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('dimensions.*') ? 'active' : '' }}" href="{{ route('dimensions.index') }}"><i class="bi bi-layers me-2"></i>Dimensions</a></li>
                    @if(!$usePayrollModule)
                    <li><a class="dropdown-item {{ request()->routeIs('masters.employees.*') ? 'active' : '' }}" href="{{ route('masters.employees.index') }}"><i class="bi bi-people me-2"></i>Employees</a></li>
                    @endif
                    <li><a class="dropdown-item {{ request()->routeIs('masters.other-names.*') ? 'active' : '' }}" href="{{ route('masters.other-names.index') }}"><i class="bi bi-person-lines-fill me-2"></i>Other Names</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('masters.currencies.*') ? 'active' : '' }}" href="{{ route('masters.currencies.index') }}"><i class="bi bi-currency-exchange me-2"></i>Currencies</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('masters.tax-codes.*') ? 'active' : '' }}" href="{{ route('masters.tax-codes.index') }}"><i class="bi bi-percent me-2"></i>Tax Codes</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('masters.price-levels.*') ? 'active' : '' }}" href="{{ route('masters.price-levels.index') }}"><i class="bi bi-tags me-2"></i>Price Levels</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('masters.opening-balance.*') ? 'active' : '' }}" href="{{ route('masters.opening-balance.index', ['tab' => 'accounts']) }}"><i class="bi bi-wallet2 me-2"></i>Opening Balance</a></li>
                </ul>
            </li>

            <li class="dropdown topnav-item topnav-item--compact">
                <button class="topnav-link dropdown-toggle {{ $arActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-arrow-down-circle topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">A/R</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown">
                    <li><a class="dropdown-item {{ request()->routeIs('masters.customers.*') ? 'active' : '' }}" href="{{ route('masters.customers.index') }}"><i class="bi bi-person-badge me-2"></i>Customers</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-file-earmark-text me-2"></i>Quotations</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-cart-check me-2"></i>Sales Orders</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-truck me-2"></i>Delivery Notes</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-receipt-cutoff me-2"></i>Sales Invoices</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-arrow-return-left me-2"></i>Sales Returns</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-cash-stack me-2"></i>Cash Sales</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-credit-card me-2"></i>Receive Payment</a></li>
                </ul>
            </li>

            <li class="dropdown topnav-item topnav-item--compact">
                <button class="topnav-link dropdown-toggle {{ $apActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-arrow-up-circle topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">A/P</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown">
                    <li><a class="dropdown-item {{ request()->routeIs('masters.vendors.*') ? 'active' : '' }}" href="{{ route('masters.vendors.index') }}"><i class="bi bi-shop me-2"></i>Vendors</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-clipboard-check me-2"></i>Purchase Requisitions</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-bag-check me-2"></i>Purchase Orders</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-box-arrow-in-down me-2"></i>GRN</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-file-earmark-ruled me-2"></i>Bills</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-wallet me-2"></i>Bill Payments</a></li>
                </ul>
            </li>

            <li class="dropdown topnav-item topnav-item--module">
                <button class="topnav-link dropdown-toggle {{ $invActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false" title="Inventory">
                    <i class="bi bi-box-seam topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Inventory</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown">
                    <li><a class="dropdown-item {{ request()->routeIs('inventory.items.*') ? 'active' : '' }}" href="{{ route('inventory.items.index') }}"><i class="bi bi-box me-2"></i>Items</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('inventory.categories.*') ? 'active' : '' }}" href="{{ route('inventory.categories.index') }}"><i class="bi bi-folder2 me-2"></i>Item Categories</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('inventory.warehouses.*') ? 'active' : '' }}" href="{{ route('inventory.warehouses.index') }}"><i class="bi bi-house-door me-2"></i>Warehouses</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('inventory.uoms.*') ? 'active' : '' }}" href="{{ route('inventory.uoms.index') }}"><i class="bi bi-rulers me-2"></i>Units of Measure</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-sliders me-2"></i>Stock Adjustment</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-arrow-repeat me-2"></i>Warehouse Transfer</a></li>
                </ul>
            </li>

            @if($useManufacturingModule)
            <li class="dropdown topnav-item topnav-item--module">
                <button class="topnav-link dropdown-toggle {{ $mfgActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false" title="Manufacturing">
                    <i class="bi bi-gear-wide-connected topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Mfg</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown">
                    <li><a class="dropdown-item" href="#"><i class="bi bi-diagram-2 me-2"></i>Bill of Materials</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-hammer me-2"></i>Production Orders</a></li>
                </ul>
            </li>
            @endif

            @if($useFixedAssetsModule && auth()->user()->hasPermission('fixed_assets', 'view'))
            <li class="dropdown topnav-item topnav-item--module">
                <button class="topnav-link dropdown-toggle {{ $fixedAssetsActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false" title="Fixed Assets">
                    <i class="bi bi-buildings topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Assets</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown">
                    @include('partials.fixed-assets-menu-items', ['variant' => 'dropdown'])
                </ul>
            </li>
            @endif

            @if($usePayrollModule && (auth()->user()->hasPermission('payroll', 'view') || auth()->user()->hasPermission('masters', 'view')))
            <li class="dropdown topnav-item">
                <button class="topnav-link dropdown-toggle {{ $payrollActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-wallet2 topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Payroll</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown topnav-dropdown--scroll">
                    @include('partials.payroll-menu-items', ['variant' => 'dropdown'])
                </ul>
            </li>
            @endif

            @if(auth()->user()->hasPermission('banking', 'view'))
            <li class="dropdown topnav-item">
                <button class="topnav-link dropdown-toggle {{ $bankActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-bank topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Banking</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown">
                    @include('partials.banking-menu-items', ['variant' => 'dropdown'])
                </ul>
            </li>
            @endif

            <li class="dropdown topnav-item">
                <button class="topnav-link dropdown-toggle {{ $companyActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-building-gear topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Company</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown" data-bs-auto-close="outside">
                    <li><a class="dropdown-item {{ request()->routeIs('admin.users.*') ? 'active' : '' }}" href="{{ route('admin.users.index') }}"><i class="bi bi-person-gear me-2"></i>Users</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('admin.roles.*') ? 'active' : '' }}" href="{{ route('admin.roles.index') }}"><i class="bi bi-shield-lock me-2"></i>Roles &amp; Permissions</a></li>
                    @if(auth()->user()->hasPermission('budget', 'view'))
                    <li><a class="dropdown-item {{ request()->routeIs('admin.budgets.*') ? 'active' : '' }}" href="{{ route('admin.budgets.index') }}"><i class="bi bi-calculator me-2"></i>Budgets</a></li>
                    @endif
                    <li class="topnav-inline-group">
                        <button type="button"
                                class="dropdown-item topnav-inline-toggle {{ $qbImportActive ? 'active' : '' }}"
                                data-bs-toggle="collapse"
                                data-bs-target="#topnavQbImport"
                                aria-expanded="{{ $qbImportActive ? 'true' : 'false' }}"
                                aria-controls="topnavQbImport">
                            <i class="bi bi-cloud-download me-2"></i><span>Import from QuickBooks</span><i class="bi bi-chevron-right topnav-inline-caret" aria-hidden="true"></i>
                        </button>
                        <div class="collapse topnav-inline-submenu {{ $qbImportActive ? 'show' : '' }}" id="topnavQbImport">
                            <a class="dropdown-item topnav-inline-sub {{ request()->routeIs('admin.quickbooks.list-import') ? 'active' : '' }}" href="{{ route('admin.quickbooks.list-import') }}"><i class="bi bi-list-check me-2"></i>QuickBooks List Import</a>
                            <a class="dropdown-item topnav-inline-sub {{ request()->routeIs('admin.quickbooks.data-import') ? 'active' : '' }}" href="{{ route('admin.quickbooks.data-import') }}"><i class="bi bi-database-down me-2"></i>QuickBooks Data Import</a>
                        </div>
                    </li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item text-danger {{ request()->routeIs('admin.company-reset.transactions*') ? 'active' : '' }}" href="{{ route('admin.company-reset.transactions') }}"><i class="bi bi-trash me-2"></i>Delete All Transactions</a></li>
                    <li><a class="dropdown-item text-danger {{ request()->routeIs('admin.company-reset.listings*') ? 'active' : '' }}" href="{{ route('admin.company-reset.listings') }}"><i class="bi bi-trash3 me-2"></i>Delete all Listings</a></li>
                </ul>
            </li>

            <li class="dropdown topnav-item">
                <button class="topnav-link dropdown-toggle {{ $reportsActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-bar-chart-line topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Reports</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown topnav-dropdown--scroll">
                    <li><a class="dropdown-item {{ request()->routeIs('reports.profit-loss') ? 'active' : '' }}" href="{{ route('reports.profit-loss') }}"><i class="bi bi-graph-up-arrow me-2"></i>Profit &amp; Loss</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('reports.balance-sheet') ? 'active' : '' }}" href="{{ route('reports.balance-sheet') }}"><i class="bi bi-clipboard-data me-2"></i>Balance Sheet</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('reports.journal') ? 'active' : '' }}" href="{{ route('reports.journal') }}"><i class="bi bi-journal-bookmark me-2"></i>Journal Report</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('reports.trial-balance') ? 'active' : '' }}" href="{{ route('reports.trial-balance') }}"><i class="bi bi-layout-three-columns me-2"></i>Trial Balance</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('reports.general-ledger') ? 'active' : '' }}" href="{{ route('reports.general-ledger') }}"><i class="bi bi-book me-2"></i>General Ledger</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-hourglass-split me-2"></i>Customer Aging</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-hourglass me-2"></i>Vendor Aging</a></li>
                    <li><a class="dropdown-item" href="#"><i class="bi bi-boxes me-2"></i>Stock Summary</a></li>
                </ul>
            </li>

            <li class="dropdown topnav-item">
                <button class="topnav-link dropdown-toggle {{ $settingsActive ? 'active' : '' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-gear topnav-link-icon" aria-hidden="true"></i><span class="topnav-link-label">Settings</span><i class="bi bi-chevron-down topnav-link-caret" aria-hidden="true"></i>
                </button>
                <ul class="dropdown-menu topnav-dropdown">
                    <li><a class="dropdown-item {{ request()->routeIs('settings.*') ? 'active' : '' }}" href="{{ route('settings.general') }}"><i class="bi bi-sliders me-2"></i>Company Settings</a></li>
                    @if(auth()->user()->is_admin)
                    <li><a class="dropdown-item {{ request()->routeIs('subscription') ? 'active' : '' }}" href="{{ route('subscription') }}"><i class="bi bi-credit-card me-2"></i>Subscription</a></li>
                    @endif
                    <li><a class="dropdown-item {{ request()->routeIs('admin.fiscal-years.*') ? 'active' : '' }}" href="{{ route('admin.fiscal-years.index') }}"><i class="bi bi-calendar-check me-2"></i>Period Closing</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('admin.voucher-numbering.*') ? 'active' : '' }}" href="{{ route('admin.voucher-numbering.index') }}"><i class="bi bi-hash me-2"></i>Voucher Numbering</a></li>
                    <li><a class="dropdown-item {{ request()->routeIs('admin.audit-trail') ? 'active' : '' }}" href="{{ route('admin.audit-trail') }}"><i class="bi bi-clock-history me-2"></i>Audit Trail</a></li>
                </ul>
            </li>
        </ul>
    </div>
</nav>
