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

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>DriverAssignments/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-3">
        <label class="form-label small text-muted mb-1">Driver ID</label>
        <input type="number" name="driver_id" class="form-control form-control-sm" value="<?= $f['driver_id'] ?: ''; ?>" placeholder="Any">
      </div>
      <div class="col-md-3">
        <label class="form-label small text-muted mb-1">Truck ID</label>
        <input type="number" name="truck_id" class="form-control form-control-sm" value="<?= $f['truck_id'] ?: ''; ?>" placeholder="Any">
      </div>
      <div class="col-md-4">
        <label class="form-label small text-muted mb-1">Status</label>
        <select name="status" class="form-select form-select-sm">
          <option value="">All</option>
          <option value="active" <?= $f['status']==='active'?'selected':''; ?>>Active only</option>
          <option value="ended"  <?= $f['status']==='ended' ?'selected':''; ?>>Ended 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; ?>DriverAssignments" 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>Driver</th>
          <th>Truck</th>
          <th>Role</th>
          <th>From</th>
          <th>To</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php $active = empty($r['assigned_to']); ?>
          <tr>
            <td><a href="<?= PROOT; ?>Drivers/view/<?= (int)$r['driver_id']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['driver_name'] ?? '?'); ?></a>
                <?php if ($r['employee_no']): ?><div class="text-muted small"><?= htmlspecialchars($r['employee_no']); ?></div><?php endif; ?></td>
            <td><a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['truck_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($r['truck_reg'] ?? '?'); ?></a>
                <div class="text-muted small"><?= htmlspecialchars(trim(($r['make'] ?? '') . ' ' . ($r['model'] ?? ''))); ?></div></td>
            <td><span class="badge bg-light text-dark border" style="text-transform:capitalize;"><?= htmlspecialchars($r['role_type']); ?></span></td>
            <td class="small"><?= humanDate($r['assigned_from']); ?></td>
            <td class="small"><?= $r['assigned_to'] ? humanDate($r['assigned_to']) : '—'; ?></td>
            <td>
              <?php if ($active): ?><span class="badge bg-success">Active</span>
              <?php else: ?><span class="badge bg-secondary">Ended</span><?php endif; ?>
            </td>
            <td class="text-end">
              <a href="<?= PROOT; ?>DriverAssignments/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="7" class="text-center text-muted py-5">
              No assignments match those filters — <a href="<?= PROOT; ?>DriverAssignments/add">create the first assignment</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(); ?>
