<?php
require_once __DIR__ . '/auth.php';
$BASE = app_base_path();

$currentPath = $_SERVER['PHP_SELF'];
$currentPage = basename($currentPath);

/* =====================================
   NETWORK OPS PENDING COMPLETIONS COUNT
===================================== */
$networkPendingCount = 0;

if (hasPermission('manage_network_jobs')) {
    try {
        $stmt = $pdo->prepare("
            SELECT COUNT(*)
            FROM network_jobs
            WHERE status = 'COMPLETION_PENDING'
        ");
        $stmt->execute();
        $networkPendingCount = (int)$stmt->fetchColumn();
    } catch (Exception $e) {
        $networkPendingCount = 0;
    }
}

?>

<div class="sidebar" id="sidebar">

    <!-- ================= DASHBOARD ================= -->
<?php if (hasPermission('view_dashboard')): ?>

<div class="sidebar-section">Overview</div>

<a href="<?= $BASE ?>/dashboard.php"
   class="<?= ($currentPage=='dashboard.php' || $currentPage=='index.php') ? 'active' : '' ?>">
    <i class="fa fa-home"></i> Dashboard
</a>

<a href="<?= $BASE ?>/executive_dashboard.php"
   class="<?= ($currentPage=='executive_dashboard.php') ? 'active' : '' ?>">
    <i class="fa fa-chart-pie"></i> Executive Dashboard
</a>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= INVENTORY ================= -->
<?php if (hasPermission('view_inventory')): ?>

<div class="sidebar-section">Inventory</div>

<div class="menu-parent <?= 
    str_contains($_SERVER['SCRIPT_NAME'], '/modules/sales_inventory/') ||
    str_contains($_SERVER['SCRIPT_NAME'], '/modules/inventory_returns/') ||
    str_contains($_SERVER['SCRIPT_NAME'], '/modules/suppliers/') ||
    str_contains($_SERVER['SCRIPT_NAME'], '/modules/purchase_orders/') ||
    str_contains($_SERVER['SCRIPT_NAME'], '/modules/supplier_invoices/')
    ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-box"></i> Inventory</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <a href="/core/modules/sales_inventory/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/sales_inventory/index.php') ? 'active' : '' ?>">
           <i class="fa fa-warehouse"></i> Sales Inventory
        </a>

        <a href="/core/modules/sales_inventory/add_product.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/sales_inventory/add_product.php') ? 'active' : '' ?>">
           <i class="fa fa-plus-circle"></i> Add Product
        </a>

        <a href="/core/modules/sales_inventory/stock_value.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/sales_inventory/stock_value.php') ? 'active' : '' ?>">
           <i class="fa fa-chart-bar"></i> Stock Value
        </a>

        <a href="/core/modules/inventory_returns/dashboard.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/inventory_returns/') ? 'active' : '' ?>">
           <i class="fa fa-undo"></i> Inventory Returns
        </a>

        <a href="/core/modules/suppliers/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/suppliers/') ? 'active' : '' ?>">
           <i class="fa fa-truck"></i> Suppliers
        </a>

        <a href="/core/modules/purchase_orders/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/purchase_orders/') ? 'active' : '' ?>">
           <i class="fa fa-shopping-cart"></i> Purchase Orders
        </a>

        <a href="/core/modules/supplier_invoices/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/supplier_invoices/') ? 'active' : '' ?>">
           <i class="fa fa-file-invoice"></i> Supplier Invoices
        </a>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>

<!-- ================= OPERATIONS ================= -->
<?php if (hasPermission('inventory.manage')): ?>

<div class="sidebar-section">Operations</div>

<div class="menu-parent <?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/operations/') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-tools"></i> Operational Meters</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <a href="<?= $BASE ?>/../modules/operations/meters.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/operations/meters.php') ? 'active' : '' ?>">
           <i class="fa fa-tachometer-alt"></i> All Meters
        </a>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= COMPANIES ================= -->
<?php if (
    hasPermission('view_companies') ||
    hasPermission('add_company') ||
    hasPermission('edit_company')
): ?>

<div class="sidebar-section">Companies</div>

<div class="menu-parent <?= in_array($currentPage,
    ['companies.php','add_company.php']
) ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-building"></i> Companies</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('view_companies')): ?>
        <a href="<?= $BASE ?>/companies.php"
           class="<?= ($currentPage=='companies.php')?'active':'' ?>">
           <i class="fa fa-list"></i> Company List
        </a>
        <?php endif; ?>

        <?php if (hasPermission('add_company')): ?>
        <a href="<?= $BASE ?>/add_company.php"
           class="<?= ($currentPage=='add_company.php')?'active':'' ?>">
           <i class="fa fa-plus"></i> Add Company
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= REGISTRATIONS ================= -->
<?php if (
    hasPermission('view_registrations') ||
    hasPermission('approve_registrations')
): ?>

