<?php
/**
 * Pacific Logistics — canonical sidebar.
 * Every menu item is gated by UserGroups::can(), so this same file is safe
 * for _sidebar_default.php (all non-admin roles fall back to that).
 * Rsflow-specific sections (Cash-out / Goods & Sales / Works / Services /
 * CRM / Tenders) are deliberately dropped — they're not part of Pacific Iter 1.
 */
$is  = fn(string $s): bool => strpos($_SERVER['REQUEST_URI'] ?? '/', '/' . $s) !== false;
$can = fn(string $c, string $a = 'index'): bool => UserGroups::can($c, $a);
?>

<a href="<?= PROOT; ?>Dashboard" class="rs-nav-link <?= $is('Dashboard') ? 'active' : ''; ?>">
    <i class="fas fa-tachometer-alt"></i><span>Dashboard</span>
</a>

<!-- ── Dispatch (top billing — the operational hub) ────────────────────── -->
<?php if ($can('TripRequests') || $can('Routes') || $can('Trips')): ?>
<div class="rs-nav-section">Dispatch <i class="fas fa-chevron-down section-chevron"></i></div>
<div class="rs-nav-section-items">
    <?php if ($can('Trips','board')):  ?><a href="<?= PROOT; ?>Trips/board"   class="rs-nav-link <?= $is('Trips/board')  ?'active':''; ?>"><i class="fas fa-th-list"></i><span>Dispatch Board</span></a><?php endif; ?>
    <?php if ($can('TripRequests')):   ?><a href="<?= PROOT; ?>TripRequests"  class="rs-nav-link <?= $is('TripRequests') ?'active':''; ?>"><i class="fas fa-clipboard-list"></i><span>Trip Requests</span></a><?php endif; ?>
    <?php if ($can('Trips')):          ?><a href="<?= PROOT; ?>Trips"         class="rs-nav-link <?= ($is('Trips') && !$is('Trips/board'))?'active':''; ?>"><i class="fas fa-truck-loading"></i><span>Trips</span></a><?php endif; ?>
    <?php if ($can('Routes')):         ?><a href="<?= PROOT; ?>Routes"        class="rs-nav-link <?= $is('Routes')       ?'active':''; ?>"><i class="fas fa-route"></i><span>Routes</span></a><?php endif; ?>
</div>
<?php endif; ?>

