<?php $this->start('body'); ?>
<?php $g = $this->data['g']; ?>
<?php
  $bg    = ['active'=>'success','inactive'=>'secondary','offline'=>'danger','retired'=>'dark'][$g['status']] ?? 'secondary';
  $stale = $g['last_seen_at'] && strtotime($g['last_seen_at']) < strtotime('-15 minutes');
?>

<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-satellite-dish me-2 text-muted"></i><span class="font-monospace"><?= htmlspecialchars($g['device_code']); ?></span>
      <span class="badge bg-<?= $bg; ?> ms-2" style="font-size:.65em;vertical-align:middle;text-transform:capitalize;"><?= htmlspecialchars($g['status']); ?></span>
    </h5>
    <div class="text-muted small">
      <?= htmlspecialchars($g['device_type']); ?> · <?= htmlspecialchars($g['protocol']); ?>
      <?php if ($g['truck_id']): ?> · fitted to <a href="<?= PROOT; ?>Trucks/view/<?= (int)$g['truck_id']; ?>" class="fw-semibold"><?= htmlspecialchars($g['truck_reg']); ?></a><?php endif; ?>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>GpsDevices" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
    <a href="<?= PROOT; ?>GpsDevices/edit/<?= (int)$g['c0d']; ?>" class="btn btn-sm btn-primary"><i class="fas fa-pen me-1"></i>Edit</a>
  </div>
</div>

<?= flashHtml(); ?>

<?php if ($g['status'] === 'inactive' && $g['device_type'] === 'unknown'): ?>
  <div class="alert alert-warning py-2 small"><i class="fas fa-exclamation-triangle me-1"></i>
    This device was auto-registered from ingestion. Set device type, assign to a truck, then flip status to <strong>Active</strong> so its positions are trusted.
  </div>
<?php endif; ?>