<div class="sidebar-section">Registrations</div>

<div class="menu-parent <?= in_array($currentPage,
    ['register_meter.php','approve_registrations.php']
) ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-clipboard-list"></i> Registrations</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('view_registrations')): ?>
        <a href="<?= $BASE ?>/register_meter.php"
           class="<?= ($currentPage=='register_meter.php')?'active':'' ?>">
           <i class="fa fa-plus-circle"></i> New Registration
        </a>
        <?php endif; ?>

        <?php if (hasPermission('approve_registrations')): ?>
        <a href="<?= $BASE ?>/approve_registrations.php"
           class="<?= ($currentPage=='approve_registrations.php')?'active':'' ?>">
           <i class="fa fa-check-circle"></i> Approve Registrations
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= REPLACEMENTS ================= -->
<?php if (
    hasPermission('request_replacement') ||
    hasPermission('approve_replacements') ||
    hasPermission('view_replacement_history')
): ?>

<div class="sidebar-section">Replacements</div>

<div class="menu-parent <?= in_array($currentPage,
    ['replacement_request.php','approve_replacements.php','replacement_history.php']
) ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-sync-alt"></i> Replacements</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('request_replacement')): ?>
        <a href="<?= $BASE ?>/replacement_request.php"
           class="<?= ($currentPage=='replacement_request.php')?'active':'' ?>">
           <i class="fa fa-exchange-alt"></i> Replacement Requests
        </a>
        <?php endif; ?>

        <?php if (hasPermission('approve_replacements')): ?>
        <a href="<?= $BASE ?>/approve_replacements.php"
           class="<?= ($currentPage=='approve_replacements.php')?'active':'' ?>">
           <i class="fa fa-check"></i> Approve Replacements
        </a>
        <?php endif; ?>

        <?php if (hasPermission('view_replacement_history')): ?>
        <a href="<?= $BASE ?>/replacement_history.php"
           class="<?= ($currentPage=='replacement_history.php')?'active':'' ?>">
           <i class="fa fa-history"></i> Replacement History
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>

   <!-- ================= PROJECTS ================= -->
<?php if (
    hasPermission('view_projects') ||
    hasPermission('manage_projects')
): ?>

<div class="sidebar-section">Projects</div>

<div class="menu-parent <?= in_array($currentPage,
    ['projects.php','add_project.php','project_view.php']
) ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-folder-open"></i> Projects</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('view_projects')): ?>
        <a href="<?= $BASE ?>/projects.php"
           class="<?= ($currentPage=='projects.php')?'active':'' ?>">
           <i class="fa fa-list"></i> Project List
        </a>
        <?php endif; ?>

        <?php if (hasPermission('manage_projects')): ?>
        <a href="<?= $BASE ?>/add_project.php"
           class="<?= ($currentPage=='add_project.php')?'active':'' ?>">
           <i class="fa fa-plus"></i> Add Project
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= TASKS ================= -->
<?php if (
    hasPermission('view_tasks') ||
    hasPermission('manage_tasks')
): ?>

<div class="sidebar-section">Tasks</div>

<div class="menu-parent <?= in_array($currentPage,
    ['project_tasks.php','add_task.php']
) ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-tasks"></i> Tasks</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('view_tasks')): ?>
        <a href="<?= $BASE ?>/project_tasks.php"
           class="<?= ($currentPage=='project_tasks.php')?'active':'' ?>">
           <i class="fa fa-columns"></i> Task Board
        </a>
        <?php endif; ?>

        <?php if (hasPermission('manage_tasks')): ?>
        <a href="<?= $BASE ?>/add_task.php"
           class="<?= ($currentPage=='add_task.php')?'active':'' ?>">
           <i class="fa fa-plus-circle"></i> Add Task
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= OPERATIONAL HUB ================= -->
<?php if (
    hasPermission('view_operations') ||
    hasPermission('create_ticket')
): ?>

<div class="sidebar-section">Operations</div>

