<?php $this->start('body'); ?>

<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-clipboard-list me-2 text-muted"></i>PM Templates
      <span class="text-muted fw-normal fs-6">· <?= count($this->data['templates']); ?></span>
  </h5>
  <a href="<?= PROOT; ?>PmTemplates/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> New template</a>
</div>

<?= flashHtml(); ?>

<div class="alert alert-info small mb-3">
  <i class="fas fa-info-circle me-1"></i>
  Templates define <em>how often</em> a PM should happen. Each template can then be assigned to N trucks via the truck's PM card,
  where you set the baseline (last-done km + date) and the system computes the next-due point.
  Cron fires alerts via the same outbox as GPS alerts.
</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>Name</th><th>Kind</th><th class="money">Interval km</th><th class="money">Interval days</th><th class="money">Warn km / days</th><th>Priority</th><th class="money">Est. cost (TZS)</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['templates'] as $t):
          $pri = ['low'=>'secondary','normal'=>'info','high'=>'warning','critical'=>'danger'][$t['priority']] ?? 'secondary'; ?>
          <tr>
            <td class="small fw-semibold"><a href="<?= PROOT; ?>PmTemplates/edit/<?= (int)$t['c0d']; ?>" class="text-decoration-none"><?= htmlspecialchars($t['name']); ?></a><?php if ($t['notes']): ?><div class="text-muted small"><?= htmlspecialchars(mb_substr($t['notes'], 0, 90)); ?></div><?php endif; ?></td>
            <td class="small text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_', ' ', $t['kind'])); ?></td>
            <td class="money small"><?= $t['interval_km']   ? number_format((int)$t['interval_km']) : '—'; ?></td>
            <td class="money small"><?= $t['interval_days'] ? (int)$t['interval_days'] . ' d' : '—'; ?></td>
            <td class="money small text-muted"><?= number_format((int)$t['threshold_km']); ?> / <?= (int)$t['threshold_days']; ?>d</td>
            <td><span class="badge bg-<?= $pri; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($t['priority']); ?></span></td>
            <td class="money small"><?= $t['default_parts_cost'] ? number_format((float)$t['default_parts_cost'], 0) : '—'; ?></td>
            <td><?php if ($t['active']): ?><span class="badge bg-success">Active</span><?php else: ?><span class="badge bg-secondary">Disabled</span><?php endif; ?></td>
            <td class="text-end">
              <form method="POST" action="<?= PROOT; ?>PmTemplates/toggle/<?= (int)$t['c0d']; ?>" class="d-inline"><?= csrf_field(); ?><button class="btn btn-sm btn-outline-secondary" title="<?= $t['active']?'Disable':'Enable'; ?>"><i class="fas fa-power-off"></i></button></form>
              <a href="<?= PROOT; ?>PmTemplates/edit/<?= (int)$t['c0d']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-pen"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['templates'])): ?>
          <tr><td colspan="9" class="text-center text-muted py-5">No templates. <a href="<?= PROOT; ?>PmTemplates/add">Create the first one</a>.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

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