<?php $isEdit = !empty($row); ?>
<form method="post" action="<?= $isEdit ? url('/master/'.$key.'/'.(int)$row['id'].'/ubah') : url('/master/'.$key.'/tambah') ?>" data-loading>
<?= csrf() ?>
<div class="row g-3 justify-content-center">
  <div class="col-lg-8">
    <div class="card">
      <div class="card-header"><i class="bi bi-<?= e($entity['icon']) ?> me-1"></i><?= e($judul) ?></div>
      <div class="card-body row g-3">
        <?php foreach ($entity['fields'] as $fk => $f):
          $type = $f['type'] ?? 'text';
          $val = $isEdit ? ($row[$fk] ?? null) : old($fk);
          $wide = in_array($type, ['textarea'], true);
          $col = $wide ? 'col-12' : 'col-md-6'; ?>
          <div class="<?= $col ?>">
            <label class="form-label <?= !empty($f['required']) ? 'required' : '' ?>" for="f_<?= e($fk) ?>"><?= e($f['label']) ?></label>
            <?php if ($type === 'bool'): ?>
              <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" value="1"
                       <?= ($isEdit ? (int)($row[$fk] ?? 0) === 1 : true) ? 'checked' : '' ?>>
                <label class="form-check-label small" for="f_<?= e($fk) ?>">Aktif</label></div>
            <?php elseif ($type === 'textarea'): ?>
              <textarea class="form-control" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" rows="2" maxlength="2000"><?= e($val) ?></textarea>
            <?php elseif ($type === 'enum'): ?>
              <select class="form-select" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" <?= !empty($f['required']) ? 'required' : '' ?>>
                <?php if (empty($f['required'])): ?><option value="">—</option><?php endif; ?>
                <?php foreach ($f['options'] as $ok => $ov): ?>
                  <option value="<?= attr($ok) ?>" <?= (string)$val === (string)$ok ? 'selected' : '' ?>><?= e($ov) ?></option>
                <?php endforeach; ?></select>
            <?php elseif ($type === 'ref'): ?>
              <select class="form-select" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" <?= !empty($f['required']) ? 'required' : '' ?>>
                <option value="">—</option>
                <?php foreach (($refs[$fk] ?? []) as $rid => $rname): ?>
                  <option value="<?= (int)$rid ?>" <?= (int)$val === (int)$rid ? 'selected' : '' ?>><?= e($rname) ?></option>
                <?php endforeach; ?></select>
            <?php elseif ($type === 'money'): ?>
              <div class="input-group"><span class="input-group-text">Rp</span>
                <input type="text" class="form-control text-end" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" data-uang
                       value="<?= attr($val !== null && $val !== '' ? nf($val,0) : '') ?>"></div>
            <?php elseif ($type === 'int'): ?>
              <input type="number" class="form-control" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" value="<?= attr($val) ?>" <?= !empty($f['required']) ? 'required' : '' ?>>
            <?php elseif ($type === 'decimal'): ?>
              <input type="number" step="0.0001" class="form-control" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" value="<?= attr($val) ?>">
            <?php elseif ($type === 'email'): ?>
              <input type="email" class="form-control" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" value="<?= attr($val) ?>" maxlength="150">
            <?php else: ?>
              <input type="text" class="form-control" id="f_<?= e($fk) ?>" name="<?= e($fk) ?>" value="<?= attr($val) ?>"
                     maxlength="200" <?= !empty($f['required']) ? 'required' : '' ?>>
            <?php endif; ?>
            <?php if (!empty($f['help'])): ?><div class="form-text"><?= e($f['help']) ?></div><?php endif; ?>
          </div>
        <?php endforeach; ?>
      </div>
      <div class="card-footer d-flex gap-2">
        <button class="btn btn-primary" type="submit"><i class="bi bi-check-lg me-1"></i>Simpan</button>
        <a class="btn btn-light" href="<?= url('/master/'.$key) ?>">Batal</a>
      </div>
    </div>
  </div>
</div>
</form>
