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

<form method="GET" action="<?= PROOT; ?>Drivers/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 · employee no · ID no · phone · licence" 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 (['available','on_trip','on_leave','suspended','terminated'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['status']===$s?'selected':''; ?>><?= ucfirst(str_replace('_',' ',$s)); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-3">
        <label class="form-label small text-muted mb-1">Licence expiring within</label>
        <select name="licence_expiring_days" class="form-select form-select-sm">
          <option value="0">Any</option>
          <?php foreach ([7=>'7 days',30=>'30 days',60=>'60 days',90=>'90 days'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= $f['licence_expiring_days']==$k?'selected':''; ?>><?= $v; ?></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; ?>Drivers" 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>Name</th>
          <th>Employee #</th>
          <th>Phone</th>
          <th>Licence</th>
          <th>Licence expires</th>
          <th>Hired</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php
            $expired = !empty($r['licence_expires']) && strtotime($r['licence_expires']) < time();
            $expSoon = !$expired && !empty($r['licence_expires']) && strtotime($r['licence_expires']) < strtotime('+30 days');
          ?>
          <tr>
            <td><a href="<?= PROOT; ?>Drivers/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['full_name']); ?></a></td>
            <td class="text-muted small"><?= htmlspecialchars($r['employee_no'] ?? '—'); ?></td>
            <td><?= htmlspecialchars($r['phone_1'] ?? '—'); ?></td>
            <td class="font-monospace small"><?= htmlspecialchars($r['licence_no'] ?? '—'); ?><?php if ($r['licence_class']): ?> <span class="badge bg-light text-dark border small"><?= htmlspecialchars($r['licence_class']); ?></span><?php endif; ?></td>
            <td class="small">
              <?php if ($r['licence_expires']): ?>
                <?= humanDate($r['licence_expires']); ?>
                <?php if ($expired): ?><span class="badge bg-danger ms-1">Expired</span>
                <?php elseif ($expSoon): ?><span class="badge bg-warning text-dark ms-1">Soon</span><?php endif; ?>
              <?php else: ?>—<?php endif; ?>
            </td>
            <td class="small"><?= $r['hire_date'] ? humanDate($r['hire_date']) : '—'; ?></td>
            <td>
              <?php $bg = ['available'=>'success','on_trip'=>'primary','on_leave'=>'info','suspended'=>'warning','terminated'=>'dark'][$r['status']] ?? 'secondary'; ?>
              <span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_',' ',$r['status'])); ?></span>
            </td>
            <td class="text-end">
              <a href="<?= PROOT; ?>Drivers/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="View"><i class="fas fa-eye"></i></a>
              <a href="<?= PROOT; ?>Drivers/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 drivers match those filters — <a href="<?= PROOT; ?>Drivers/add">add the first driver</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(); ?>
