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

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-satellite-dish me-2 text-muted"></i>GPS Devices
      <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <a href="<?= PROOT; ?>GpsDevices/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> Register Device</a>
</div>

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>GpsDevices/index/1" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2">
    <div class="row g-2 align-items-end">
      <div class="col-md-4"><label class="form-label small text-muted mb-1">Search</label>
        <div class="input-group input-group-sm"><span class="input-group-text bg-white"><i class="fas fa-search text-muted"></i></span>
          <input type="text" class="form-control" name="q" value="<?= htmlspecialchars($f['q']); ?>" placeholder="IMEI · SIM · truck reg" autocomplete="off"></div></div>
      <div class="col-md-2"><label class="form-label small text-muted mb-1">Status</label>
        <select name="status" class="form-select form-select-sm"><option value="">All</option>
          <?php foreach (['active','inactive','offline','retired'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['status']===$s?'selected':''; ?>><?= ucfirst($s); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label small text-muted mb-1">Type</label>
        <select name="device_type" class="form-select form-select-sm"><option value="">All</option>
          <?php foreach (['teltonika_fmc920'=>'Teltonika FMC920','teltonika_fmb920'=>'Teltonika FMB920','traccar_generic'=>'Traccar generic','streamax_x1n'=>'Streamax X1N','unknown'=>'Unknown','other'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= $f['device_type']===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><label class="form-label small text-muted mb-1">&nbsp;</label>
        <div class="form-check small">
          <input type="checkbox" name="unassigned" value="1" class="form-check-input" id="chkUn" <?= !empty($f['unassigned'])?'checked':''; ?>>
          <label class="form-check-label" for="chkUn">Unassigned only</label>
        </div></div>
      <div class="col-md-1 d-flex gap-1">
        <button class="btn btn-sm btn-primary flex-grow-1"><i class="fas fa-filter"></i></button>
        <a href="<?= PROOT; ?>GpsDevices" class="btn btn-sm btn-outline-secondary"><i class="fas fa-times"></i></a>
      </div>
    </div>
  </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>Device</th><th>Truck</th><th>SIM</th><th>Type</th><th>Status</th>
          <th>Last seen</th><th>Last position</th><th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php
            $bg   = ['active'=>'success','inactive'=>'secondary','offline'=>'danger','retired'=>'dark'][$r['status']] ?? 'secondary';
            $stale = $r['last_seen_at'] && strtotime($r['last_seen_at']) < strtotime('-15 minutes');
          ?>
          <tr>
            <td class="small">
              <a href="<?= PROOT; ?>GpsDevices/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold font-monospace"><?= htmlspecialchars($r['device_code']); ?></a>
              <?php if ($r['fuel_sensor']): ?><i class="fas fa-gas-pump text-muted ms-1" title="Fuel sensor"></i><?php endif; ?>
              <?php if ($r['dashcam']):     ?><i class="fas fa-video     text-muted ms-1" title="Dashcam"></i><?php endif; ?>
            </td>
            <td class="small">
              <?php if ($r['truck_id']): ?>
                <a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['truck_id']; ?>" class="fw-semibold"><?= htmlspecialchars($r['truck_reg']); ?></a>
              <?php else: ?>
                <span class="text-muted fst-italic">— unassigned —</span>
              <?php endif; ?>
            </td>
            <td class="small text-muted"><?= htmlspecialchars($r['sim_msisdn'] ?: '—'); ?></td>
            <td class="small text-muted"><?= htmlspecialchars($r['device_type']); ?><div class="text-muted"><?= htmlspecialchars($r['protocol']); ?></div></td>
            <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span></td>
            <td class="small"><?= $r['last_seen_at'] ? humanDateTime($r['last_seen_at']) : '<span class="text-muted">never</span>'; ?><?php if ($stale): ?><div class="text-danger small"><i class="fas fa-exclamation-triangle"></i> stale</div><?php endif; ?></td>
            <td class="small">
              <?php if ($r['last_lat'] !== null): ?>
                <span class="font-monospace"><?= number_format((float)$r['last_lat'], 4); ?>, <?= number_format((float)$r['last_lng'], 4); ?></span>
                <?php if ($r['last_speed'] !== null): ?><div class="text-muted"><?= number_format((float)$r['last_speed'], 0); ?> kph</div><?php endif; ?>
              <?php else: ?><span class="text-muted">—</span><?php endif; ?>
            </td>
            <td class="text-end"><a href="<?= PROOT; ?>GpsDevices/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">No devices — <a href="<?= PROOT; ?>GpsDevices/add">register the first one</a>. Devices also auto-register on first ingestion (as inactive).</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if (!empty($this->data['pg_html'])): ?>
    <div class="card-footer bg-white d-flex justify-content-between py-2">
      <span class="small text-muted">Page <?= (int)$this->data['page']; ?> · <?= (int)$this->data['total']; ?> total</span>
      <?= $this->data['pg_html']; ?>
    </div>
  <?php endif; ?>
</div>

<?php $this->end(); ?>
