<?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>Add Route</h5>
  <a href="<?= PROOT; ?>Routes" 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; ?>Routes/save" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-2"><label class="form-label">Code</label>
        <input type="text" name="code" class="form-control text-uppercase" value="<?= htmlspecialchars($f['code'] ?? ''); ?>" placeholder="DAR-ARU"></div>
      <div class="col-md-7"><label class="form-label">Route name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" required value="<?= htmlspecialchars($f['name'] ?? ''); ?>" placeholder="e.g. Dar es Salaam → Arusha"></div>
      <div class="col-md-3"><label class="form-label">Type</label>
        <select name="route_type" class="form-select">
          <?php foreach (['town'=>'Town (dump truck)','regional'=>'Regional','intercity'=>'Intercity','cross_border'=>'Cross-border'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['route_type'] ?? 'town')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>

      <div class="col-md-4"><label class="form-label">Origin city</label>
        <input type="text" name="origin_city" class="form-control" value="<?= htmlspecialchars($f['origin_city'] ?? ''); ?>"></div>
      <div class="col-md-4"><label class="form-label">Destination city</label>
        <input type="text" name="destination_city" class="form-control" value="<?= htmlspecialchars($f['destination_city'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Distance (km)</label>
        <input type="text" name="distance_km" class="form-control js-num" value="<?= htmlspecialchars((string)($f['distance_km'] ?? '')); ?>"></div>
      <div class="col-md-2"><label class="form-label">Avg duration (hrs)</label>
        <input type="text" name="avg_duration_hrs" class="form-control js-num" value="<?= htmlspecialchars((string)($f['avg_duration_hrs'] ?? '')); ?>"></div>

      <div class="col-12"><label class="form-label">Stops / waypoints</label>
        <textarea name="stops_summary" rows="3" class="form-control" placeholder="One per line — e.g.&#10;Chalinze weighbridge&#10;Morogoro fuel stop&#10;Dodoma overnight rest"><?= htmlspecialchars($f['stops_summary'] ?? ''); ?></textarea></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; ?>Routes" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Save route</button>
  </div>
</form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
document.querySelectorAll('.js-num').forEach(el => new Cleave(el, { numeral: true, numeralThousandsGroupStyle: 'thousand', numeralDecimalScale: 2 }));
document.querySelectorAll('input[name="code"]').forEach(el => el.addEventListener('input', e => e.target.value = e.target.value.toUpperCase()));
</script>
<?php $this->end(); ?>
