<?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-bell me-2 text-muted"></i>Alert Rules
      <span class="text-muted fw-normal fs-6">· <?= count($this->data['rules']); ?></span>
  </h5>
  <a href="<?= PROOT; ?>AlertRules/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> New Rule</a>
</div>

<?= flashHtml(); ?>

<div class="alert alert-info small mb-3">
  <i class="fas fa-info-circle me-1"></i>
  Rules govern when alerts fire and who is notified. Sync rules (<strong>over_speed</strong>, <strong>fuel_drop</strong>,
  <strong>ignition_after_hours</strong>, <strong>geofence_enter/exit</strong>) evaluate on each incoming position.
  Async rules (<strong>device_offline</strong>) evaluate every 5 min via cron.
</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>Rule</th><th>Kind</th><th>Severity</th><th>Threshold</th><th>Notify</th><th>Cooldown</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rules'] as $r):
          $sev = ['info'=>'secondary','low'=>'info','medium'=>'warning','high'=>'danger','critical'=>'danger'][$r['severity']] ?? 'secondary';
          $th = json_decode((string)$r['threshold_json'], true);
        ?>
          <tr>
            <td class="small"><a href="<?= PROOT; ?>AlertRules/edit/<?= (int)$r['c0d']; ?>" class="fw-semibold text-decoration-none"><?= htmlspecialchars($r['name']); ?></a><?php if ($r['notes']): ?><div class="text-muted small"><?= htmlspecialchars($r['notes']); ?></div><?php endif; ?></td>
            <td class="small text-muted"><code><?= htmlspecialchars($r['kind']); ?></code></td>
            <td><span class="badge bg-<?= $sev; ?>"><?= htmlspecialchars($r['severity']); ?></span></td>
            <td class="small font-monospace text-muted"><?= $th ? htmlspecialchars(json_encode($th, JSON_UNESCAPED_SLASHES)) : '—'; ?></td>
            <td class="small">
              <?php if ($r['notify_inapp']):    ?><span class="badge bg-light text-dark border me-1"><i class="fas fa-desktop fa-xs"></i></span><?php endif; ?>
              <?php if ($r['notify_sms_to']):   ?><span class="badge bg-light text-dark border me-1" title="<?= htmlspecialchars($r['notify_sms_to']); ?>"><i class="fas fa-sms fa-xs"></i></span><?php endif; ?>
              <?php if ($r['notify_email_to']): ?><span class="badge bg-light text-dark border me-1" title="<?= htmlspecialchars($r['notify_email_to']); ?>"><i class="fas fa-envelope fa-xs"></i></span><?php endif; ?>
            </td>
            <td class="small text-muted"><?= (int)$r['cooldown_minutes']; ?> min</td>
            <td><?php if ($r['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; ?>AlertRules/toggle/<?= (int)$r['c0d']; ?>" class="d-inline"><?= csrf_field(); ?><button class="btn btn-sm btn-outline-secondary" title="<?= $r['active']?'Disable':'Enable'; ?>"><i class="fas fa-power-off"></i></button></form>
              <a href="<?= PROOT; ?>AlertRules/edit/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-pen"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rules'])): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">No rules configured — <a href="<?= PROOT; ?>AlertRules/add">add your first</a>.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

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