<?php $this->start('body'); ?>

<?php $f = $this->data['form']; $t = $this->data['t']; ?>

<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($t['reg_no']); ?></h5>
  <a href="<?= PROOT; ?>Trucks/view/<?= (int)$t['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back to truck</a>
</div>

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

<form method="POST" action="<?= PROOT; ?>Trucks/update/<?= (int)$t['c0d']; ?>" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">

    <h6 class="text-uppercase text-muted small mb-3">Identity</h6>
    <div class="row g-3">
      <div class="col-md-4">
        <label class="form-label">Registration no <span class="text-danger">*</span></label>
        <input type="text" name="reg_no" class="form-control text-uppercase" required value="<?= htmlspecialchars($f['reg_no'] ?? ''); ?>">
      </div>
      <div class="col-md-4">
        <label class="form-label">Truck type</label>
        <select name="truck_type_id" class="form-select">
          <option value="">— select —</option>
          <?php foreach ($this->data['types'] as $tt): ?>
            <option value="<?= (int)$tt['c0d']; ?>" <?= ($f['truck_type_id'] ?? '')==$tt['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($tt['display_name']); ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-4">
        <label class="form-label">Status</label>
        <select name="status" class="form-select">
          <?php foreach (['active'=>'Active','maintenance'=>'In maintenance','inactive'=>'Inactive','retired'=>'Retired'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['status'] ?? 'active')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select>
      </div>

      <div class="col-md-4"><label class="form-label">Make</label><input type="text" name="make"  class="form-control" value="<?= htmlspecialchars($f['make']  ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Model</label><input type="text" name="model" class="form-control" value="<?= htmlspecialchars($f['model'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Year</label><input type="text" name="year" class="form-control js-year" maxlength="4" value="<?= htmlspecialchars((string)($f['year'] ?? '')); ?>"></div>
      <div class="col-md-2"><label class="form-label">Colour</label><input type="text" name="colour" class="form-control" value="<?= htmlspecialchars($f['colour'] ?? ''); ?>"></div>

      <div class="col-md-6"><label class="form-label">Chassis no</label><input type="text" name="chassis_no" class="form-control" value="<?= htmlspecialchars($f['chassis_no'] ?? ''); ?>"></div>
      <div class="col-md-6"><label class="form-label">Engine no</label><input type="text" name="engine_no" class="form-control" value="<?= htmlspecialchars($f['engine_no'] ?? ''); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Capacity &amp; Odometer</h6>
    <div class="row g-3">
      <div class="col-md-3"><label class="form-label">Tare (kg)</label>          <input type="text" name="tare_kg"          class="form-control js-num" value="<?= htmlspecialchars((string)($f['tare_kg'] ?? '')); ?>"></div>
      <div class="col-md-3"><label class="form-label">Capacity (kg)</label>      <input type="text" name="capacity_kg"      class="form-control js-num" value="<?= htmlspecialchars((string)($f['capacity_kg'] ?? '')); ?>"></div>
      <div class="col-md-3"><label class="form-label">Fuel tank (litres)</label> <input type="text" name="fuel_tank_liters" class="form-control js-num" value="<?= htmlspecialchars((string)($f['fuel_tank_liters'] ?? '')); ?>"></div>
      <div class="col-md-3"><label class="form-label">Current odometer (km)</label><input type="text" name="current_odometer" class="form-control js-num" value="<?= htmlspecialchars((string)($f['current_odometer'] ?? '0')); ?>"></div>
    </div>

    <hr class="my-4">
    <h6 class="text-uppercase text-muted small mb-3">Ownership</h6>
    <div class="row g-3">
      <div class="col-md-3">
        <label class="form-label">Ownership</label>
        <select name="ownership" class="form-select">
          <?php foreach (['owned'=>'Owned','leased'=>'Leased','contracted'=>'Contracted'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['ownership'] ?? 'owned')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-3"><label class="form-label">Acquired on</label><input type="date" name="acquired_on" class="form-control" value="<?= htmlspecialchars($f['acquired_on'] ?? ''); ?>"></div>
      <div class="col-md-6"><label class="form-label">Notes</label>       <input type="text" name="notes"       class="form-control" value="<?= htmlspecialchars($f['notes']       ?? ''); ?>"></div>
    </div>

  </div>
  <div class="card-footer bg-white d-flex justify-content-between py-3">
    <button type="button" class="btn btn-outline-danger" id="btnDelete"><i class="fas fa-trash me-1"></i>Remove truck</button>
    <div class="d-flex gap-2">
      <a href="<?= PROOT; ?>Trucks/view/<?= (int)$t['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>
  </div>
</form>

<!-- Hidden delete form (submitted from jQuery-confirm dialog) -->
<form id="deleteForm" method="POST" action="<?= PROOT; ?>Trucks/delete/<?= (int)$t['c0d']; ?>" class="d-none">
  <?= csrf_field(); ?>
</form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
document.querySelectorAll('.js-num').forEach(el => new Cleave(el, { numeral: true, numeralThousandsGroupStyle: 'thousand', numeralDecimalScale: 0 }));
document.querySelectorAll('.js-year').forEach(el => new Cleave(el, { numericOnly: true, blocks: [4] }));
var reg = document.querySelector('input[name="reg_no"]');
if (reg) reg.addEventListener('input', e => e.target.value = e.target.value.toUpperCase());

document.getElementById('btnDelete')?.addEventListener('click', function () {
  $.confirm({
    title: 'Remove truck?',
    content: 'This soft-deletes the truck. Its history and documents will be preserved but it will no longer appear in the fleet register.',
    type: 'red',
    buttons: {
      remove: { text: 'Remove', btnClass: 'btn-danger', action: () => document.getElementById('deleteForm').submit() },
      cancel: function () {}
    }
  });
});
</script>
<?php $this->end(); ?>