<div class="menu-parent <?= 
    (str_contains($_SERVER['SCRIPT_NAME'], '/operations/') 
    || str_contains($_SERVER['SCRIPT_NAME'], '/trade_jobs/')) 
    ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-tools"></i> Operational Hub</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('view_operations')): ?>
        <a href="<?= $BASE ?>/operations/dashboard.php"
           class="<?= ($currentPage=='dashboard.php') ? 'active' : '' ?>">
           <i class="fa fa-chart-line"></i> Operations Dashboard
        </a>

        <a href="<?= $BASE ?>/operations/tickets/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/operations/tickets/') ? 'active' : '' ?>">
           <i class="fa fa-ticket-alt"></i> Ticket Queue
        </a>

        <a href="<?= $BASE ?>/operations/reports/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/operations/reports/') ? 'active' : '' ?>">
           <i class="fa fa-chart-bar"></i> Operations Reports
        </a>
        <?php endif; ?>

        <?php if (hasPermission('trade_jobs.manage')): ?>
        <a href="<?= $BASE ?>/trade_jobs/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/trade_jobs/') ? 'active' : '' ?>">
           <i class="fa fa-briefcase"></i> Trade Jobs
        </a>
        <?php endif; ?>

        <?php if (hasPermission('create_ticket')): ?>
        <a href="<?= $BASE ?>/operations/tickets/create.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/operations/tickets/create.php') ? 'active' : '' ?>">
           <i class="fa fa-plus"></i> Create Ticket
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= NETWORK OPS ================= -->
<?php if (hasPermission('manage_network_jobs')): ?>

<div class="sidebar-section">Network Ops</div>

<div class="menu-parent <?= str_contains($_SERVER['SCRIPT_NAME'], '/operations/network/') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-network-wired"></i> National Network OPS</span>

        <?php if ($networkPendingCount > 0): ?>
        <span style="background:#dc2626;color:white;padding:2px 8px;border-radius:20px;margin-left:6px;">
            <?= $networkPendingCount ?>
        </span>
        <?php endif; ?>

        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <a href="<?= $BASE ?>/operations/network/dashboard.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/operations/network/dashboard.php') ? 'active' : '' ?>">
           <i class="fa fa-chart-line"></i> OPS Dashboard
        </a>

        <a href="<?= $BASE ?>/operations/network/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/operations/network/index.php') ? 'active' : '' ?>">
           <i class="fa fa-list"></i> All Network Jobs
        </a>

        <a href="<?= $BASE ?>/operations/network/create.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/operations/network/create.php') ? 'active' : '' ?>">
           <i class="fa fa-plus"></i> Create Network Job
        </a>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= FIELD OPS ================= -->
<?php if (hasPermission('view_network_invites')): ?>

<div class="sidebar-section">Field Ops</div>

<div class="menu-parent <?= str_contains($currentPath, '/installer/network/') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-user-cog"></i> National Field OPS</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <a href="<?= $BASE ?>/installer/network/dashboard.php"
           class="<?= str_contains($currentPath, '/installer/network/dashboard.php') ? 'active' : '' ?>">
           <i class="fa fa-chart-line"></i> Dashboard
        </a>

        <a href="<?= $BASE ?>/installer/network/invites.php"
           class="<?= str_contains($currentPath, '/installer/network/invites.php') ? 'active' : '' ?>">
           <i class="fa fa-envelope"></i> My Invites
        </a>

        <a href="<?= $BASE ?>/installer/network/my_jobs.php"
           class="<?= str_contains($currentPath, '/installer/network/my_jobs.php') ? 'active' : '' ?>">
           <i class="fa fa-briefcase"></i> My Jobs
        </a>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= METERS ================= -->
<?php if (
    hasPermission('view_meters') ||
    hasPermission('register_meters') ||
    hasPermission('view_tenants')
): ?>

<div class="sidebar-section">Meters</div>

<div class="menu-parent <?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/meters/') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-tachometer-alt"></i> Meters</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('view_meters')): ?>
        <a href="<?= $BASE ?>/../modules/meters/meters.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/meters/meters.php') ? 'active' : '' ?>">
           <i class="fa fa-list"></i> All Meters
        </a>
        <?php endif; ?>

        <?php if (hasPermission('register_meters')): ?>
        <a href="<?= $BASE ?>/../modules/meters/meter_wizard.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/meters/meter_wizard.php') ? 'active' : '' ?>">
           <i class="fa fa-plus-circle"></i> Register Meter
        </a>
        <?php endif; ?>

        <?php if (hasPermission('view_tenants')): ?>
        <a href="<?= $BASE ?>/../modules/meters/tenants.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/meters/tenants.php') ? 'active' : '' ?>">
           <i class="fa fa-users"></i> Tenant Management
        </a>
        <?php endif; ?>

        <?php if (hasPermission('view_tenant_history')): ?>
        <a href="<?= $BASE ?>/../modules/meters/tenant_history.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/meters/tenant_history.php') ? 'active' : '' ?>">
           <i class="fa fa-history"></i> Tenant History
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>
<!-- ================= FINANCE ================= -->
<?php if (
    hasPermission('finance.manage') ||
    hasPermission('view_finance')
): ?>