<!-- ── Fleet ──────────────────────────────────────────────────────────── -->
<?php if ($can('Trucks') || $can('Drivers') || $can('DriverAssignments') || $can('FuelEntries') || $can('WorkRequests') || $can('WorkOrders') || $can('PmDashboard') || $can('PmTemplates') || $can('DocExpiries') || $can('FleetAnalytics') || $can('GpsDashboard') || $can('Vendors')): ?>
<div class="rs-nav-section">Fleet <i class="fas fa-chevron-down section-chevron"></i></div>
<div class="rs-nav-section-items">
    <?php if ($can('Trucks')):            ?><a href="<?= PROOT; ?>Trucks"             class="rs-nav-link <?= $is('Trucks')            ?'active':''; ?>"><i class="fas fa-truck"></i><span>Trucks</span></a><?php endif; ?>
    <?php if ($can('Drivers')):           ?><a href="<?= PROOT; ?>Drivers"            class="rs-nav-link <?= $is('Drivers')           ?'active':''; ?>"><i class="fas fa-id-card"></i><span>Drivers</span></a><?php endif; ?>
    <?php if ($can('DriverAssignments')): ?><a href="<?= PROOT; ?>DriverAssignments"  class="rs-nav-link <?= $is('DriverAssignments') ?'active':''; ?>"><i class="fas fa-exchange-alt"></i><span>Assignments</span></a><?php endif; ?>
    <?php if ($can('FuelEntries')):       ?><a href="<?= PROOT; ?>FuelEntries"        class="rs-nav-link <?= $is('FuelEntries')       ?'active':''; ?>"><i class="fas fa-gas-pump"></i><span>Fuel</span></a><?php endif; ?>
    <?php if ($can('WorkRequests')):      ?><a href="<?= PROOT; ?>WorkRequests"       class="rs-nav-link <?= $is('WorkRequests')      ?'active':''; ?>"><i class="fas fa-hand-paper"></i><span>Work Requests</span></a><?php endif; ?>
    <?php if ($can('PmDashboard')):       ?><a href="<?= PROOT; ?>PmDashboard"        class="rs-nav-link <?= $is('PmDashboard')       ?'active':''; ?>"><i class="fas fa-tools"></i><span>PM Dashboard</span></a><?php endif; ?>
    <?php if ($can('PmTemplates')):       ?><a href="<?= PROOT; ?>PmTemplates"        class="rs-nav-link <?= $is('PmTemplates')       ?'active':''; ?>"><i class="fas fa-clipboard-list"></i><span>PM Templates</span></a><?php endif; ?>
    <?php if ($can('DocExpiries')):       ?><a href="<?= PROOT; ?>DocExpiries"        class="rs-nav-link <?= $is('DocExpiries')       ?'active':''; ?>"><i class="fas fa-file-signature"></i><span>Doc Expiries</span></a><?php endif; ?>
    <?php if ($can('FleetAnalytics')):    ?><a href="<?= PROOT; ?>FleetAnalytics"     class="rs-nav-link <?= $is('FleetAnalytics')    ?'active':''; ?>"><i class="fas fa-chart-line"></i><span>Fleet Analytics</span></a><?php endif; ?>
    <?php if ($can('GpsDashboard')):      ?><a href="<?= PROOT; ?>GpsDashboard"       class="rs-nav-link <?= $is('GpsDashboard')      ?'active':''; ?>"><i class="fas fa-map-marked-alt"></i><span>GPS Dashboard</span></a><?php endif; ?>
    <?php if ($can('WorkOrders')):        ?><a href="<?= PROOT; ?>WorkOrders"         class="rs-nav-link <?= $is('WorkOrders')        ?'active':''; ?>"><i class="fas fa-wrench"></i><span>Work Orders</span></a><?php endif; ?>
    <?php if ($can('Vendors')):           ?><a href="<?= PROOT; ?>Vendors"            class="rs-nav-link <?= $is('Vendors')           ?'active':''; ?>"><i class="fas fa-store"></i><span>Vendors</span></a><?php endif; ?>
</div>
<?php endif; ?>

<!-- ── GPS Tracking ───────────────────────────────────────────────────── -->
<?php if ($can('GpsDevices') || $can('Geofences') || $can('Alerts') || $can('AlertRules')): ?>
<div class="rs-nav-section">GPS Tracking <i class="fas fa-chevron-down section-chevron"></i></div>
<div class="rs-nav-section-items">
    <?php if ($can('GpsDevices')): ?><a href="<?= PROOT; ?>GpsDevices" class="rs-nav-link <?= $is('GpsDevices')?'active':''; ?>"><i class="fas fa-satellite-dish"></i><span>Devices</span></a><?php endif; ?>
    <?php if ($can('Geofences')):  ?><a href="<?= PROOT; ?>Geofences"  class="rs-nav-link <?= $is('Geofences') ?'active':''; ?>"><i class="fas fa-draw-polygon"></i><span>Geofences</span></a><?php endif; ?>
    <?php if ($can('Alerts')):     ?><a href="<?= PROOT; ?>Alerts"     class="rs-nav-link <?= $is('Alerts')    ?'active':''; ?>"><i class="fas fa-exclamation-triangle"></i><span>Alerts</span></a><?php endif; ?>
    <?php if ($can('AlertRules')): ?><a href="<?= PROOT; ?>AlertRules" class="rs-nav-link <?= $is('AlertRules')?'active':''; ?>"><i class="fas fa-bell"></i><span>Alert Rules</span></a><?php endif; ?>
</div>
<?php endif; ?>

