<?php $features=json_decode((string)($trailer['equipment_features']??''),true)?:[]; ?>
<form method="post" action="<?= e($action) ?>" class="form-stack" data-trailer-form>
<?= csrf_field() ?><?php if(!empty($trailer['id'])): ?><input type="hidden" name="id" value="<?= e((string)$trailer['id']) ?>"><?php endif; ?>
<section class="form-section"><div class="form-section-head"><div><p class="eyebrow">Trailer Profile</p><h3>Ownership, Type & Assignment</h3></div><span class="section-chip">Supports carrier-owned equipment</span></div><div class="form-grid three">
<label><span>Trailer Number *</span><input name="trailer_number" value="<?= e(old('trailer_number',$trailer['trailer_number']??'')) ?>" required></label>
<label><span>Carrier Company</span><select name="carrier_id"><option value="">Company / unassigned trailer</option><?php foreach($carriers as $carrier): ?><option value="<?= e((string)$carrier['id']) ?>" <?= (string)old('carrier_id',$trailer['carrier_id']??'')===(string)$carrier['id']?'selected':'' ?>><?= e($carrier['company_name']) ?></option><?php endforeach; ?></select></label>
<label><span>Ownership Type</span><select name="ownership_type"><?php foreach(['carrier_owned'=>'Carrier Owned','company_owned'=>'Company Owned','leased'=>'Leased','rental'=>'Rental','customer_owned'=>'Customer Owned'] as $value=>$label): ?><option value="<?= e($value) ?>" <?= old('ownership_type',$trailer['ownership_type']??'carrier_owned')===$value?'selected':'' ?>><?= e($label) ?></option><?php endforeach; ?></select></label>
<label><span>Trailer Type</span><select name="type" data-trailer-type><?php foreach(['Dry Van','Reefer','Flatbed','Step Deck','Conestoga','Power Only','Box Truck','Hotshot','Tanker','Other'] as $type): ?><option value="<?= e($type) ?>" <?= old('type',$trailer['type']??'Dry Van')===$type?'selected':'' ?>><?= e($type) ?></option><?php endforeach; ?></select></label>
<label><span>Plate Number</span><input name="plate_number" value="<?= e(old('plate_number',$trailer['plate_number']??'')) ?>"></label>
<label><span>Assigned Truck</span><select name="truck_id"><option value="">Unassigned</option><?php foreach($trucks as $truck): ?><option value="<?= e((string)$truck['id']) ?>" <?= (string)old('truck_id',$trailer['truck_id']??'')===(string)$truck['id']?'selected':'' ?>><?= e($truck['unit_number']) ?><?= $truck['carrier_name']?' · '.e($truck['carrier_name']):'' ?></option><?php endforeach; ?></select></label>
<label><span>Status</span><select name="status"><?php foreach(['available','assigned','maintenance','inactive'] as $status): ?><option value="<?= e($status) ?>" <?= old('status',$trailer['status']??'available')===$status?'selected':'' ?>><?= e(readable_status($status)) ?></option><?php endforeach; ?></select></label>
<label><span>Length (ft)</span><input type="number" step="0.1" name="length_ft" value="<?= e(old('length_ft',$trailer['length_ft']??'')) ?>"></label>
<label><span>Max Payload (lbs)</span><input type="number" name="max_payload_lbs" value="<?= e(old('max_payload_lbs',$trailer['max_payload_lbs']??'')) ?>"></label>
<label><span>Door Type</span><select name="door_type"><option value="">Select</option><?php foreach(['Swing Doors','Roll-Up','Side Door','Open Deck','Other'] as $value): ?><option value="<?= e($value) ?>" <?= old('door_type',$trailer['door_type']??'')===$value?'selected':'' ?>><?= e($value) ?></option><?php endforeach; ?></select></label>
</div><div class="option-grid">
<?php foreach(['food_grade'=>'Food Grade','air_ride'=>'Air Ride','liftgate'=>'Liftgate','hazmat_ready'=>'Hazmat Ready'] as $field=>$label): ?><label class="toggle-option"><input type="checkbox" name="<?= e($field) ?>" value="1" <?= old($field,$trailer[$field]??0)?'checked':'' ?>><span><?= e($label) ?></span></label><?php endforeach; ?>
</div></section>

