<?php
$stages = $draft['stages'] ?? [];
$reqChoices = [
    'amount' => 'Amount',
    'next_step' => 'Next step',
    'next_step_at' => 'Next step date',
    'close_date' => 'Close date',
];
foreach ($draft['fields'] ?? [] as $f) {
    if (($f['entity'] ?? '') === 'opportunity' && !empty($f['active'])) {
        $reqChoices[$f['key']] = $f['label'];
    }
}
?>
<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="stages">
    <input type="hidden" name="action" value="update">
    <div class="set-section-head">
      <div>
        <div class="set-section-title"><i class="bi bi-kanban"></i> 3. Pipeline builder</div>
        <p class="set-section-sub">Add or rename stages. Say which boxes must be filled before a deal can enter that stage.</p>
      </div>
      <button class="btn btn-primary" type="submit">Save draft</button>
    </div>
    <div class="sup-stage-list">
      <?php foreach ($stages as $s):
          $id = (string) ($s['id'] ?? '');
          $flag = ((int) ($s['is_lost'] ?? 0) === 1) ? 'lost' : (((int) ($s['is_won'] ?? 0) === 1) ? 'won' : 'open');
          $req = $s['required_fields'] ?? [];
      ?>
        <article class="sup-stage">
          <div class="sup-stage-top">
            <input class="form-control" name="stages[<?= e($id) ?>][name]" value="<?= e((string) $s['name']) ?>" required maxlength="80">
            <input class="form-control sup-prob" type="number" min="0" max="100" name="stages[<?= e($id) ?>][probability]" value="<?= (int) $s['probability'] ?>" title="Chance to win">
            <select class="form-select" name="stages[<?= e($id) ?>][flag]">
              <option value="open" <?= $flag === 'open' ? 'selected' : '' ?>>Open</option>
              <option value="won" <?= $flag === 'won' ? 'selected' : '' ?>>Won</option>
              <option value="lost" <?= $flag === 'lost' ? 'selected' : '' ?>>Lost</option>
            </select>
            <div class="sup-stage-move">
              <button class="btn btn-sm btn-outline-secondary" type="submit" form="sup-move-<?= e($id) ?>-up" title="Move up"><i class="bi bi-chevron-up"></i></button>
              <button class="btn btn-sm btn-outline-secondary" type="submit" form="sup-move-<?= e($id) ?>-dn" title="Move down"><i class="bi bi-chevron-down"></i></button>
              <button class="btn btn-sm btn-outline-danger" type="submit" form="sup-del-<?= e($id) ?>" title="Remove"><i class="bi bi-trash"></i></button>
            </div>
          </div>
          <div class="sup-req">
            <span>Required to enter this stage</span>
            <div class="sup-checks">
              <?php foreach ($reqChoices as $key => $label): ?>
                <label><input type="checkbox" name="stages[<?= e($id) ?>][required_fields][]" value="<?= e($key) ?>" <?= in_array($key, $req, true) ? 'checked' : '' ?>> <?= e($label) ?></label>
              <?php endforeach; ?>
            </div>
          </div>
        </article>
      <?php endforeach; ?>
    </div>
  </form>

  <?php foreach ($stages as $s):
      $id = (string) ($s['id'] ?? '');
  ?>
    <form class="ajax-form d-none" id="sup-move-<?= e($id) ?>-up" action="<?= e(url('setup/save')) ?>" method="post">
      <?= csrf_field() ?><input type="hidden" name="section" value="stages"><input type="hidden" name="action" value="move"><input type="hidden" name="id" value="<?= e($id) ?>"><input type="hidden" name="dir" value="up">
    </form>
    <form class="ajax-form d-none" id="sup-move-<?= e($id) ?>-dn" action="<?= e(url('setup/save')) ?>" method="post">
      <?= csrf_field() ?><input type="hidden" name="section" value="stages"><input type="hidden" name="action" value="move"><input type="hidden" name="id" value="<?= e($id) ?>"><input type="hidden" name="dir" value="down">
    </form>
    <form class="ajax-form d-none" id="sup-del-<?= e($id) ?>" action="<?= e(url('setup/save')) ?>" method="post">
      <?= csrf_field() ?><input type="hidden" name="section" value="stages"><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="stages">
    <input type="hidden" name="action" value="add">
    <div class="set-section-title mb-2">Add a stage</div>
    <div class="sup-add-row">
      <input class="form-control" name="name" required maxlength="80" placeholder="Stage name">
      <input class="form-control sup-prob" type="number" min="0" max="100" name="probability" value="25">
      <button class="btn btn-primary" type="submit">Add</button>
    </div>
  </form>
</div>
