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

<!-- Search & filters ---------------------------------------------------- -->
<form method="GET" action="<?= PROOT; ?>Trucks/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="Reg no · make · model · chassis" autocomplete="off">
        </div>
      </div>
      <div class="col-md-3">
        <label class="form-label small text-muted mb-1">Type</label>
        <select name="type_id" class="form-select form-select-sm">
          <option value="">All types</option>
          <?php foreach ($this->data['types'] as $tt): ?>
            <option value="<?= (int)$tt['c0d']; ?>" <?= $f['type_id']==$tt['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($tt['display_name']); ?>
            </option>
          <?php endforeach; ?>
        </select>
      </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 statuses</option>
          <?php foreach (['active','maintenance','inactive','retired'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['status']===$s?'selected':''; ?>><?= ucfirst($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; ?>Trucks" class="btn btn-sm btn-outline-secondary" title="Clear"><i class="fas fa-times"></i></a>
      </div>
    </div>
  </div>
</form>

<!-- Results table ------------------------------------------------------ -->
<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:130px;">Reg no</th>
          <th>Type</th>
          <th>Make / Model</th>
          <th class="money">Capacity (kg)</th>
          <th class="money">Odometer (km)</th>
          <th>Ownership</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <tr>
            <td><a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['reg_no']); ?></a></td>
            <td><span class="badge bg-light text-dark border"><?= htmlspecialchars($r['type_name'] ?? '—'); ?></span></td>
            <td>
              <?= htmlspecialchars(trim(($r['make'] ?? '') . ' ' . ($r['model'] ?? ''))); ?>
              <?php if (!empty($r['year'])): ?><span class="text-muted small">(<?= (int)$r['year']; ?>)</span><?php endif; ?>
            </td>
            <td class="money"><?= $r['capacity_kg']       ? number_format((int)$r['capacity_kg'])      : '—'; ?></td>
            <td class="money"><?= $r['current_odometer']  ? number_format((int)$r['current_odometer']) : '0'; ?></td>
            <td class="text-muted small"><?= htmlspecialchars(ucfirst($r['ownership'])); ?></td>
            <td>
              <?php
                $badge = ['active'=>'success','maintenance'=>'warning','inactive'=>'secondary','retired'=>'dark'][$r['status']] ?? 'secondary';
              ?>
              <span class="badge bg-<?= $badge; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span>
            </td>
            <td class="text-end">
              <a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="View"><i class="fas fa-eye"></i></a>
              <a href="<?= PROOT; ?>Trucks/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 trucks match those filters — <a href="<?= PROOT; ?>Trucks/add">add the first truck</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(); ?>
