<?php $pageTitle = 'Global Search'; ?>
<div class="page-heading"><div><p class="eyebrow">Find Anything</p><h2>Search Results</h2><p><?= $query ? 'Results for “'.e($query).'”' : 'Search loads, carriers, invoices, drivers, logbooks, equipment, borders, and documents.' ?></p></div></div>
<?php if ($query === ''): ?><section class="card empty-state">Enter a search term in the top bar.</section><?php endif; ?>
<?php
$sections = [
'loads'=>['Loads',fn($r)=>$r['load_number'].' · '.$r['pickup_location'].' → '.$r['delivery_location'],fn($r)=>current_role()==='carrier'?url('/carrier/loads/show?id='.$r['id']):url('/loads/edit?id='.$r['id'])],
'carriers'=>['Carrier Companies',fn($r)=>$r['company_name'].' · MC '.($r['mc_number']?:'-').' · '.readable_status($r['onboarding_status']),fn($r)=>url('/carriers/show?id='.$r['id'])],
'invoices'=>['Invoices',fn($r)=>$r['invoice_number'].' · '.($r['company_name']?:'Customer').' · '.money($r['total']),fn($r)=>url('/invoices/view?id='.$r['id'])],
'drivers'=>['Drivers',fn($r)=>$r['name'].' · '.($r['phone']?:$r['email']),fn($r)=>url('/drivers/edit?id='.$r['id'])],
'hos'=>['Driver Logbooks',fn($r)=>$r['driver_name'].' · '.$r['log_date'].' · '.readable_status($r['status']),fn($r)=>url('/hos/driver?driver_id='.$r['driver_id'].'&date='.$r['log_date'])],
'fuel'=>['Fuel Allocations',fn($r)=>$r['allocation_number'].' · '.($r['load_number']?'Load '.$r['load_number']:'General').' · '.money($r['remaining_amount']).' remaining',fn($r)=>url('/fuel')],
'equipment'=>['Equipment',fn($r)=>$r['kind'].' · '.$r['identifier'].' · '.readable_status($r['status']),fn($r)=>url($r['kind']==='Truck'?'/trucks/edit?id='.$r['id']:'/trailers/edit?id='.$r['id'])],
'borders'=>['Border Crossings',fn($r)=>$r['crossing_number'].' · '.$r['crossing_name'].' · '.readable_status($r['status']),fn($r)=>url('/borders/show?id='.$r['id'])],
'documents'=>['Documents',fn($r)=>$r['document_type'].' · '.$r['file_name'],fn($r)=>url('/documents/download?id='.$r['id'])],
];
?>
<div class="content-grid two-col search-results"><?php foreach ($sections as $key=>$meta): if (empty($results[$key])) continue; ?><section class="card"><div class="card-header"><h3><?= e($meta[0]) ?></h3><span class="badge badge-muted"><?= count($results[$key]) ?></span></div><div class="result-list"><?php foreach ($results[$key] as $row): ?><a href="<?= e($meta[2]($row)) ?>"><?= e($meta[1]($row)) ?><span>Open</span></a><?php endforeach; ?></div></section><?php endforeach; ?></div>
<?php if ($query !== '' && !array_filter($results)): ?><section class="card empty-state">No matching records found.</section><?php endif; ?>
