<?php $pageTitle='Carrier Portal'; ?>
<div class="page-heading"><div><p class="eyebrow">Carrier Portal</p><h1><?= e($carrier['company_name']??'Carrier account not linked') ?></h1><p>Review assigned loads, equipment, compliance documents, load revenue, and dispatch fees from one mobile-friendly workspace.</p></div></div>
<?php if(!$carrier): ?><div class="alert alert-danger">This login is not linked to a carrier company. Ask an administrator to select a carrier on the user account.</div><?php else: ?>
<div class="stats-grid compact-stats">
 <div class="stat-card"><span>Open Loads</span><strong><?= count(array_filter($loads,fn($l)=>!in_array($l['status'],['paid','cancelled'],true))) ?></strong><small>Current dispatch workload</small></div>
 <div class="stat-card"><span>Power & Trailers</span><strong><?= count($equipment) ?></strong><small>Linked equipment records</small></div>
 <div class="stat-card"><span>Compliance Files</span><strong><?= count($documents) ?></strong><small>Carrier documents on file</small></div>
 <div class="stat-card highlight"><span>Dispatch Fees</span><strong><?= money(array_sum(array_map(fn($l)=>(float)$l['dispatch_fee_amount'],$loads))) ?></strong><small>Recorded across assigned loads</small></div>
</div>
<section class="panel" id="carrier-loads"><div class="panel-header"><div><p class="eyebrow">Assigned Freight</p><h2>My Loads</h2></div></div><div class="driver-load-grid"><?php foreach($loads as $load): ?><a class="driver-load-card" href="<?= url('/carrier/loads/show?id='.$load['id']) ?>"><div class="load-card-top"><strong><?= e($load['load_number']) ?></strong><span class="badge badge-<?= status_class($load['status']) ?>"><?= e(readable_status($load['status'])) ?></span></div><p><?= e($load['pickup_location']) ?> → <?= e($load['delivery_location']) ?></p><small><?= e($load['driver_name']?:'Driver not assigned') ?> · <?= e($load['truck_number']?:'No truck') ?> · <?= e($load['trailer_number']?:'No trailer') ?></small><div class="carrier-money-row"><span>Gross <?= money($load['carrier_rate']) ?></span><strong>Net <?= money($load['carrier_net_amount']) ?></strong></div><?php if(!empty($load['temperature_controlled'])): ?><span class="reefer-badge">Reefer <?= e((string)$load['temperature_setpoint'].$load['temperature_unit']) ?></span><?php endif; ?></a><?php endforeach; ?><?php if(!$loads): ?><p class="empty">No loads assigned yet.</p><?php endif; ?></div></section>
<div class="content-grid two-col"><section class="panel"><div class="panel-header"><div><p class="eyebrow">Equipment</p><h2>My Equipment</h2></div></div><div class="mobile-card-list"><?php foreach($equipment as $item): ?><div class="mobile-record-card"><div><strong><?= e($item['kind'].' '.$item['identifier']) ?></strong><span><?= e($item['equipment_type']?:readable_status($item['ownership_type'])) ?></span></div><span class="badge badge-<?= status_class($item['status']) ?>"><?= e(readable_status($item['status'])) ?></span></div><?php endforeach; ?><?php if(!$equipment): ?><p class="empty">No equipment linked.</p><?php endif; ?></div></section><section class="panel"><div class="panel-header"><div><p class="eyebrow">Compliance</p><h2>Documents</h2></div></div><div class="mini-list"><?php foreach($documents as $doc): ?><a class="mini-card" href="<?= url('/documents/download?id='.$doc['id']) ?>"><strong><?= e($doc['document_type']) ?></strong><span><?= e($doc['file_name']) ?></span></a><?php endforeach; ?><?php if(!$documents): ?><p class="empty">No documents uploaded.</p><?php endif; ?></div></section></div>
<?php endif; ?>
