<?php
$user=Auth::user();$role=$user['role']??'';$admin=in_array($role,['owner','admin'],true);$path=current_path();
function nav_icon(string $name): string{
    $i=[
        'home'=>'<path d="M3 11.5 12 4l9 7.5"/><path d="M5.5 10.5V20h13v-9.5"/>',
        'dispatch'=>'<path d="M4 17h16M6 17l2-9h8l2 9M9 12h6"/>',
        'loads'=>'<rect x="4" y="5" width="16" height="14" rx="2"/><path d="M8 9h8M8 13h5"/>',
        'map'=>'<path d="m3 6 6-3 6 3 6-3v15l-6 3-6-3-6 3Z"/><path d="M9 3v15M15 6v15"/>',
        'people'=>'<circle cx="9" cy="8" r="3"/><path d="M3 20a6 6 0 0 1 12 0M16 5a3 3 0 0 1 0 6M18 14a5 5 0 0 1 3 5"/>',
        'truck'=>'<path d="M3 6h11v11H3zM14 10h4l3 3v4h-7z"/><circle cx="7" cy="18" r="2"/><circle cx="18" cy="18" r="2"/>',
        'trailer'=>'<rect x="3" y="6" width="15" height="10" rx="1"/><path d="M18 12h3M6 16v2M15 16v2"/><circle cx="6" cy="19" r="1.5"/><circle cx="15" cy="19" r="1.5"/>',
        'fuel'=>'<path d="M6 3h8v18H6zM8 7h4"/><path d="M14 8h2l2 2v7a2 2 0 0 0 4 0v-6l-2-2"/>',
        'book'=>'<path d="M5 3h11a3 3 0 0 1 3 3v15H8a3 3 0 0 1-3-3Z"/><path d="M8 3v18M11 8h5M11 12h5"/>',
        'border'=>'<path d="M5 3v18M19 3v18M5 7h14M5 17h14"/><path d="m9 12 2 2 4-4"/>',
        'doc'=>'<path d="M6 2h8l4 4v16H6z"/><path d="M14 2v5h5M9 12h6M9 16h6"/>',
        'money'=>'<path d="M4 19h16M6 16V9M10 16V5M14 16v-4M18 16V7"/>',
        'invoice'=>'<path d="M7 2h10v20l-2-1.5L13 22l-2-1.5L9 22l-2-1.5Z"/><path d="M10 7h4M10 11h4M10 15h4"/>',
        'wrench'=>'<path d="M14.7 6.3a4 4 0 0 0-5-5L12 3.6 9.6 6 7.3 3.7a4 4 0 0 0 5 5L4 17l3 3 8.3-8.3a4 4 0 0 0 5-5L18 9l-2.4-2.4Z"/>',
        'settings'=>'<circle cx="12" cy="12" r="3"/><path d="M19 12a7 7 0 0 0-.1-1l2-1.5-2-3.4-2.4 1A7 7 0 0 0 15 6l-.3-2.6h-4L10.5 6A7 7 0 0 0 9 7.1l-2.4-1-2 3.4 2 1.5a7 7 0 0 0 0 2l-2 1.5 2 3.4 2.4-1a7 7 0 0 0 1.5 1.1l.2 2.6h4L15 18a7 7 0 0 0 1.5-1.1l2.4 1 2-3.4-2-1.5a7 7 0 0 0 .1-1Z"/>',
        'spark'=>'<path d="m12 2 1.7 5.3L19 9l-5.3 1.7L12 16l-1.7-5.3L5 9l5.3-1.7Z"/><path d="m19 15 .8 2.2L22 18l-2.2.8L19 21l-.8-2.2L16 18l2.2-.8Z"/>',
        'bell'=>'<path d="M18 8a6 6 0 0 0-12 0c0 7-3 7-3 9h18c0-2-3-2-3-9M10 21h4"/>',
        'task'=>'<path d="M9 11l2 2 4-5"/><rect x="4" y="3" width="16" height="18" rx="2"/>',
        'report'=>'<path d="M5 20V10M12 20V4M19 20v-7"/>',
    ];return '<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">'.($i[$name]??$i['loads']).'</svg>';
}
function nav_link(string $href,string $label,string $icon,?string $badge=null):void{
    $current=current_path();$active=$current===$href || ($href!=='/dashboard' && $href!=='/loads' && $href!=='/ai' && str_starts_with($current,$href.'/'));
    echo '<a class="nav-item '.($active?'active':'').'" href="'.url($href).'" data-nav-label="'.e(strtolower($label)).'"><span class="nav-icon nav-icon-'.e($icon).'">'.nav_icon($icon).'</span><span class="nav-label">'.$label.'</span>';
    if($badge!==null&&$badge!=='')echo '<b class="nav-count">'.$badge.'</b>';echo '</a>';
}
function nav_group(string $label,string $icon,array $prefixes,callable $links):void{
    $open=false;foreach($prefixes as $p)if(str_starts_with(current_path(),$p)){$open=true;break;}
    echo '<details class="nav-section" '.($open?'open':'').'><summary><span class="nav-icon nav-icon-'.e($icon).'">'.nav_icon($icon).'</span><span class="nav-label">'.$label.'</span><i></i></summary><div>';$links();echo '</div></details>';
}
$pendingAI=ai_pending_action_count();$alerts=unread_notification_count();
?>
<aside class="sidebar sidebar-v3 sidebar-v4" id="sidebar" aria-label="Primary navigation">
    <div class="sidebar-brand-row">
        <a class="brand" href="<?= url($role==='driver'?'/driver/loads':($role==='carrier'?'/carrier':'/dashboard')) ?>"><span class="brand-mark-image"><img src="<?= asset('images/fleetlume-mark.png') ?>" alt="" width="42" height="42"></span><span class="brand-copy"><strong>FleetLume</strong><small>Control center</small></span></a>
        <button class="icon-button sidebar-collapse" type="button" data-sidebar-collapse aria-label="Collapse sidebar"><?= nav_icon('dispatch') ?></button>
        <button class="icon-button sidebar-close" type="button" data-sidebar-close aria-label="Close navigation">×</button>
    </div>
    <div class="nav-search"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg><input type="search" placeholder="Filter menu" aria-label="Filter navigation" data-nav-filter><kbd>⌘K</kbd></div>
    <nav class="sidebar-nav compact-nav" data-nav-container>
    <?php if($role==='driver'): ?>
        <?php nav_link('/driver/loads','My Loads','loads');nav_link('/hos/driver','Logbook & HOS','book');nav_link('/fuel','Fuel Allowance','fuel');nav_link('/borders','Border Crossings','border');nav_link('/driver/expenses','Expenses','money');nav_link('/driver/finances','Settlements','invoice');nav_link('/documents','Documents','doc'); ?>
    <?php elseif($role==='carrier'): ?>
        <?php nav_link('/carrier','Overview','home');nav_link('/hos','Driver Logbooks','book');nav_link('/documents','Documents','doc'); ?>
    <?php elseif($role==='accountant'): ?>
        <?php nav_link('/accounting','Finance Home','home'); ?>
        <?php nav_group('Receivables','invoice',['/invoices'],function(){nav_link('/invoices','Invoices','invoice');nav_link('/accounting/factoring','Factoring','money');}); ?>
        <?php nav_group('Payables & cash','money',['/accounting','/settlements','/fuel'],function(){nav_link('/fuel','Fuel Control','fuel');nav_link('/accounting/expenses','Expenses','money');nav_link('/accounting/vendors','Vendors','people');nav_link('/accounting/bills','Vendor Bills','doc');nav_link('/accounting/banking','Banking','money');nav_link('/settlements','Settlements','invoice');}); ?>
        <?php nav_group('Books & reports','report',['/accounting/journals','/accounting/reports'],function(){nav_link('/accounting/journals','General Ledger','book');nav_link('/accounting/reports','Financial Reports','report');}); ?>
    <?php elseif($role==='maintenance'): ?>
        <?php nav_link('/maintenance','Maintenance','wrench');nav_link('/trucks','Power Units','truck');nav_link('/trailers','Trailers & Reefers','trailer');nav_link('/documents','Documents','doc');nav_link('/tasks','Team Tasks','task'); ?>
    <?php elseif($role==='team'): ?>
        <?php nav_link('/tasks','My Tasks','task');nav_link('/loads/calendar','Schedule','dispatch');nav_link('/loads','Loads','loads');if(can('fuel.view'))nav_link('/fuel','Fuel Control','fuel');if(can('hos.view'))nav_link('/hos','Driver Logbooks','book');if(can('border.manage'))nav_link('/borders','Border Crossings','border');nav_link('/documents','Documents','doc'); ?>
    <?php else: ?>
        <?php nav_link('/dashboard','Dashboard','home'); ?>
        <?php nav_group('Dispatch','dispatch',['/loads','/tracking'],function(){nav_link('/loads/board','Dispatch Board','dispatch');nav_link('/loads','All Loads','loads');nav_link('/loads/calendar','Calendar','dispatch');nav_link('/tracking','Live Map','map');}); ?>
        <?php nav_group('Carrier network','people',['/carriers','/drivers','/trucks','/trailers','/brokers'],function(){nav_link('/carriers','Carriers','people');nav_link('/drivers','Drivers','people');nav_link('/trucks','Power Units','truck');nav_link('/trailers','Trailers & Reefers','trailer');nav_link('/brokers','Brokers / Customers','people');}); ?>
        <?php nav_group('Operations','task',['/fuel','/hos','/borders','/documents','/tasks','/maintenance'],function()use($admin){nav_link('/fuel','Fuel Allocation','fuel');nav_link('/hos','Driver HOS','book');nav_link('/borders','Border Crossings','border');nav_link('/documents','Document Center','doc');nav_link('/tasks','Team Tasks','task');if($admin)nav_link('/maintenance','Maintenance','wrench');}); ?>
        <?php nav_group('Finance','money',['/accounting','/invoices','/settlements','/reports'],function()use($admin){if($admin)nav_link('/accounting','Accounting Center','money');nav_link('/invoices','Invoices','invoice');if($admin)nav_link('/settlements','Settlements','invoice');nav_link('/reports','Operations Reports','report');}); ?>
        <?php if($admin)nav_group('Administration','settings',['/users','/settings'],function(){nav_link('/users','Users & Portals','people');nav_link('/settings','Company Settings','settings');}); ?>
    <?php endif; ?>
        <?php if(can('ai.use')): ?>
            <?php nav_group('FleetLume AI','spark',['/ai'],function()use($pendingAI){nav_link('/ai','AI Workspace','spark');nav_link('/ai/automation','Autopilot','spark',$pendingAI?(string)$pendingAI:null);}); ?>
        <?php endif; ?>
        <?php nav_link('/notifications','Notifications','bell',$alerts?(string)$alerts:null); ?>
    </nav>
    <a class="sidebar-help" href="<?= url('/notifications') ?>" aria-label="Open FleetLume help and alerts"><?= nav_icon('bell') ?><span><strong>Need help?</strong><small>Alerts, tasks & support</small></span><b>›</b></a>
    <div class="sidebar-footer"><span class="sidebar-user-avatar"><?= e(user_initials($user['name']??'User')) ?></span><span class="sidebar-user-copy"><strong><?= e($user['name']??'User') ?></strong><small><?= e(readable_status($role)) ?></small></span><a class="icon-button" href="<?= url($admin?'/settings':'/notifications') ?>" aria-label="Settings"><?= nav_icon('settings') ?></a></div>
</aside>
