<?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-clipboard-list me-2 text-muted"></i>Trip Requests
      <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <a href="<?= PROOT; ?>TripRequests/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> New Request</a>
</div>

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>TripRequests/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-4">
        <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="Request # · cargo · address · customer" autocomplete="off">
        </div>
      </div>
      <div class="col-md-2">
        <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 (['draft','pending_review','approved','rejected','cancelled','dispatched'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['status']===$s?'selected':''; ?>><?= ucfirst(str_replace('_',' ',$s)); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">Priority</label>
        <select name="priority" class="form-select form-select-sm">
          <option value="">All</option>
          <?php foreach (['low','normal','high','urgent'] as $p): ?>
            <option value="<?= $p; ?>" <?= $f['priority']===$p?'selected':''; ?>><?= ucfirst($p); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">Pickup from</label>
        <input type="date" name="from_date" class="form-control form-control-sm" value="<?= htmlspecialchars($f['from_date']); ?>">
      </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; ?>TripRequests" 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:100px;">Request #</th>
          <th>Customer</th>
          <th>Cargo</th>
          <th>Route</th>
          <th>Pickup</th>
          <th class="money">Quote</th>
          <th>Priority</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php
            $statusBg = ['draft'=>'secondary','pending_review'=>'warning','approved'=>'success','rejected'=>'danger','cancelled'=>'dark','dispatched'=>'primary'][$r['status']] ?? 'secondary';
            $prioBg   = ['low'=>'light','normal'=>'light','high'=>'warning','urgent'=>'danger'][$r['priority']] ?? 'light';
            $prioText = ['low','normal'][0] === $r['priority'] || $r['priority']==='normal' ? 'text-muted' : ($r['priority']==='high' ? 'text-dark' : 'text-white');
          ?>
          <tr>
            <td class="font-monospace small"><a href="<?= PROOT; ?>TripRequests/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['request_no']); ?></a></td>
            <td>
              <?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 small fst-italic">Internal</span><?php endif; ?>
            </td>
            <td class="small">
              <?= htmlspecialchars($r['cargo_description'] ?: '—'); ?>
              <div class="text-muted"><?= htmlspecialchars(ucwords(str_replace('_',' ',$r['cargo_type']))); ?><?php if ($r['weight_kg']): ?> · <?= number_format((float)$r['weight_kg'],0); ?> kg<?php endif; ?></div>
            </td>
            <td class="small">
              <?php if ($r['route_name']): ?>
                <?= htmlspecialchars($r['route_name']); ?>
              <?php else: ?>
                <?= htmlspecialchars($r['origin_address'] ?: '?'); ?> <i class="fas fa-arrow-right text-muted"></i> <?= htmlspecialchars($r['destination_address'] ?: '?'); ?>
              <?php endif; ?>
            </td>
            <td class="small"><?= $r['pickup_date'] ? humanDate($r['pickup_date']) : '—'; ?></td>
            <td class="money small"><?= $r['quote_amount'] ? htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['quote_amount'],0,'.',',') : '—'; ?></td>
            <td><span class="badge bg-<?= $prioBg; ?> <?= $prioText; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['priority']); ?></span></td>
            <td><span class="badge bg-<?= $statusBg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_',' ',$r['status'])); ?></span></td>
            <td class="text-end">
              <a href="<?= PROOT; ?>TripRequests/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 trip requests match those filters — <a href="<?= PROOT; ?>TripRequests/add">create the first one</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(); ?>
