<?php
use App\Services\SetupDefaults;
$fields = $draft['fields'] ?? [];
$entities = ['lead' => 'Lead', 'account' => 'Account', 'opportunity' => 'Deal', 'service' => 'Visit / service'];
$types = SetupDefaults::FIELD_TYPES;
$byEntity = [];
foreach ($fields as $f) {
    $byEntity[$f['entity'] ?? 'lead'][] = $f;
}
?>
<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="fields">
    <input type="hidden" name="action" value="update">
    <div class="set-section-head">
      <div>
        <div class="set-section-title"><i class="bi bi-input-cursor-text"></i> 4. Field builder</div>
        <p class="set-section-sub">Add boxes: text, number, date, dropdown, or money. Industry boxes can be renamed or turned off.</p>
      </div>
      <button class="btn btn-primary" type="submit">Save draft</button>
    </div>
    <?php foreach ($entities as $ent => $label): ?>
      <h2 class="sup-h"><?= e($label) ?></h2>
      <?php if (empty($byEntity[$ent])): ?>
        <p class="text-muted">No extra boxes yet.</p>
      <?php else: foreach ($byEntity[$ent] as $f):
          $id = (string) $f['id'];
          $opts = '';
          foreach ($f['options'] ?? [] as $val => $lab) {
              $opts .= $val . '=' . $lab . "\n";
          }
      ?>
        <div class="sup-field-row">
          <input type="hidden" name="fields[<?= e($id) ?>][active]" value="0">
          <label class="sup-on"><input type="checkbox" name="fields[<?= e($id) ?>][active]" value="1" <?= !empty($f['active']) ? 'checked' : '' ?>> On</label>
          <input class="form-control" name="fields[<?= e($id) ?>][label]" value="<?= e((string) $f['label']) ?>" required>
          <span class="sup-type"><?= e((string) $f['type']) ?></span>
          <code><?= e((string) $f['key']) ?></code>
          <?php if (($f['type'] ?? '') === 'select'): ?>
            <textarea class="form-control" name="fields[<?= e($id) ?>][options]" rows="2" placeholder="value=Label"><?= e(trim($opts)) ?></textarea>
          <?php endif; ?>
          <?php if (($f['source'] ?? '') === 'custom'): ?>
            <button class="btn btn-sm btn-outline-danger" type="submit" form="sup-fdel-<?= e($id) ?>">Remove</button>
          <?php else: ?>
            <span class="text-muted small">Industry</span>
          <?php endif; ?>
        </div>
      <?php endforeach; endif; ?>
    <?php endforeach; ?>
  </form>

  <?php foreach ($fields as $f):
      if (($f['source'] ?? '') !== 'custom') continue;
      $id = (string) $f['id'];
  ?>
    <form class="ajax-form d-none" id="sup-fdel-<?= e($id) ?>" action="<?= e(url('setup/save')) ?>" method="post">
      <?= csrf_field() ?><input type="hidden" name="section" value="fields"><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="fields">
    <input type="hidden" name="action" value="add">
    <div class="set-section-title mb-2">Add a box</div>
    <div class="sup-add-grid">
      <label class="sup-field"><span>Belongs on</span>
        <select class="form-select" name="entity">
          <?php foreach ($entities as $ent => $label): ?><option value="<?= e($ent) ?>"><?= e($label) ?></option><?php endforeach; ?>
        </select>
      </label>
      <label class="sup-field"><span>Label</span><input class="form-control" name="label" required maxlength="80" placeholder="Last order date"></label>
      <label class="sup-field"><span>Key</span><input class="form-control" name="key" required maxlength="40" placeholder="last_order_at"></label>
      <label class="sup-field"><span>Type</span>
        <select class="form-select" name="type">
          <?php foreach ($types as $t): ?><option value="<?= e($t) ?>"><?= e(ucfirst($t)) ?></option><?php endforeach; ?>
        </select>
      </label>
      <label class="sup-field"><span>Dropdown choices</span><input class="form-control" name="options" placeholder="prospect=Prospect, active=Active"></label>
      <div class="sup-field"><span>&nbsp;</span><button class="btn btn-primary" type="submit">Add box</button></div>
    </div>
  </form>
</div>
