<?php $this->start('body'); ?>
<?php $rows = $this->data['rows']; $from = $this->data['from']; $to = $this->data['to']; $preset = $this->data['preset']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-user-tie me-2 text-muted"></i>Driver Scorecards
      <span class="text-muted fw-normal fs-6">· <?= count($rows); ?> drivers</span>
    </h5>
    <div class="text-muted small"><?= humanDate($from); ?> → <?= humanDate($to); ?></div>
  </div>
  <?php include __DIR__ . '/_range_form.php'; ?>
</div>

<?= flashHtml(); ?>

<div class="alert alert-info small mb-3">
  <i class="fas fa-info-circle me-1"></i>
  Score starts at <strong>100</strong>. Penalties: <strong>-5</strong> per over-speed event per 1,000 km ·
  <strong>-10</strong> per fuel-drop event · <strong>-3</strong> per after-hours ignition · <strong>-2</strong> per harsh-brake per 1,000 km.
  Grades: A ≥ 90 · B ≥ 75 · C ≥ 60 · D ≥ 40 · F below 40.
</div>

<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 class="money">Trips</th><th class="money">Km</th><th class="money">Revenue</th>
          <th class="money" title="Over-speed events">Over</th><th class="money" title="Fuel drop events">Fuel</th>
          <th class="money" title="After-hours ignition">AH</th><th class="money" title="Harsh brake events">HB</th>
          <th class="money">Score</th><th>Grade</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($rows as $r):
          $g = $r['grade'];
          $bg = ['A' => 'success', 'B' => 'info', 'C' => 'warning', 'D' => 'danger', 'F' => 'dark'][$g] ?? 'secondary';
        ?>
          <tr>
            <td class="small"><a href="<?= PROOT; ?>Drivers/view/<?= (int)$r['driver_id']; ?>" class="fw-semibold text-decoration-none"><?= htmlspecialchars($r['full_name']); ?></a><?php if ($r['employee_no']): ?><div class="text-muted small"><?= htmlspecialchars($r['employee_no']); ?></div><?php endif; ?></td>
            <td class="money small"><?= (int)$r['trips']; ?></td>
            <td class="money small"><?= number_format((float)$r['km'], 0); ?></td>
            <td class="money small text-success"><?= number_format((float)$r['revenue_attributed'], 0); ?></td>
            <td class="money small <?= (int)$r['over_speed'] > 0 ? 'text-warning' : 'text-muted'; ?>"><?= (int)$r['over_speed']; ?></td>
            <td class="money small <?= (int)$r['fuel_drop']  > 0 ? 'text-danger'  : 'text-muted'; ?>"><?= (int)$r['fuel_drop']; ?></td>
            <td class="money small <?= (int)$r['after_hours']> 0 ? 'text-warning' : 'text-muted'; ?>"><?= (int)$r['after_hours']; ?></td>
            <td class="money small <?= (int)$r['harsh_brake']> 0 ? 'text-warning' : 'text-muted'; ?>"><?= (int)$r['harsh_brake']; ?></td>
            <td class="money fw-bold"><?= number_format((float)$r['score'], 1); ?></td>
            <td><span class="badge bg-<?= $bg; ?>" style="font-size:.9em;padding:.35em .55em;"><?= htmlspecialchars($g); ?></span></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?>
          <tr><td colspan="10" class="text-center text-muted py-5">No driver activity in this range.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php $this->end(); ?>
