<?php $this->start('body'); ?>
<?php $s = $this->data['summary']; $topT = $this->data['top_trucks']; $topC = $this->data['top_customers'];
$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-chart-line me-2 text-muted"></i>Fleet Analytics</h5>
    <div class="text-muted small"><?= humanDate($from); ?> → <?= humanDate($to); ?></div>
  </div>
  <?php include __DIR__ . '/_range_form.php'; ?>
</div>

<?= flashHtml(); ?>

<!-- KPI strip -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Revenue</div><div class="fs-4 fw-bold text-success">TZS <?= number_format((float)$s['revenue'], 0); ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Costs</div><div class="fs-4 fw-bold text-danger">TZS <?= number_format((float)$s['costs'], 0); ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Margin</div><div class="fs-4 fw-bold <?= (float)$s['margin'] >= 0 ? 'text-success' : 'text-danger'; ?>">TZS <?= number_format((float)$s['margin'], 0); ?></div><div class="small text-muted"><?= number_format((float)$s['margin_pct'], 1); ?>%</div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Cost / Rev per km</div><div class="fs-6 fw-bold text-danger"><?= number_format((float)$s['cost_per_km'], 0); ?></div><div class="small text-success"><?= number_format((float)$s['revenue_per_km'], 0); ?></div></div></div></div>
</div>

<div class="row g-2 mb-4">
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Closed trips</div><div class="fs-5 fw-bold"><?= number_format((int)$s['closed_trips']); ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Total km</div><div class="fs-5 fw-bold"><?= number_format((float)$s['total_km'], 0); ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Active trucks</div><div class="fs-5 fw-bold"><?= (int)$s['active_trucks']; ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Active drivers</div><div class="fs-5 fw-bold"><?= (int)$s['active_drivers']; ?></div></div></div></div>
</div>

<div class="row g-3">
  <!-- Top 5 trucks by margin -->
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body">
        <div class="d-flex justify-content-between align-items-center mb-3">
          <h6 class="text-uppercase text-muted small mb-0">Top trucks by margin</h6>
          <a href="<?= PROOT; ?>FleetAnalytics/truckPnl?preset=<?= htmlspecialchars($preset); ?>&from=<?= htmlspecialchars($from); ?>&to=<?= htmlspecialchars($to); ?>" class="btn btn-sm btn-outline-secondary">Full P&amp;L →</a>
        </div>
        <table class="table table-sm mb-0 align-middle">
          <thead class="table-light"><tr><th>Truck</th><th class="money">Revenue</th><th class="money">Costs</th><th class="money">Margin</th><th class="money">%</th></tr></thead>
          <tbody>
            <?php foreach ($topT as $r): ?>
              <tr>
                <td class="small"><a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['truck_id']; ?>" class="fw-semibold text-decoration-none"><?= htmlspecialchars($r['reg_no']); ?></a><div class="text-muted small"><?= htmlspecialchars($r['truck_desc']); ?></div></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 <?= (float)$r['margin'] >= 0 ? 'text-success' : 'text-danger'; ?>"><?= number_format((float)$r['margin'], 0); ?></td>
                <td class="money small text-muted"><?= number_format((float)$r['margin_pct'], 1); ?>%</td>
              </tr>
            <?php endforeach; ?>
            <?php if (empty($topT)): ?><tr><td colspan="5" class="text-center text-muted py-3">No truck activity in this range.</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>

  <!-- Top 5 customers by revenue -->
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100">
      <div class="card-body">
        <div class="d-flex justify-content-between align-items-center mb-3">
          <h6 class="text-uppercase text-muted small mb-0">Top customers by revenue</h6>
          <a href="<?= PROOT; ?>FleetAnalytics/customerRevenue?preset=<?= htmlspecialchars($preset); ?>&from=<?= htmlspecialchars($from); ?>&to=<?= htmlspecialchars($to); ?>" class="btn btn-sm btn-outline-secondary">All customers →</a>
        </div>
        <table class="table table-sm mb-0 align-middle">
          <thead class="table-light"><tr><th>Customer</th><th class="money">Revenue</th><th class="money">Invoiced</th><th class="money">Outstanding</th></tr></thead>
          <tbody>
            <?php foreach ($topC as $r): ?>
              <tr>
                <td class="small"><a href="<?= PROOT; ?>Customers/view/<?= (int)$r['customer_id']; ?>" class="fw-semibold text-decoration-none"><?= htmlspecialchars($r['customer_name']); ?></a><?php if ($r['phone']): ?><div class="text-muted small"><?= htmlspecialchars($r['phone']); ?></div><?php endif; ?></td>
                <td class="money small text-success"><?= number_format((float)$r['revenue'], 0); ?></td>
                <td class="money small"><?= number_format((float)$r['invoiced'], 0); ?></td>
                <td class="money fw-semibold <?= (float)$r['outstanding'] > 0 ? 'text-danger' : 'text-muted'; ?>"><?= number_format((float)$r['outstanding'], 0); ?></td>
              </tr>
            <?php endforeach; ?>
            <?php if (empty($topC)): ?><tr><td colspan="4" class="text-center text-muted py-3">No customer activity.</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<div class="row g-3 mt-1">
  <div class="col-12">
    <div class="card border-0 shadow-sm">
      <div class="card-body py-3 d-flex justify-content-around gap-2 flex-wrap">
        <a href="<?= PROOT; ?>FleetAnalytics/matrix?preset=<?= htmlspecialchars($preset); ?>" class="btn btn-primary"><i class="fas fa-th me-1"></i>Truck Matrix (deep-drill)</a>
        <a href="<?= PROOT; ?>FleetAnalytics/truckPnl?preset=<?= htmlspecialchars($preset); ?>" class="btn btn-outline-primary"><i class="fas fa-truck me-1"></i>Truck P&amp;L</a>
        <a href="<?= PROOT; ?>FleetAnalytics/customerRevenue?preset=<?= htmlspecialchars($preset); ?>" class="btn btn-outline-primary"><i class="fas fa-building me-1"></i>Customer Revenue</a>
        <a href="<?= PROOT; ?>FleetAnalytics/driverScorecards?preset=<?= htmlspecialchars($preset); ?>" class="btn btn-outline-primary"><i class="fas fa-user-tie me-1"></i>Driver Scorecards</a>
      </div>
    </div>
  </div>
</div>

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