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

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

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

<form method="POST" action="<?= PROOT; ?>Routes/update/<?= (int)$r['c0d']; ?>" 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'] ?? ''); ?>"></div>
      <div class="col-md-6"><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'] ?? ''); ?>"></div>
      <div class="col-md-2"><label class="form-label">Type</label>
        <select name="route_type" class="form-select">
          <?php foreach (['town'=>'Town','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-2"><label class="form-label">Status</label>
        <select name="active" class="form-select">
          <option value="1" <?= (int)($f['active'] ?? 1)===1?'selected':''; ?>>Active</option>
          <option value="0" <?= (int)($f['active'] ?? 1)===0?'selected':''; ?>>Inactive</option>
        </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"><?= 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-between py-3">
    <button type="button" class="btn btn-outline-danger" id="btnDelete"><i class="fas fa-trash me-1"></i>Remove route</button>
    <div class="d-flex gap-2">
      <a href="<?= PROOT; ?>Routes/view/<?= (int)$r['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>

<form id="deleteForm" method="POST" action="<?= PROOT; ?>Routes/delete/<?= (int)$r['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: 2 }));
document.getElementById('btnDelete')?.addEventListener('click', function () {
  $.confirm({
    title: 'Remove route?',
    content: 'Soft-deletes the route. Trip requests that reference this route keep their reference.',
    type: 'red',
    buttons: {
      remove: { text: 'Remove', btnClass: 'btn-danger', action: () => document.getElementById('deleteForm').submit() },
      cancel: function () {}
    }
  });
});
</script>
<?php $this->end(); ?>
