<?php $pageTitle='Trailers'; ?>
<div class="panel">
<?php $title='Trailer & Reefer Network';$eyebrow='Carrier Equipment';$searchAction=url('/trailers');$createUrl=url('/trailers/create');$createText='Add Trailer';require app_path('Views/components/search_header.php'); ?>
<div class="table-wrap"><table><thead><tr><th>Trailer</th><th>Carrier / Ownership</th><th>Equipment</th><th>Reefer Status</th><th>Compliance</th><th>Status</th><th class="text-right">Actions</th></tr></thead><tbody>
<?php foreach($trailers as $trailer):
    $tempOut = !empty($trailer['reefer_capable']) && $trailer['current_temperature'] !== null && (($trailer['min_temperature'] !== null && (float)$trailer['current_temperature'] < (float)$trailer['min_temperature']) || ($trailer['max_temperature'] !== null && (float)$trailer['current_temperature'] > (float)$trailer['max_temperature']));
?>
<tr>
<td><strong><?= e($trailer['trailer_number']) ?></strong><br><small><?= e($trailer['plate_number']??'No plate') ?><?= !empty($trailer['truck_number'])?' · Truck '.e($trailer['truck_number']):'' ?></small></td>
<td><strong><?= e($trailer['carrier_name']??'Company / Unlinked') ?></strong><br><small><?= e(readable_status($trailer['ownership_type']??'carrier_owned')) ?></small></td>
<td><?= e($trailer['type']??'Trailer') ?><br><small><?= !empty($trailer['length_ft'])?e((string)$trailer['length_ft']).' ft · ':'' ?><?= !empty($trailer['max_payload_lbs'])?number_format((int)$trailer['max_payload_lbs']).' lb':'' ?></small></td>
<td><?php if(!empty($trailer['reefer_capable'])): ?><span class="reefer-badge">Reefer</span><br><strong class="<?= $tempOut?'text-danger':'' ?>"><?= $trailer['current_temperature']!==null?e((string)$trailer['current_temperature'].$trailer['temperature_unit']):'No reading' ?></strong><small>Set <?= $trailer['current_setpoint']!==null?e((string)$trailer['current_setpoint'].$trailer['temperature_unit']):'-' ?> · <?= e(readable_status($trailer['reefer_mode']??'off')) ?><br>Fuel <?= $trailer['reefer_fuel_level']!==null?e((string)$trailer['reefer_fuel_level']).'%':'-' ?></small><?php else: ?><span class="muted">Standard trailer</span><?php endif; ?></td>
<td><small>Registration <?= e(date_display($trailer['registration_expiry']??null)) ?><br>Inspection <?= e(date_display($trailer['inspection_expiry']??null)) ?><?php if(!empty($trailer['reefer_capable'])): ?><br>Reefer service <?= e(date_display($trailer['reefer_service_due']??null)) ?><?php endif; ?></small></td>
<td><span class="badge badge-<?= status_class($trailer['status']) ?>"><?= e(readable_status($trailer['status'])) ?></span></td>
<td class="text-right table-actions"><a class="btn btn-light btn-sm" href="<?= url('/trailers/edit?id='.$trailer['id']) ?>">Edit</a><form method="post" action="<?= url('/trailers/delete') ?>" onsubmit="return confirm('Delete this trailer?')"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string)$trailer['id']) ?>"><button class="btn btn-danger btn-sm" type="submit">Delete</button></form></td>
</tr><?php endforeach; ?><?php if(!$trailers): ?><tr><td colspan="7" class="empty">No trailers found.</td></tr><?php endif; ?></tbody></table></div>
</div>