<!-- ── Directory ──────────────────────────────────────────────────────── -->
<?php if ($can('Customers')): ?>
<div class="rs-nav-section">Directory <i class="fas fa-chevron-down section-chevron"></i></div>
<div class="rs-nav-section-items">
    <a href="<?= PROOT; ?>Customers" class="rs-nav-link <?= $is('Customers') ? 'active' : ''; ?>"><i class="fas fa-building"></i><span>Customers</span></a>
    <?php if ($can('Suppliers')): ?><a href="<?= PROOT; ?>Suppliers" class="rs-nav-link <?= $is('Suppliers') ? 'active' : ''; ?>"><i class="fas fa-industry"></i><span>Suppliers</span></a><?php endif; ?>
</div>
<?php endif; ?>

<!-- ── Accounting ─────────────────────────────────────────────────────── -->
<?php if ($can('Expenses') || $can('CustomerInvoices') || $can('CustomerReceipts') || $can('AgedReceivables') || $can('ChartOfAccounts') || $can('Journals') || $can('FinancialReports') || $can('FinancialStatements') || $can('Cashbook') || $can('BankRecon') || $can('TaxReturns')): ?>
<div class="rs-nav-section">Accounting <i class="fas fa-chevron-down section-chevron"></i></div>
<div class="rs-nav-section-items">
    <?php if ($can('CustomerInvoices')):    ?><a href="<?= PROOT; ?>CustomerInvoices"    class="rs-nav-link <?= $is('CustomerInvoices')?'active':''; ?>"><i class="fas fa-file-invoice-dollar"></i><span>Customer Invoices</span></a><?php endif; ?>
    <?php if ($can('CustomerReceipts')):    ?><a href="<?= PROOT; ?>CustomerReceipts"    class="rs-nav-link <?= $is('CustomerReceipts')?'active':''; ?>"><i class="fas fa-hand-holding-usd"></i><span>Customer Receipts</span></a><?php endif; ?>
    <?php if ($can('AgedReceivables')):     ?><a href="<?= PROOT; ?>AgedReceivables"     class="rs-nav-link <?= $is('AgedReceivables')?'active':''; ?>"><i class="fas fa-clock"></i><span>Aged Receivables</span></a><?php endif; ?>
    <?php if ($can('CustomerStatements')):  ?><a href="<?= PROOT; ?>CustomerStatements"  class="rs-nav-link <?= $is('CustomerStatements')?'active':''; ?>"><i class="fas fa-file-invoice"></i><span>Customer Statements</span></a><?php endif; ?>
    <?php if ($can('SupplierBills')):       ?><a href="<?= PROOT; ?>SupplierBills"       class="rs-nav-link <?= $is('SupplierBills')?'active':''; ?>"><i class="fas fa-file-invoice"></i><span>Supplier Bills</span></a><?php endif; ?>
    <?php if ($can('SupplierPayments')):    ?><a href="<?= PROOT; ?>SupplierPayments"    class="rs-nav-link <?= $is('SupplierPayments')?'active':''; ?>"><i class="fas fa-money-bill-transfer"></i><span>Supplier Payments</span></a><?php endif; ?>
    <?php if ($can('AgedPayables')):        ?><a href="<?= PROOT; ?>AgedPayables"        class="rs-nav-link <?= $is('AgedPayables')?'active':''; ?>"><i class="fas fa-clock"></i><span>Aged Payables</span></a><?php endif; ?>
    <?php if ($can('SupplierStatements')):  ?><a href="<?= PROOT; ?>SupplierStatements"  class="rs-nav-link <?= $is('SupplierStatements')?'active':''; ?>"><i class="fas fa-file-invoice"></i><span>Supplier Statements</span></a><?php endif; ?>
    <?php if ($can('Payroll')):             ?><a href="<?= PROOT; ?>Payroll"             class="rs-nav-link <?= $is('Payroll')?'active':''; ?>"><i class="fas fa-money-check-alt"></i><span>Payroll</span></a><?php endif; ?>
    <?php if ($can('Expenses')):            ?><a href="<?= PROOT; ?>Expenses"            class="rs-nav-link <?= $is('Expenses')?'active':''; ?>"><i class="fas fa-receipt"></i><span>Expenses</span></a><?php endif; ?>
    <?php if ($can('FinancialReports')):    ?><a href="<?= PROOT; ?>FinancialReports"    class="rs-nav-link <?= $is('FinancialReports')?'active':''; ?>"><i class="fas fa-chart-line"></i><span>Financial Reports</span></a><?php endif; ?>
    <?php if ($can('FinancialStatements')): ?><a href="<?= PROOT; ?>FinancialStatements" class="rs-nav-link <?= $is('FinancialStatements')?'active':''; ?>"><i class="fas fa-book-open"></i><span>Financial Statements</span></a><?php endif; ?>
    <?php if ($can('Journals')):            ?><a href="<?= PROOT; ?>Journals"            class="rs-nav-link <?= $is('Journals')?'active':''; ?>"><i class="fas fa-file-alt"></i><span>Journals</span></a><?php endif; ?>
    <?php if ($can('Cashbook')):            ?><a href="<?= PROOT; ?>Cashbook"            class="rs-nav-link <?= $is('Cashbook')?'active':''; ?>"><i class="fas fa-book"></i><span>Cashbook</span></a><?php endif; ?>
    <?php if ($can('BankRecon')):           ?><a href="<?= PROOT; ?>BankRecon"           class="rs-nav-link <?= $is('BankRecon')?'active':''; ?>"><i class="fas fa-cash-register"></i><span>Bank Reconciliation</span></a><?php endif; ?>
    <?php if ($can('TaxReturns')):          ?><a href="<?= PROOT; ?>TaxReturns"          class="rs-nav-link <?= $is('TaxReturns')?'active':''; ?>"><i class="fas fa-file-invoice"></i><span>Tax Returns</span></a><?php endif; ?>
    <?php if ($can('ChartOfAccounts')):     ?><a href="<?= PROOT; ?>ChartOfAccounts"     class="rs-nav-link <?= $is('ChartOfAccounts')?'active':''; ?>"><i class="fas fa-sitemap"></i><span>Chart of Accounts</span></a><?php endif; ?>
