<?php $this->start('body'); ?>
<?php $f = $this->data['filters']; $c = $this->data['counts']; $open = (int)$c['upcoming'] + (int)$c['due'] + (int)$c['overdue']; ?>

<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-tools me-2 text-muted"></i>PM Dashboard
      <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>PmTemplates" class="btn btn-sm btn-outline-secondary"><i class="fas fa-clipboard-list me-1"></i> Templates</a>
    <form method="POST" action="<?= PROOT; ?>PmDashboard/triggerNow" class="d-inline"><?= csrf_field(); ?>
      <button class="btn btn-sm btn-outline-primary" title="Run the PM sweep immediately without waiting for cron"><i class="fas fa-sync me-1"></i>Sweep now</button>
    </form>
  </div>
</div>

<?= flashHtml(); ?>

<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">Overdue</div><div class="fs-5 fw-bold text-danger"><?= (int)$c['overdue']; ?></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">Due</div><div class="fs-5 fw-bold text-warning"><?= (int)$c['due']; ?></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">Upcoming</div><div class="fs-5 fw-bold text-info"><?= (int)$c['upcoming']; ?></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">Completed</div><div class="fs-5 fw-bold text-success"><?= (int)$c['completed']; ?></div></div></div></div>
</div>

<ul class="nav nav-tabs mb-3 small">
  <?php $tabs = ['open'=>['All open',$open], 'overdue'=>['Overdue',$c['overdue']], 'due'=>['Due',$c['due']], 'upcoming'=>['Upcoming',$c['upcoming']], 'completed'=>['Completed',$c['completed']], 'skipped'=>['Skipped',$c['skipped']]];
    foreach ($tabs as $st => [$lab,$n]): ?>
    <li class="nav-item">
      <a class="nav-link <?= $f['status']===$st ? 'active' : ''; ?>" href="<?= PROOT; ?>PmDashboard?status=<?= $st; ?>">
        <?= htmlspecialchars($lab); ?>
        <?php if ($n > 0): ?><span class="badge bg-secondary ms-1"><?= (int)$n; ?></span><?php endif; ?>
      </a>
    </li>
  <?php endforeach; ?>
</ul>

<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>Event #</th><th>Truck</th><th>Template</th><th>Kind</th>
          <th class="money">Planned km</th><th class="money">Current km</th>
          <th>Planned date</th><th>Severity</th><th>Status</th><th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
      <?php foreach ($this->data['rows'] as $r):
        $bg  = ['upcoming'=>'info','due'=>'warning','overdue'=>'danger','completed'=>'success','skipped'=>'secondary'][$r['status']] ?? 'secondary';
        $sev = ['low'=>'secondary','normal'=>'info','medium'=>'warning','high'=>'danger','critical'=>'danger'][$r['severity']] ?? 'secondary';
      ?>
        <tr>
          <td class="small"><a href="<?= PROOT; ?>PmDashboard/view/<?= (int)$r['c0d']; ?>" class="fw-semibold font-monospace text-decoration-none"><?= htmlspecialchars($r['event_no']); ?></a></td>
          <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['make']): ?><div class="text-muted small"><?= htmlspecialchars(trim($r['make'] . ' ' . $r['model'])); ?></div><?php endif; ?></td>
          <td class="small"><?= htmlspecialchars($r['template_name']); ?></td>
          <td class="small text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_', ' ', $r['kind'])); ?></td>
          <td class="money small"><?= $r['planned_km'] ? number_format((int)$r['planned_km']) : '—'; ?><?php if ((int)$r['km_over'] > 0): ?><div class="text-danger small">+<?= number_format((int)$r['km_over']); ?> km over</div><?php endif; ?></td>
          <td class="money small"><?= $r['current_odometer'] ? number_format((int)$r['current_odometer']) : '—'; ?></td>
          <td class="small"><?= $r['planned_date'] ? humanDate($r['planned_date']) : '—'; ?><?php if ((int)$r['days_over'] > 0): ?><div class="text-danger small"><?= (int)$r['days_over']; ?> days late</div><?php endif; ?></td>
          <td><span class="badge bg-<?= $sev; ?>"><?= htmlspecialchars($r['severity']); ?></span></td>
          <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span></td>
          <td class="text-end"><a href="<?= PROOT; ?>PmDashboard/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a></td>
        </tr>
      <?php endforeach; ?>
      <?php if (empty($this->data['rows'])): ?>
        <tr><td colspan="10" class="text-center text-muted py-5">Nothing to service right now 🚚</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 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(); ?>