<div class="sidebar-section">Finance</div>

<div class="menu-parent <?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/finance/') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-coins"></i> Finance</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <a href="<?= $BASE ?>/../modules/finance/dashboard.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/finance/dashboard.php') ? 'active' : '' ?>">
           <i class="fa fa-chart-pie"></i> Finance Dashboard
        </a>

        <a href="<?= $BASE ?>/../modules/finance/quotations/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/finance/quotations/') ? 'active' : '' ?>">
           <i class="fa fa-file-signature"></i> Quotations
        </a>

        <a href="<?= $BASE ?>/../modules/finance/invoices/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/finance/invoices/') ? 'active' : '' ?>">
           <i class="fa fa-file-invoice"></i> Invoices
        </a>

        <a href="<?= $BASE ?>/../modules/finance/payments/history.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/finance/payments/') ? 'active' : '' ?>">
           <i class="fa fa-credit-card"></i> Payments
        </a>

        <a href="<?= $BASE ?>/../modules/finance/reports/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/finance/reports/') ? 'active' : '' ?>">
           <i class="fa fa-chart-line"></i> Finance Reports
        </a>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>

<!-- ================= BILLING ================= -->
<?php if (hasPermission('view_billing')): ?>

<div class="sidebar-section">Billing</div>

<div class="menu-parent <?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-file-invoice"></i> Billing</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <a href="<?= $BASE ?>/../modules/billing/dashboard.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/dashboard.php') ? 'active' : '' ?>">
           <i class="fa fa-chart-line"></i> Billing Dashboard
        </a>

        <a href="<?= $BASE ?>/../modules/billing/customers/create.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/customers/') ? 'active' : '' ?>">
           <i class="fa fa-user-plus"></i> Add Customer
        </a>

        <a href="<?= $BASE ?>/../modules/billing/accounts/create.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/accounts/') ? 'active' : '' ?>">
           <i class="fa fa-id-card"></i> Add Account
        </a>
        
        <a href="<?= $BASE ?>/../modules/billing/accounts/accounts.php"
   class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/accounts/accounts.php') ? 'active' : '' ?>">
    <i class="fa fa-layer-group"></i> Accounts / Units
</a>

<a href="<?= $BASE ?>/../modules/billing/accounts/units/index.php"
   class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/accounts/units/') ? 'active' : '' ?>">
    <i class="fa fa-layer-group"></i> Unit Management
</a>

        <a href="<?= $BASE ?>/../modules/billing/meter_setup/"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/meter_setup/') ? 'active' : '' ?>">
           <i class="fa fa-sliders-h"></i> Meter Setup
        </a>

        <a href="<?= $BASE ?>/../modules/billing/readings/"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/readings/') ? 'active' : '' ?>">
           <i class="fa fa-tachometer-alt"></i> Meter Readings
        </a>

        <a href="<?= $BASE ?>/../modules/billing/tariffs/"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/tariffs/') ? 'active' : '' ?>">
           <i class="fa fa-percentage"></i> Tariffs
        </a>

        <a href="<?= $BASE ?>/../modules/billing/simulate.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/simulate.php') ? 'active' : '' ?>">
           <i class="fa fa-calculator"></i> Billing Simulator
        </a>

        <a href="<?= $BASE ?>/../modules/billing/billing_runs/"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/billing_runs/') ? 'active' : '' ?>">
           <i class="fa fa-play-circle"></i> Billing Runs
        </a>

        <a href="<?= $BASE ?>/../modules/billing/invoices/invoices.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/invoices/') ? 'active' : '' ?>">
           <i class="fa fa-file-invoice-dollar"></i> Invoices
        </a>

        <?php if (hasPermission('view_statements')): ?>
        <a href="<?= $BASE ?>/../modules/billing/statements/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/statements/') ? 'active' : '' ?>">
           <i class="fa fa-file-alt"></i> Statements
        </a>
        <?php endif; ?>

        <?php if (hasPermission('view_reports')): ?>
        <a href="<?= $BASE ?>/../modules/billing/reports/transaction.php?invoice_id=1"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/reports/transaction.php') ? 'active' : '' ?>">
           <i class="fa fa-exchange-alt"></i> Transaction
        </a>

        <a href="<?= $BASE ?>/../modules/billing/reports/aging.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/reports/aging.php') ? 'active' : '' ?>">
           <i class="fa fa-clock"></i> Aging
        </a>

        <a href="<?= $BASE ?>/../modules/billing/reports/customer_aging.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/reports/customer_aging.php') ? 'active' : '' ?>">
           <i class="fa fa-users"></i> Customer Aging
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>

