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

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-pen me-2 text-muted"></i>Edit · <?= htmlspecialchars($g['device_code']); ?></h5>
  <a href="<?= PROOT; ?>GpsDevices/view/<?= (int)$g['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

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

<form method="POST" action="<?= PROOT; ?>GpsDevices/update/<?= (int)$g['c0d']; ?>" 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</label>
        <input type="text" name="device_code" class="form-control" value="<?= htmlspecialchars($f['device_code'] ?? ''); ?>"></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','unknown'=>'Unknown (auto-registered)','other'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['device_type'] ?? '')===$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','osmand'=>'OsmAnd','traccar'=>'Traccar','other'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['protocol'] ?? '')===$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'] ?? '')===$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'] ?? ''); ?>"></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">Fuel sensor</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">Dashcam</label>
        </div></div>

      <div class="col-12"><label class="form-label">Notes</label>
        <textarea name="notes" rows="2" class="form-control"><?= 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/view/<?= (int)$g['c0d']; ?>" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Save changes</button>
  </div>
</form>

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