</div>
<?php endif; ?>

<!-- ── Data (bulk import) ─────────────────────────────────────────────── -->
<?php if ($can('Import')): ?>
<div class="rs-nav-section">Data <i class="fas fa-chevron-down section-chevron"></i></div>
<div class="rs-nav-section-items">
    <a href="<?= PROOT; ?>Import" class="rs-nav-link <?= $is('Import') ? 'active' : ''; ?>"><i class="fas fa-file-import"></i><span>Import from Excel</span></a>
</div>
<?php endif; ?>

<!-- ── Admin & Settings ───────────────────────────────────────────────── -->
<?php // Users + UserGroups links removed — controllers do not exist yet;
     // add them back when UsersController + UserGroupsController ship. ?>
<?php if ($can('CompanyProfile') || $can('DocNumbers') || $can('Notifications') || $can('Download')): ?>
<div class="rs-nav-section">Admin <i class="fas fa-chevron-down section-chevron"></i></div>
<div class="rs-nav-section-items">
    <?php if ($can('CompanyProfile')):  ?><a href="<?= PROOT; ?>CompanyProfile"  class="rs-nav-link <?= $is('CompanyProfile')?'active':''; ?>"><i class="fas fa-building"></i><span>Company Profile</span></a><?php endif; ?>
    <?php if ($can('DocNumbers')):      ?><a href="<?= PROOT; ?>DocNumbers"      class="rs-nav-link <?= $is('DocNumbers')?'active':''; ?>"><i class="fas fa-hashtag"></i><span>Document Numbering</span></a><?php endif; ?>
    <?php if ($can('Notifications')):   ?><a href="<?= PROOT; ?>Notifications"   class="rs-nav-link <?= $is('Notifications')?'active':''; ?>"><i class="fas fa-bell"></i><span>Notifications</span></a><?php endif; ?>
    <?php if ($can('Download')):        ?><a href="<?= PROOT; ?>Download"        class="rs-nav-link <?= $is('Download')?'active':''; ?>"><i class="fas fa-mobile-alt"></i><span>App Downloads</span></a><?php endif; ?>
</div>
<?php endif; ?>

<a href="<?= PROOT; ?>Logout" class="rs-nav-link mt-3"><i class="fas fa-sign-out-alt"></i><span>Sign out</span></a>