<!-- ================= SETTLEMENT ================= -->
<?php if (hasPermission('view_settlement')): ?>

<div class="sidebar-section">Settlement</div>

<div class="menu-parent <?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/settlements/') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-hand-holding-dollar"></i> Settlement</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <a href="<?= $BASE ?>/../modules/settlements/index.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/settlements/index.php') ? 'active' : '' ?>">
           <i class="fa fa-chart-line"></i> Dashboard
        </a>

        <a href="<?= $BASE ?>/../modules/settlements/upload.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/settlements/upload.php') ? 'active' : '' ?>">
           <i class="fa fa-upload"></i> Upload CSV
        </a>
        
        <a href="<?= $BASE ?>/../modules/settlements/history.php"
   class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/settlements/history.php') ? 'active' : '' ?>">
   <i class="fa fa-clock-rotate-left"></i> Settlement History
</a>
<a href="<?= $BASE ?>/../modules/billing/index.php"
   class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/billing/') ? 'active' : '' ?>">
   <i class="fa fa-file-invoice-dollar"></i> Billing Runs
</a>

        <a href="<?= $BASE ?>/../modules/settlements/view.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/settlements/view.php') ? 'active' : '' ?>">
           <i class="fa fa-eye"></i> Settlement Preview
        </a>

        <a href="<?= $BASE ?>/../modules/settlements/banking.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/modules/settlements/banking.php') ? 'active' : '' ?>">
           <i class="fa fa-university"></i> Banking Management
        </a>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= REPORTS ================= -->
<?php if (hasPermission('view_activity')): ?>

<div class="sidebar-section">Reports</div>

<div class="menu-parent <?= ($currentPage=='activity.php') ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-chart-bar"></i> Reports</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">
        <a href="<?= $BASE ?>/activity.php"
           class="<?= ($currentPage=='activity.php') ? 'active' : '' ?>">
           <i class="fa fa-list"></i> Activity Logs
        </a>
    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= ADMINISTRATION ================= -->
<?php if (
    hasPermission('manage_users') ||
    hasPermission('manage_roles') ||
    hasPermission('manage_installers')
): ?>

<div class="sidebar-section">Administration</div>

<div class="menu-parent <?= in_array($currentPage,
    ['users.php','roles.php','applications.php','invites.php']
) ? 'open active-parent' : '' ?>">

    <div class="menu-toggle">
        <span><i class="fa fa-cogs"></i> Administration</span>
        <span class="arrow">▾</span>
    </div>

    <div class="submenu">

        <?php if (hasPermission('manage_users')): ?>
        <a href="<?= $BASE ?>/users.php"
           class="<?= ($currentPage=='users.php')?'active':'' ?>">
           <i class="fa fa-users"></i> Users
        </a>
        <?php endif; ?>

        <?php if (hasPermission('manage_roles')): ?>
        <a href="<?= $BASE ?>/roles.php"
           class="<?= ($currentPage=='roles.php')?'active':'' ?>">
           <i class="fa fa-user-shield"></i> Roles
        </a>
        <?php endif; ?>

        <?php if (hasPermission('manage_installers')): ?>
        <a href="/core/installers/applications.php"
           class="<?= str_contains($_SERVER['SCRIPT_NAME'], '/installers/applications.php') ? 'active' : '' ?>">
           <i class="fa fa-file-alt"></i> Installer Applications
        </a>

        <a href="/core/installers/invites.php"
           class="<?= str_contains($currentPath, '/installers/invites.php') ? 'active' : '' ?>">
           <i class="fa fa-paper-plane"></i> Installer Invites
        </a>
        <?php endif; ?>

    </div>

</div>

<div class="sidebar-divider"></div>

<?php endif; ?>


<!-- ================= LOGOUT ================= -->
<a href="<?= $BASE ?>/logout.php">
    <i class="fa fa-sign-out-alt"></i> Logout
</a>

</div>