<?php $this->start('body'); ?>
<?php $events = $this->data['events']; $filters = $this->data['filters']; $trucks = $this->data['trucks']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-bell me-2 text-muted"></i>GPS Alerts</h5>
    <div class="text-muted small">Recent events from the ingestion stream.</div>
  </div>
  <a href="<?= PROOT; ?>GpsDashboard" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Live map</a>
</div>

<form method="GET" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2 d-flex flex-wrap gap-2 align-items-end">
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Truck</label>
      <select name="truck_id" class="form-select form-select-sm">
        <option value="0">All</option>
        <?php foreach ($trucks as $t): ?>
          <option value="<?= (int)$t['c0d']; ?>" <?= (int)$filters['truck_id'] === (int)$t['c0d'] ? 'selected' : ''; ?>><?= htmlspecialchars($t['truck_reg']); ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Kind</label>
      <input type="text" name="kind" value="<?= htmlspecialchars($filters['kind']); ?>" class="form-control form-control-sm" placeholder="speed_over · geofence_entry · &hellip;">
    </div>
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Severity</label>
      <select name="severity" class="form-select form-select-sm">
        <?php foreach (['' => 'All', 'info' => 'Info', 'warn' => 'Warn', 'crit' => 'Critical'] as $k => $v): ?>
          <option value="<?= $k; ?>" <?= $filters['severity'] === $k ? 'selected' : ''; ?>><?= $v; ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">From</label><input type="datetime-local" name="from" value="<?= htmlspecialchars($filters['from']); ?>" class="form-control form-control-sm"></div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">To</label>  <input type="datetime-local" name="to"   value="<?= htmlspecialchars($filters['to']);   ?>" class="form-control form-control-sm"></div>
    <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-search"></i></button>
  </div>
</form>

<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr><th style="width:150px;">When</th><th>Kind</th><th>Truck</th><th>Device</th><th class="text-center">Sev</th><th class="money">Speed</th><th>Location</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php if (empty($events)): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">No events match.</td></tr>
        <?php else: ?>
          <?php foreach ($events as $e):
            $sevColor = ['info'=>'primary','warn'=>'warning text-dark','crit'=>'danger','critical'=>'danger'][$e['severity']] ?? 'secondary';
          ?>
            <tr>
              <td class="small font-monospace"><?= humanDateTime($e['ts']); ?></td>
              <td class="small fw-semibold"><?= htmlspecialchars(str_replace('_', ' ', $e['kind'])); ?></td>
              <td class="small"><?= $e['truck_reg'] ? '<a href="' . PROOT . 'GpsDashboard/truck/' . (int)$e['truck_id'] . '" class="text-decoration-none">' . htmlspecialchars($e['truck_reg']) . '</a>' : '—'; ?></td>
              <td class="small text-muted font-monospace"><?= htmlspecialchars($e['device_code'] ?? '—'); ?></td>
              <td class="text-center"><span class="badge bg-<?= $sevColor; ?>"><?= htmlspecialchars($e['severity']); ?></span></td>
              <td class="money font-monospace small"><?= $e['speed_kph'] !== null ? number_format((float)$e['speed_kph'], 0) : '—'; ?></td>
              <td class="small">
                <?php if ($e['lat'] && $e['lng']): ?>
                  <span class="font-monospace text-muted"><?= number_format((float)$e['lat'], 4); ?>, <?= number_format((float)$e['lng'], 4); ?></span>
                <?php else: ?>—<?php endif; ?>
              </td>
              <td class="text-end small">
                <?php if ($e['trip_id']): ?>
                  <a href="<?= PROOT; ?>GpsDashboard/trip/<?= (int)$e['trip_id']; ?>" class="btn btn-sm btn-outline-secondary" title="Trip playback"><i class="fas fa-route"></i></a>
                <?php endif; ?>
              </td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php $this->end(); ?>
