<?php $pageTitle='Edit Load'; ?>
<div class="page-heading"><div><p class="eyebrow">Dispatch Workspace</p><h1><?= e($load['load_number']) ?></h1><p><?= e($load['carrier_name']?:'Carrier not assigned') ?> · <?= e($load['pickup_location']) ?> → <?= e($load['delivery_location']) ?></p></div><div class="page-actions"><a class="btn btn-light" href="<?= url('/loads') ?>">Back to Loads</a><a class="btn btn-primary" href="<?= url('/driver/loads/show?id='.$load['id']) ?>" target="_blank">Driver View</a></div></div>
<div class="content-grid load-editor-grid">
  <div><?php $action=url('/loads/update');require app_path('Views/loads/form.php'); ?></div>
  <aside class="content-grid load-side-rail">
    <section class="panel">
      <div class="panel-header"><div><p class="eyebrow">Carrier Economics</p><h2>Load Financials</h2></div></div>
      <div class="financial-summary-list">
        <div><span>Customer rate</span><strong><?= money($load['rate']) ?></strong></div>
        <div><span>Carrier gross</span><strong><?= money($load['carrier_rate']) ?></strong></div>
        <div><span>Dispatch fee</span><strong><?= money($load['dispatch_fee_amount']) ?></strong></div>
        <div class="total"><span>Carrier net</span><strong><?= money($load['carrier_net_amount']) ?></strong></div>
      </div>
      <small class="muted"><?= e(readable_status($load['payment_responsibility'])) ?> · Confirmation <?= e(readable_status($load['carrier_confirmation_status'])) ?></small>
    </section>

    <?php if(!empty($load['temperature_controlled'])): ?>
    <section class="panel reefer-monitor-card">
      <div class="panel-header"><div><p class="eyebrow">Cold Chain</p><h2>Reefer Monitor</h2></div><span class="reefer-badge"><?= e((string)$load['temperature_setpoint'].$load['temperature_unit']) ?></span></div>
      <div class="reefer-reading-grid">
        <div><span>Allowed range</span><strong><?= e((string)$load['temperature_min'].'–'.$load['temperature_max'].$load['temperature_unit']) ?></strong></div>
        <div><span>Trailer reading</span><strong><?= $load['trailer_current_temperature']!==null?e((string)$load['trailer_current_temperature'].($load['trailer_temperature_unit']?:$load['temperature_unit'])):'No reading' ?></strong></div>
        <div><span>Mode</span><strong><?= e(readable_status($load['reefer_mode']?:$load['trailer_reefer_mode']?:'not_set')) ?></strong></div>
        <div><span>Fuel</span><strong><?= $load['trailer_reefer_fuel_level']!==null?e((string)$load['trailer_reefer_fuel_level']).'%':'-' ?></strong></div>
      </div>
      <form method="post" action="<?= url('/loads/temperature') ?>" class="form-stack compact-form">
        <?= csrf_field() ?><input type="hidden" name="load_id" value="<?= e((string)$load['id']) ?>">
        <div class="form-grid two"><label><span>Temperature</span><input type="number" step="0.1" name="temperature" required></label><label><span>Unit</span><select name="temperature_unit"><option value="<?= e($load['temperature_unit']) ?>"><?= e($load['temperature_unit']) ?></option><option value="<?= $load['temperature_unit']==='F'?'C':'F' ?>"><?= $load['temperature_unit']==='F'?'C':'F' ?></option></select></label><label><span>Setpoint</span><input type="number" step="0.1" name="setpoint" value="<?= e((string)$load['temperature_setpoint']) ?>"></label><label><span>Fuel %</span><input type="number" min="0" max="100" step="0.1" name="reefer_fuel_level"></label></div>
        <label><span>Notes</span><input name="notes" placeholder="Dock check, pre-trip, delivery reading"></label><button class="btn btn-primary" type="submit">Record Reading</button>
      </form>
      <div class="temperature-log-list"><?php foreach(array_slice($temperatureLogs,0,6) as $reading): ?><div class="temperature-log-item <?= (($load['temperature_min']!==null&&(float)$reading['temperature']<(float)$load['temperature_min'])||($load['temperature_max']!==null&&(float)$reading['temperature']>(float)$load['temperature_max']))?'is-alert':'' ?>"><div><strong><?= e((string)$reading['temperature'].$reading['temperature_unit']) ?></strong><span><?= e(datetime_display($reading['recorded_at'])) ?></span></div><small><?= e($reading['driver_name']?:'Team') ?><?= $reading['stop_location']?' · '.e($reading['stop_location']):'' ?></small></div><?php endforeach; ?><?php if(!$temperatureLogs): ?><p class="empty">No temperature readings yet.</p><?php endif; ?></div>
    </section>
    <?php endif; ?>

    <section class="panel">
      <div class="panel-header"><div><p class="eyebrow">Live Location</p><h2>Driver Location</h2></div></div>
      <?php if(!empty($load['current_latitude'])&&!empty($load['current_longitude'])): ?><div class="location-card"><strong><?= e($load['last_location_address']?:'Latest shared location') ?></strong><span><?= e($load['current_latitude'].', '.$load['current_longitude']) ?></span><small>Updated <?= e(datetime_display($load['last_location_at'])) ?></small><a class="btn btn-primary btn-block" target="_blank" rel="noopener" href="https://www.google.com/maps?q=<?= e((string)$load['current_latitude']) ?>,<?= e((string)$load['current_longitude']) ?>">Open in Maps</a></div><?php else: ?><p class="empty">No location shared by driver yet.</p><?php endif; ?>
    </section>

    <section class="panel"><div class="panel-header"><div><p class="eyebrow">Load Files</p><h2>Photos, Challans & Docs</h2></div></div><div class="mini-list"><?php foreach($documents as $document): ?><a class="mini-card" href="<?= url('/documents/download?id='.$document['id']) ?>"><strong><?= e($document['document_type']) ?></strong><span><?= e($document['file_name']) ?></span><?php if(!empty($document['stop_location'])): ?><span>Stop <?= e((string)$document['stop_sequence']) ?> · <?= e($document['stop_location']) ?></span><?php endif; ?></a><?php endforeach; ?><?php if(!$documents): ?><p class="empty">No load files uploaded yet.</p><?php endif; ?></div></section>

    <section class="panel"><div class="panel-header"><div><p class="eyebrow">Timeline</p><h2>Activity</h2></div></div><div class="timeline compact-timeline"><?php foreach($events??[] as $event): ?><div><span></span><strong><?= e(readable_status($event['event_type'])) ?></strong><p><?= e($event['description']??'') ?> · <?= e(datetime_display($event['created_at'])) ?></p></div><?php endforeach; ?><?php if(empty($events)): ?><p class="empty">No activity yet.</p><?php endif; ?></div></section>
  </aside>
</div>
