<?php $this->start('body'); ?>
<?php $f = $this->data['form']; $isNew = $this->data['is_new']; $r = $this->data['rule']; ?>
<?php $action = $isNew ? PROOT . 'AlertRules/save' : PROOT . 'AlertRules/update/' . (int)$r['c0d']; ?>

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-<?= $isNew?'plus':'pen'; ?> me-2 text-muted"></i><?= $isNew ? 'New Alert Rule' : 'Edit · ' . htmlspecialchars($r['name']); ?></h5>
  <a href="<?= PROOT; ?>AlertRules" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= $this->raw('fb_msg'); ?>

<form method="POST" action="<?= $action; ?>" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-6"><label class="form-label">Rule name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" required value="<?= htmlspecialchars($f['name'] ?? ''); ?>" placeholder="e.g. Over-speed &gt; 80 kph"></div>
      <div class="col-md-3"><label class="form-label">Kind <span class="text-danger">*</span></label>
        <select name="kind" class="form-select" required>
          <?php foreach (['over_speed'=>'Over-speed','fuel_drop'=>'Fuel drop','ignition_after_hours'=>'Ignition after hours','device_offline'=>'Device offline','geofence_enter'=>'Geofence enter','geofence_exit'=>'Geofence exit','idle_too_long'=>'Idle too long'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= ($f['kind'] ?? '')===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Severity</label>
        <select name="severity" class="form-select">
          <?php foreach (['info','low','medium','high','critical'] as $s): ?>
            <option value="<?= $s; ?>" <?= ($f['severity'] ?? 'medium')===$s?'selected':''; ?>><?= ucfirst($s); ?></option>
          <?php endforeach; ?>
        </select></div>

      <div class="col-12">
        <label class="form-label">Threshold JSON</label>
        <input type="text" name="threshold_json" class="form-control font-monospace" value='<?= htmlspecialchars($f['threshold_json'] ?? ''); ?>' placeholder='{"speed_kph":80} · {"drop_l":20,"within_min":10} · {"hour_from":20,"hour_to":5} · {"offline_minutes":30}'>
        <div class="form-text small">Kind-specific config. See docblock in AlertRules.php for the shape per kind.</div>
      </div>

      <div class="col-md-6"><label class="form-label">Scope · truck ids (JSON array, blank = all)</label>
        <input type="text" name="scope_truck_ids" class="form-control font-monospace" value='<?= htmlspecialchars($f['scope_truck_ids'] ?? ''); ?>' placeholder='[]  or  [12,34]'></div>
      <div class="col-md-6"><label class="form-label">Scope · geofence ids (JSON array, blank = all)</label>
        <input type="text" name="scope_geofence_ids" class="form-control font-monospace" value='<?= htmlspecialchars($f['scope_geofence_ids'] ?? ''); ?>' placeholder='[]  or  [5,7]'></div>

      <div class="col-md-6"><label class="form-label">Notify SMS to (CSV of MSISDNs)</label>
        <input type="text" name="notify_sms_to" class="form-control" value="<?= htmlspecialchars($f['notify_sms_to'] ?? ''); ?>" placeholder="+255712345678, +255789012345"></div>
      <div class="col-md-6"><label class="form-label">Notify email to (CSV)</label>
        <input type="text" name="notify_email_to" class="form-control" value="<?= htmlspecialchars($f['notify_email_to'] ?? ''); ?>" placeholder="fleet@pacificgroup.co.tz, ops@..."></div>

      <div class="col-md-3"><label class="form-label">Cooldown (min)</label>
        <input type="text" name="cooldown_minutes" class="form-control js-int" value="<?= htmlspecialchars((string)($f['cooldown_minutes'] ?? 15)); ?>"></div>
      <div class="col-md-3 d-flex align-items-end">
        <div class="form-check">
          <input type="checkbox" name="notify_inapp" value="1" class="form-check-input" id="chkIa" <?= (!isset($f['notify_inapp']) || !empty($f['notify_inapp']))?'checked':''; ?>>
          <label class="form-check-label" for="chkIa">In-app dashboard alert</label>
        </div></div>
      <div class="col-md-3 d-flex align-items-end">
        <div class="form-check">
          <input type="checkbox" name="active" value="1" class="form-check-input" id="chkAc" <?= (!isset($f['active']) || !empty($f['active']))?'checked':''; ?>>
          <label class="form-check-label" for="chkAc">Active</label>
        </div></div>

      <div class="col-12"><label class="form-label">Notes</label>
        <textarea name="notes" rows="2" class="form-control"><?= htmlspecialchars($f['notes'] ?? ''); ?></textarea></div>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>AlertRules" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i><?= $isNew?'Create rule':'Save changes'; ?></button>
  </div>
</form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>document.querySelectorAll('.js-int').forEach(el => new Cleave(el, { numeral: true, numeralThousandsGroupStyle: 'thousand', numeralDecimalScale: 0 }));</script>
<?php $this->end(); ?>