<div class="row g-3">
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3">Device</h6>
        <dl class="row mb-0 small">
          <dt class="col-5 text-muted fw-normal">Device code</dt><dd class="col-7 font-monospace"><?= htmlspecialchars($g['device_code']); ?></dd>
          <dt class="col-5 text-muted fw-normal">Type</dt><dd class="col-7"><?= htmlspecialchars($g['device_type']); ?></dd>
          <dt class="col-5 text-muted fw-normal">Protocol</dt><dd class="col-7"><?= htmlspecialchars($g['protocol']); ?></dd>
          <dt class="col-5 text-muted fw-normal">SIM MSISDN</dt><dd class="col-7"><?= htmlspecialchars($g['sim_msisdn'] ?: '—'); ?></dd>
          <dt class="col-5 text-muted fw-normal">SIM ICCID</dt><dd class="col-7"><?= htmlspecialchars($g['sim_iccid'] ?: '—'); ?></dd>
          <dt class="col-5 text-muted fw-normal">Installed</dt><dd class="col-7"><?= $g['installed_on'] ? humanDate($g['installed_on']) : '—'; ?></dd>
          <dt class="col-5 text-muted fw-normal">Firmware</dt><dd class="col-7"><?= htmlspecialchars($g['firmware_ver'] ?: '—'); ?></dd>
          <dt class="col-5 text-muted fw-normal">Fuel sensor</dt><dd class="col-7"><?= $g['fuel_sensor'] ? '<span class="badge bg-success">Yes</span>' : '<span class="text-muted">No</span>'; ?></dd>
          <dt class="col-5 text-muted fw-normal">Dashcam</dt><dd class="col-7"><?= $g['dashcam'] ? '<span class="badge bg-success">Yes</span>' : '<span class="text-muted">No</span>'; ?></dd>
          <?php if ($g['truck_id']): ?>
            <dt class="col-5 text-muted fw-normal">Truck</dt>
            <dd class="col-7"><a href="<?= PROOT; ?>Trucks/view/<?= (int)$g['truck_id']; ?>"><?= htmlspecialchars($g['truck_reg']); ?></a><?php if ($g['make']): ?> · <?= htmlspecialchars(trim($g['make'] . ' ' . $g['model'])); ?><?php endif; ?></dd>
          <?php endif; ?>
          <?php if ($g['notes']): ?>
            <dt class="col-5 text-muted fw-normal">Notes</dt><dd class="col-7"><?= nl2br(htmlspecialchars($g['notes'])); ?></dd>
          <?php endif; ?>
        </dl>
      </div>
    </div>
  </div>

  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body">
        <h6 class="text-uppercase text-muted small mb-3"><i class="fas fa-map-marker-alt me-1"></i>Last Known Position</h6>
        <?php if ($g['last_position_ts']): ?>
          <dl class="row mb-3 small">
            <dt class="col-5 text-muted fw-normal">Fix time</dt><dd class="col-7"><?= humanDateTime($g['last_position_ts']); ?></dd>
            <dt class="col-5 text-muted fw-normal">Last seen (server)</dt>
            <dd class="col-7"><?= $g['last_seen_at'] ? humanDateTime($g['last_seen_at']) : '—'; ?><?php if ($stale): ?> <span class="badge bg-danger ms-1">stale</span><?php endif; ?></dd>
            <dt class="col-5 text-muted fw-normal">Coordinates</dt>
            <dd class="col-7 font-monospace"><?= number_format((float)$g['last_lat'], 6); ?>, <?= number_format((float)$g['last_lng'], 6); ?></dd>
            <dt class="col-5 text-muted fw-normal">Speed</dt>
            <dd class="col-7"><?= $g['last_speed'] !== null ? number_format((float)$g['last_speed'], 0) . ' kph' : '—'; ?></dd>
            <dt class="col-5 text-muted fw-normal">Ignition</dt>
            <dd class="col-7"><?= $g['last_ignition'] === null ? '—' : ($g['last_ignition'] ? '<span class="badge bg-success">On</span>' : '<span class="badge bg-secondary">Off</span>'); ?></dd>
            <dt class="col-5 text-muted fw-normal">Fuel level</dt>
            <dd class="col-7"><?= $g['last_fuel_level'] !== null ? number_format((float)$g['last_fuel_level'], 1) . ' L' : '—'; ?></dd>
            <?php if ($g['current_odometer']): ?>
              <dt class="col-5 text-muted fw-normal">Truck odo</dt>
              <dd class="col-7"><?= number_format((int)$g['current_odometer']); ?> km</dd>
            <?php endif; ?>
          </dl>
          <a href="https://www.google.com/maps/search/?api=1&query=<?= (float)$g['last_lat']; ?>,<?= (float)$g['last_lng']; ?>" target="_blank" rel="noopener" class="btn btn-sm btn-outline-primary">
            <i class="fas fa-external-link-alt me-1"></i>Open in Google Maps
          </a>
        <?php else: ?>
          <p class="text-muted fst-italic small mb-0">No positions received yet. Configure the tracker to POST to <code><?= htmlspecialchars(PROOT); ?>GpsIngest/webhook?token=***</code>.</p>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>

<div class="text-end mt-3">
  <button type="button" class="btn btn-sm btn-outline-danger" onclick="doDelete()"><i class="fas fa-trash me-1"></i>Remove device</button>
</div>

<form id="delForm" method="POST" action="<?= PROOT; ?>GpsDevices/delete/<?= (int)$g['c0d']; ?>" class="d-none"><?= csrf_field(); ?></form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
function doDelete() {
    $.confirm({
        title: 'Remove this device?',
        content: 'Soft-deletes the tracker. Historic positions and events are kept.',
        type: 'red',
        buttons: { remove: { text: 'Remove', btnClass: 'btn-danger', action: () => document.getElementById('delForm').submit() }, back: function(){} }
    });
}
</script>
<?php $this->end(); ?>
