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

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-route me-2 text-muted"></i>Routes
      <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <a href="<?= PROOT; ?>Routes/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> Add Route</a>
</div>

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>Routes/index/1" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2">
    <div class="row g-2 align-items-end">
      <div class="col-md-5">
        <label class="form-label small text-muted mb-1">Search</label>
        <div class="input-group input-group-sm">
          <span class="input-group-text bg-white"><i class="fas fa-search text-muted"></i></span>
          <input type="text" class="form-control" name="q" value="<?= htmlspecialchars($f['q']); ?>" placeholder="Name · code · city" autocomplete="off">
        </div>
      </div>
      <div class="col-md-3">
        <label class="form-label small text-muted mb-1">Type</label>
        <select name="route_type" class="form-select form-select-sm">
          <option value="">All</option>
          <?php foreach (['town'=>'Town','regional'=>'Regional','intercity'=>'Intercity','cross_border'=>'Cross-border'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= $f['route_type']===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">Active</label>
        <select name="active" class="form-select form-select-sm">
          <option value="">All</option>
          <option value="1" <?= $f['active']===1?'selected':''; ?>>Active only</option>
          <option value="0" <?= $f['active']===0?'selected':''; ?>>Inactive only</option>
        </select>
      </div>
      <div class="col-md-2 d-flex gap-2">
        <button class="btn btn-sm btn-primary flex-grow-1"><i class="fas fa-filter me-1"></i>Apply</button>
        <a href="<?= PROOT; ?>Routes" class="btn btn-sm btn-outline-secondary" title="Clear"><i class="fas fa-times"></i></a>
      </div>
    </div>
  </div>
</form>

<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr>
          <th style="width:90px;">Code</th>
          <th>Name</th>
          <th>Type</th>
          <th>Origin → Destination</th>
          <th class="money">Distance</th>
          <th class="money">Duration</th>
          <th>Active</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <tr>
            <td class="font-monospace small"><?= htmlspecialchars($r['code'] ?: '—'); ?></td>
            <td><a href="<?= PROOT; ?>Routes/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['name']); ?></a></td>
            <td><span class="badge bg-light text-dark border" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_',' ',$r['route_type'])); ?></span></td>
            <td class="small">
              <?php if ($r['origin_city'] || $r['destination_city']): ?>
                <?= htmlspecialchars($r['origin_city'] ?: '?'); ?> <i class="fas fa-arrow-right text-muted mx-1"></i> <?= htmlspecialchars($r['destination_city'] ?: '?'); ?>
              <?php else: ?><span class="text-muted">—</span><?php endif; ?>
            </td>
            <td class="money"><?= $r['distance_km']       ? number_format((float)$r['distance_km'], 1) . ' km'   : '—'; ?></td>
            <td class="money"><?= $r['avg_duration_hrs']  ? number_format((float)$r['avg_duration_hrs'], 1) . ' h' : '—'; ?></td>
            <td>
              <?php if ($r['active']): ?><span class="badge bg-success">Active</span>
              <?php else: ?><span class="badge bg-secondary">Inactive</span><?php endif; ?>
            </td>
            <td class="text-end">
              <a href="<?= PROOT; ?>Routes/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="View"><i class="fas fa-eye"></i></a>
              <a href="<?= PROOT; ?>Routes/edit/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="Edit"><i class="fas fa-pen"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">
              No routes yet — <a href="<?= PROOT; ?>Routes/add">add the first route</a>.
          </td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if (!empty($this->data['pg_html'])): ?>
    <div class="card-footer bg-white d-flex justify-content-between align-items-center py-2">
      <span class="small text-muted">Page <?= (int)$this->data['page']; ?> · <?= (int)$this->data['total']; ?> total</span>
      <?= $this->data['pg_html']; ?>
    </div>
  <?php endif; ?>
</div>

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