<?php $pageTitle='Loads'; ?>
<div class="panel">
<?php $title='Carrier Dispatch Loads';$eyebrow='Dispatch Operations';$searchAction=url('/loads');$createUrl=url('/loads/create');$createText='Add Load';require app_path('Views/components/search_header.php'); ?>
<div class="quick-links"><a href="<?= url('/loads/board') ?>">Open visual dispatch board</a><a href="<?= url('/loads/calendar') ?>">Open dispatch calendar</a><a href="<?= url('/carriers') ?>">Manage carriers</a></div>
<div class="table-wrap"><table><thead><tr><th>Load / Route</th><th>Carrier</th><th>Driver / Equipment</th><th>Schedule</th><th>Status</th><th>Tracking / Reefer</th><th>Financials</th><th class="text-right">Actions</th></tr></thead><tbody>
<?php foreach($loads as $load): ?>
<tr>
<td><strong><?= e($load['load_number']) ?></strong><br><small><?= e($load['reference_number']??'') ?><?= !empty($load['miles'])?' · '.e((string)$load['miles']).' miles':'' ?></small><div class="table-route"><?= e($load['pickup_location']) ?><span>→</span><?= e($load['delivery_location']) ?></div><small><?= e($load['broker_name']??'No broker') ?></small></td>
<td><strong><?= e($load['carrier_name']??'Unassigned') ?></strong><br><small><?= e(readable_status($load['equipment_source']??'external_carrier')) ?><br><?= e(readable_status($load['carrier_confirmation_status']??'not_sent')) ?></small></td>
<td><?= e($load['driver_name']??'Unassigned') ?><br><small>Truck <?= e($load['truck_number']??'-') ?> · Trailer <?= e($load['trailer_number']??'-') ?></small></td>
<td><small>Pickup <?= e(datetime_display($load['pickup_datetime']??null)) ?><br>Delivery <?= e(datetime_display($load['delivery_datetime']??null)) ?></small></td>
<td><span class="badge badge-<?= status_class($load['status']) ?>"><?= e(readable_status($load['status'])) ?></span></td>
<td>
<?php if(!empty($load['temperature_controlled'])): ?><span class="reefer-badge">Reefer <?= e((string)$load['temperature_setpoint'].($load['temperature_unit']??'F')) ?></span><br><small>Range <?= e((string)$load['temperature_min'].'–'.$load['temperature_max'].($load['temperature_unit']??'F')) ?><?php if($load['trailer_current_temperature']!==null): ?><br>Current <?= e((string)$load['trailer_current_temperature'].($load['trailer_temperature_unit']??$load['temperature_unit'])) ?><?php endif; ?></small><?php endif; ?>
<?php if(!empty($load['current_latitude'])&&!empty($load['current_longitude'])): ?><br><a class="edit-link" target="_blank" rel="noopener" href="https://www.google.com/maps?q=<?= e((string)$load['current_latitude']) ?>,<?= e((string)$load['current_longitude']) ?>">View location</a><br><small><?= e($load['last_location_address']?:datetime_display($load['last_location_at']??null)) ?></small><?php elseif(empty($load['temperature_controlled'])): ?><small>No live data</small><?php endif; ?>
</td>
<td><div class="financial-cell"><span>Customer <strong><?= money($load['rate']) ?></strong></span><span>Carrier <strong><?= money($load['carrier_rate']) ?></strong></span><span>Fee <strong><?= money($load['dispatch_fee_amount']) ?></strong></span><span>Net <strong><?= money($load['carrier_net_amount']) ?></strong></span></div></td>
<td class="text-right table-actions"><a class="btn btn-light btn-sm" href="<?= url('/loads/edit?id='.$load['id']) ?>">Open</a><a class="btn btn-light btn-sm" href="<?= url('/documents/create?related_type=load&related_id='.$load['id']) ?>">Doc</a><?php if(in_array($load['status'],['delivered','invoiced'],true)): ?><a class="btn btn-light btn-sm" href="<?= url('/invoices/create?load_id='.$load['id']) ?>">Invoice</a><?php endif; ?><form method="post" action="<?= url('/loads/delete') ?>" onsubmit="return confirm('Delete this load?')"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string)$load['id']) ?>"><button class="btn btn-danger btn-sm" type="submit">Delete</button></form></td>
</tr>
<?php endforeach; ?><?php if(!$loads): ?><tr><td colspan="8" class="empty">No loads found.</td></tr><?php endif; ?></tbody></table></div>
</div>