<section class="form-section reefer-section" data-reefer-section>
<div class="form-section-head"><div><p class="eyebrow">Reefer Details</p><h3>Temperature-Controlled Equipment</h3><p class="muted">Track unit identification, operating range, current condition, washouts, fuel, and service.</p></div><label class="toggle-option toggle-primary"><input type="checkbox" name="reefer_capable" value="1" data-reefer-toggle <?= old('reefer_capable',$trailer['reefer_capable']??0)?'checked':'' ?>><span>Reefer capable</span></label></div>
<div class="form-grid three" data-reefer-fields>
<label><span>Reefer Make</span><input name="reefer_make" value="<?= e(old('reefer_make',$trailer['reefer_make']??'')) ?>" placeholder="Carrier, Thermo King"></label>
<label><span>Reefer Model</span><input name="reefer_model" value="<?= e(old('reefer_model',$trailer['reefer_model']??'')) ?>"></label>
<label><span>Reefer Unit Number</span><input name="reefer_unit_number" value="<?= e(old('reefer_unit_number',$trailer['reefer_unit_number']??'')) ?>"></label>
<label><span>Reefer Year</span><input type="number" name="reefer_year" value="<?= e(old('reefer_year',$trailer['reefer_year']??'')) ?>"></label>
<label><span>Temperature Unit</span><select name="temperature_unit"><option value="F" <?= old('temperature_unit',$trailer['temperature_unit']??'F')==='F'?'selected':'' ?>>Fahrenheit (°F)</option><option value="C" <?= old('temperature_unit',$trailer['temperature_unit']??'')==='C'?'selected':'' ?>>Celsius (°C)</option></select></label>
<label><span>Operating Mode</span><select name="reefer_mode"><option value="">Not set</option><option value="continuous" <?= old('reefer_mode',$trailer['reefer_mode']??'')==='continuous'?'selected':'' ?>>Continuous</option><option value="cycle_sentry" <?= old('reefer_mode',$trailer['reefer_mode']??'')==='cycle_sentry'?'selected':'' ?>>Cycle Sentry</option><option value="off" <?= old('reefer_mode',$trailer['reefer_mode']??'')==='off'?'selected':'' ?>>Off</option></select></label>
<label><span>Minimum Temperature</span><input type="number" step="0.1" name="min_temperature" value="<?= e(old('min_temperature',$trailer['min_temperature']??'')) ?>"></label>
<label><span>Maximum Temperature</span><input type="number" step="0.1" name="max_temperature" value="<?= e(old('max_temperature',$trailer['max_temperature']??'')) ?>"></label>
<label><span>Current Setpoint</span><input type="number" step="0.1" name="current_setpoint" value="<?= e(old('current_setpoint',$trailer['current_setpoint']??'')) ?>"></label>
<label><span>Current Temperature</span><input type="number" step="0.1" name="current_temperature" value="<?= e(old('current_temperature',$trailer['current_temperature']??'')) ?>"></label>
<label><span>Reefer Hours</span><input type="number" step="0.1" name="reefer_hours" value="<?= e(old('reefer_hours',$trailer['reefer_hours']??'')) ?>"></label>
<label><span>Fuel Level (%)</span><input type="number" min="0" max="100" step="0.1" name="reefer_fuel_level" value="<?= e(old('reefer_fuel_level',$trailer['reefer_fuel_level']??'')) ?>"></label>
<label><span>Last Temperature Time</span><input type="datetime-local" name="last_temperature_at" value="<?= e(old('last_temperature_at',isset($trailer['last_temperature_at'])&&$trailer['last_temperature_at']?str_replace(' ','T',substr($trailer['last_temperature_at'],0,16)):'')) ?>"></label>
<label><span>Last Washout</span><input type="date" name="last_washout_date" value="<?= e(old('last_washout_date',$trailer['last_washout_date']??'')) ?>"></label>
<label><span>Reefer Service Due</span><input type="date" name="reefer_service_due" value="<?= e(old('reefer_service_due',$trailer['reefer_service_due']??'')) ?>"></label>
<label><span>Telematics / Sensor ID</span><input name="telematics_id" value="<?= e(old('telematics_id',$trailer['telematics_id']??'')) ?>"></label>
</div></section>

<section class="form-section"><div class="form-section-head"><div><p class="eyebrow">Equipment</p><h3>Features & Compliance</h3></div></div>
<div class="option-grid"><?php foreach(['straps'=>'Straps','load_bars'=>'Load Bars','pallet_jack'=>'Pallet Jack','tarps'=>'Tarps','chains'=>'Chains','side_kit'=>'Side Kit','conestoga'=>'Conestoga Kit','vented'=>'Vented'] as $field=>$label): ?><label class="toggle-option"><input type="checkbox" name="feature_<?= e($field) ?>" value="1" <?= in_array($field,$features,true)?'checked':'' ?>><span><?= e($label) ?></span></label><?php endforeach; ?></div>
<div class="form-grid three" style="margin-top:16px"><label><span>Registration Expiry</span><input type="date" name="registration_expiry" value="<?= e(old('registration_expiry',$trailer['registration_expiry']??'')) ?>"></label><label><span>Inspection Expiry</span><input type="date" name="inspection_expiry" value="<?= e(old('inspection_expiry',$trailer['inspection_expiry']??'')) ?>"></label><label class="field-full"><span>Notes</span><textarea name="notes" rows="4"><?= e(old('notes',$trailer['notes']??'')) ?></textarea></label></div>
</section>
<div class="form-actions sticky-form-actions"><button class="btn btn-primary" type="submit">Save Trailer</button><a class="btn btn-light" href="<?= url('/trailers') ?>">Cancel</a></div>
</form>
