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

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-plus me-2 text-muted"></i>Register GPS Device</h5>
  <a href="<?= PROOT; ?>GpsDevices" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= $this->raw('fb_msg'); ?>

<div class="alert alert-info small">
  <i class="fas fa-info-circle me-1"></i>
  Register a Teltonika (or other) tracker. <strong>Device code</strong> is the IMEI on Codec 8E devices or the unique-id on Osmand.
  Point the tracker (or Traccar bridge) at <code><?= htmlspecialchars(PROOT); ?>GpsIngest/webhook?token=***</code> to start receiving positions.
</div>

<form method="POST" action="<?= PROOT; ?>GpsDevices/save" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-4"><label class="form-label">Device code / IMEI <span class="text-danger">*</span></label>
        <input type="text" name="device_code" class="form-control" required
               value="<?= htmlspecialchars($f['device_code'] ?? ''); ?>"
               placeholder="e.g. 352093088123456"></div>
      <div class="col-md-4"><label class="form-label">Device type</label>
        <select name="device_type" class="form-select">
          <?php foreach (['teltonika_fmc920'=>'Teltonika FMC920','teltonika_fmb920'=>'Teltonika FMB920','traccar_generic'=>'Traccar generic','streamax_x1n'=>'Streamax X1N','other'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['device_type'] ?? 'teltonika_fmc920')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-4"><label class="form-label">Protocol</label>
        <select name="protocol" class="form-select">
          <?php foreach (['codec_8e'=>'Codec 8E (Teltonika native)','osmand'=>'OsmAnd (mobile / generic)','traccar'=>'Traccar-forward','other'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['protocol'] ?? 'codec_8e')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>

      <div class="col-md-4"><label class="form-label">Fitted to truck</label>
        <select name="truck_id" class="form-select">
          <option value="">— spare / unassigned —</option>
          <?php foreach ($this->data['trucks'] as $t): ?>
            <option value="<?= (int)$t['c0d']; ?>" <?= ($f['truck_id'] ?? '')==$t['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($t['reg_no']); ?><?php if ($t['make']): ?> · <?= htmlspecialchars(trim($t['make'] . ' ' . $t['model'])); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><label class="form-label">Status</label>
        <select name="status" class="form-select">
          <?php foreach (['active','inactive','offline','retired'] as $s): ?>
            <option value="<?= $s; ?>" <?= ($f['status'] ?? 'active')===$s?'selected':''; ?>><?= ucfirst($s); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">SIM MSISDN</label>
        <input type="text" name="sim_msisdn" class="form-control" value="<?= htmlspecialchars($f['sim_msisdn'] ?? ''); ?>" placeholder="+2557..."></div>
      <div class="col-md-3"><label class="form-label">SIM ICCID</label>
        <input type="text" name="sim_iccid" class="form-control" value="<?= htmlspecialchars($f['sim_iccid'] ?? ''); ?>"></div>

      <div class="col-md-3"><label class="form-label">Installed on</label>
        <input type="date" name="installed_on" class="form-control" value="<?= htmlspecialchars($f['installed_on'] ?? ''); ?>"></div>
      <div class="col-md-3"><label class="form-label">Firmware version</label>
        <input type="text" name="firmware_ver" class="form-control" value="<?= htmlspecialchars($f['firmware_ver'] ?? ''); ?>"></div>
      <div class="col-md-3 d-flex align-items-end">
        <div class="form-check">
          <input type="checkbox" name="fuel_sensor" value="1" class="form-check-input" id="chkFs" <?= !empty($f['fuel_sensor'])?'checked':''; ?>>
          <label class="form-check-label" for="chkFs">Has fuel sensor (Escort TD-500 etc.)</label>
        </div></div>
      <div class="col-md-3 d-flex align-items-end">
        <div class="form-check">
          <input type="checkbox" name="dashcam" value="1" class="form-check-input" id="chkDc" <?= !empty($f['dashcam'])?'checked':''; ?>>
          <label class="form-check-label" for="chkDc">Has dashcam</label>
        </div></div>

      <div class="col-12"><label class="form-label">Notes</label>
        <textarea name="notes" rows="2" class="form-control" placeholder="Location on truck, mounting notes, wiring, gotchas..."><?= htmlspecialchars($f['notes'] ?? ''); ?></textarea></div>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>GpsDevices" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Register device</button>
  </div>
</form>

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