<?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-truck-loading me-2 text-muted"></i>Trips
      <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <a href="<?= PROOT; ?>Trips/board" class="btn btn-sm btn-primary"><i class="fas fa-th-list me-1"></i>Dispatch Board</a>
</div>

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>Trips/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="Trip # · cargo · driver · truck · customer" autocomplete="off">
        </div>
      </div>
      <div class="col-md-3">
        <label class="form-label small text-muted mb-1">Status</label>
        <select name="status" class="form-select form-select-sm">
          <option value="">All</option>
          <?php foreach (['dispatched','in_transit','delivered','closed','cancelled'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['status']===$s?'selected':''; ?>><?= ucfirst(str_replace('_',' ',$s)); ?></option>
          <?php endforeach; ?>
        </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; ?>Trips" 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:110px;">Trip #</th>
          <th>Driver / Truck</th>
          <th>Customer / Cargo</th>
          <th>Route</th>
          <th>Pickup</th>
          <th class="money">Km</th>
          <th class="money">Invoice</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php $bg = ['dispatched'=>'primary','in_transit'=>'info','delivered'=>'warning','closed'=>'success','cancelled'=>'dark'][$r['status']] ?? 'secondary'; ?>
          <tr>
            <td class="font-monospace small"><a href="<?= PROOT; ?>Trips/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['trip_no']); ?></a></td>
            <td class="small">
              <a href="<?= PROOT; ?>Drivers/view/<?= (int)$r['driver_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($r['driver_name'] ?? '?'); ?></a><br>
              <a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['truck_id']; ?>" class="text-decoration-none text-muted"><i class="fas fa-truck fa-xs me-1"></i><?= htmlspecialchars($r['truck_reg'] ?? '?'); ?></a>
            </td>
            <td class="small">
              <?php if ($r['customer_name']): ?><a href="<?= PROOT; ?>Customers/view/<?= (int)$r['customer_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($r['customer_name']); ?></a><?php else: ?><span class="text-muted fst-italic">Internal</span><?php endif; ?><br>
              <span class="text-muted"><?= htmlspecialchars($r['cargo_description'] ?: '—'); ?></span>
            </td>
            <td class="small">
              <?php if ($r['route_name']): ?>
                <a href="<?= PROOT; ?>Routes/view/<?= (int)$r['route_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($r['route_name']); ?></a>
              <?php else: ?>
                <span class="text-muted"><?= htmlspecialchars($r['origin_address'] ?: '?'); ?> → <?= htmlspecialchars($r['destination_address'] ?: '?'); ?></span>
              <?php endif; ?>
            </td>
            <td class="small"><?= $r['actual_start'] ? humanDateTime($r['actual_start']) : ($r['planned_pickup'] ? humanDateTime($r['planned_pickup']) . ' <span class="text-muted small">(planned)</span>' : '—'); ?></td>
            <td class="money small"><?= $r['actual_km'] ? number_format((float)$r['actual_km'], 0) : ($r['planned_km'] ? '<span class="text-muted">' . number_format((float)$r['planned_km'], 0) . '</span>' : '—'); ?></td>
            <td class="money small"><?= $r['invoice_amount'] ? htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['invoice_amount'], 0, '.', ',') : ($r['quote_amount'] ? '<span class="text-muted">' . htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['quote_amount'], 0, '.', ',') . '</span>' : '—'); ?></td>
            <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_',' ',$r['status'])); ?></span></td>
            <td class="text-end">
              <a href="<?= PROOT; ?>Trips/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="View"><i class="fas fa-eye"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="9" class="text-center text-muted py-5">
              No trips yet — trips are created from approved requests via the <a href="<?= PROOT; ?>Trips/board">Dispatch Board</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(); ?>
