<?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-truck me-2 text-muted"></i>Truck P&amp;L
      <span class="text-muted fw-normal fs-6">· <?= count($rows); ?> trucks</span>
    </h5>
    <div class="text-muted small"><?= humanDate($from); ?> → <?= humanDate($to); ?></div>
  </div>
  <?php include __DIR__ . '/_range_form.php'; ?>
</div>

<?= flashHtml(); ?>

<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>Truck</th><th class="money">Trips</th><th class="money">Km</th><th class="money">Revenue</th><th class="money">Costs</th><th class="money">Margin</th><th class="money">%</th><th class="money">Cost/km</th><th class="money">Rev/km</th></tr>
      </thead>
      <tbody>
        <?php $totRev = 0; $totCost = 0; $totKm = 0; foreach ($rows as $r):
          $totRev += (float)$r['revenue']; $totCost += (float)$r['costs']; $totKm += (float)$r['km'];
          $marginBg = (float)$r['margin'] > 0 ? 'text-success' : ((float)$r['margin'] < 0 ? 'text-danger' : 'text-muted');
        ?>
          <tr>
            <td class="small"><a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['truck_id']; ?>" class="fw-semibold text-decoration-none"><?= htmlspecialchars($r['reg_no']); ?></a><?php if ($r['truck_desc']): ?><div class="text-muted small"><?= htmlspecialchars($r['truck_desc']); ?></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'], 0); ?></td>
            <td class="money small text-danger"><?= number_format((float)$r['costs'], 0); ?></td>
            <td class="money fw-semibold <?= $marginBg; ?>"><?= number_format((float)$r['margin'], 0); ?></td>
            <td class="money small <?= $marginBg; ?>"><?= number_format((float)$r['margin_pct'], 1); ?>%</td>
            <td class="money small"><?= (float)$r['km'] > 0 ? number_format((float)$r['cost_per_km'], 0) : '—'; ?></td>
            <td class="money small"><?= (float)$r['km'] > 0 ? number_format((float)$r['rev_per_km'], 0) : '—'; ?></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($rows)): ?>
          <tr><td colspan="9" class="text-center text-muted py-5">No trucks with activity in this range.</td></tr>
        <?php endif; ?>
      </tbody>
      <?php if (!empty($rows)): ?>
        <tfoot class="table-light">
          <tr>
            <th>Total</th><th class="money">—</th>
            <th class="money"><?= number_format($totKm, 0); ?></th>
            <th class="money text-success"><?= number_format($totRev, 0); ?></th>
            <th class="money text-danger"><?= number_format($totCost, 0); ?></th>
            <th class="money fw-bold <?= $totRev - $totCost >= 0 ? 'text-success' : 'text-danger'; ?>"><?= number_format($totRev - $totCost, 0); ?></th>
            <th class="money"><?= $totRev > 0 ? number_format(($totRev - $totCost) / $totRev * 100, 1) . '%' : '—'; ?></th>
            <th class="money"><?= $totKm > 0 ? number_format($totCost / $totKm, 0) : '—'; ?></th>
            <th class="money"><?= $totKm > 0 ? number_format($totRev / $totKm, 0) : '—'; ?></th>
          </tr>
        </tfoot>
      <?php endif; ?>
    </table>
  </div>
</div>

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