<?php
use App\Services\SetupDefaults;
$rules = $draft['automations'] ?? [];
?>
<div class="sup-page">
  <?php \App\Core\View::partial('setup_nav', compact('section', 'dirty', 'canUndo', 'publishedAt')); ?>
  <form class="set-section ajax-form mb-3" action="<?= e(url('setup/save')) ?>" method="post">
    <?= csrf_field() ?>
    <input type="hidden" name="section" value="alerts">
    <input type="hidden" name="action" value="update">
    <div class="set-section-head">
      <div>
        <div class="set-section-title"><i class="bi bi-lightning-charge"></i> 6. Automation builder</div>
        <p class="set-section-sub">If this happens, then do that — create a task, send a reminder, or assign a person.</p>
      </div>
      <button class="btn btn-primary" type="submit">Save draft</button>
    </div>
    <?php if (!$rules): ?>
      <p class="text-muted">No rules yet.</p>
    <?php endif; ?>
    <?php foreach ($rules as $r):
        $id = (string) $r['id'];
    ?>
      <div class="sup-rule">
        <input type="hidden" name="rules[<?= e($id) ?>][active]" value="0">
        <label class="sup-on"><input type="checkbox" name="rules[<?= e($id) ?>][active]" value="1" <?= !empty($r['active']) ? 'checked' : '' ?>> On</label>
        <select class="form-select" name="rules[<?= e($id) ?>][trigger]">
          <?php foreach (SetupDefaults::TRIGGERS as $k => $lab): ?>
            <option value="<?= e($k) ?>" <?= ($r['trigger'] ?? '') === $k ? 'selected' : '' ?>><?= e($lab) ?></option>
          <?php endforeach; ?>
        </select>
        <select class="form-select" name="rules[<?= e($id) ?>][action]">
          <?php foreach (SetupDefaults::ACTIONS as $k => $lab): ?>
            <option value="<?= e($k) ?>" <?= ($r['action'] ?? '') === $k ? 'selected' : '' ?>><?= e($lab) ?></option>
          <?php endforeach; ?>
        </select>
        <input class="form-control" name="rules[<?= e($id) ?>][title]" value="<?= e((string) $r['title']) ?>" maxlength="80">
        <input class="form-control sup-prob" type="number" min="0" max="90" name="rules[<?= e($id) ?>][due_days]" value="<?= (int) ($r['due_days'] ?? 1) ?>" title="Days until the task is due">
        <button class="btn btn-sm btn-outline-danger" type="submit" form="sup-adel-<?= e($id) ?>">Remove</button>
      </div>
    <?php endforeach; ?>
  </form>
  <?php foreach ($rules as $r): $id = (string) $r['id']; ?>
    <form class="ajax-form d-none" id="sup-adel-<?= e($id) ?>" action="<?= e(url('setup/save')) ?>" method="post">
      <?= csrf_field() ?><input type="hidden" name="section" value="alerts"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= e($id) ?>">
    </form>
  <?php endforeach; ?>

  <form class="set-section ajax-form" action="<?= e(url('setup/save')) ?>" method="post">
    <?= csrf_field() ?>
    <input type="hidden" name="section" value="alerts">
    <input type="hidden" name="action" value="add">
    <div class="set-section-title mb-2">Add a rule</div>
    <div class="sup-add-grid">
      <label class="sup-field"><span>When</span>
        <select class="form-select" name="trigger">
          <?php foreach (SetupDefaults::TRIGGERS as $k => $lab): ?><option value="<?= e($k) ?>"><?= e($lab) ?></option><?php endforeach; ?>
        </select>
      </label>
      <label class="sup-field"><span>Then</span>
        <select class="form-select" name="action_type">
          <?php foreach (SetupDefaults::ACTIONS as $k => $lab): ?><option value="<?= e($k) ?>"><?= e($lab) ?></option><?php endforeach; ?>
        </select>
      </label>
      <label class="sup-field"><span>Title</span><input class="form-control" name="title" value="Follow up" maxlength="80"></label>
      <label class="sup-field"><span>Due in days</span><input class="form-control" type="number" min="0" max="90" name="due_days" value="1"></label>
      <div class="sup-field"><span>&nbsp;</span><button class="btn btn-primary" type="submit">Add rule</button></div>
    </div>
  </form>
</div>
