<?php
$fields = $fields ?? [];
$values = $values ?? [];
$prefix = $prefix ?? 'attrs';
$form = $form ?? '';
if (!$fields) {
    return;
}
if ($form !== '') {
    $fields = array_values(array_filter($fields, static fn($f) => show_field($form, (string) ($f['key'] ?? ''))));
    if (!$fields) {
        return;
    }
}
?>
<div class="row g-2 mt-1">
<?php foreach ($fields as $f):
    $key = $f['key'];
    $val = $values[$key] ?? '';
    $name = $prefix . '[' . $key . ']';
?>
  <div class="col-md-4">
    <label class="form-label"><?= e($f['label']) ?></label>
    <?php if ($f['type'] === 'select'): ?>
      <select class="form-select" name="<?= e($name) ?>">
        <option value="">—</option>
        <?php foreach ($f['options'] as $optVal => $optLabel): ?>
          <option value="<?= e((string)$optVal) ?>" <?= (string)$val === (string)$optVal ? 'selected' : '' ?>><?= e((string)$optLabel) ?></option>
        <?php endforeach; ?>
      </select>
    <?php elseif ($f['type'] === 'date'): ?>
      <input class="form-control" type="date" name="<?= e($name) ?>" value="<?= e((string)$val) ?>">
    <?php elseif ($f['type'] === 'money'): ?>
      <input class="form-control" type="number" min="0" step="0.01" name="<?= e($name) ?>" value="<?= e((string)$val) ?>">
    <?php elseif ($f['type'] === 'number'): ?>
      <input class="form-control" type="number" step="1" name="<?= e($name) ?>" value="<?= e((string)$val) ?>">
    <?php elseif ($f['type'] === 'textarea'): ?>
      <textarea class="form-control" name="<?= e($name) ?>" rows="2"><?= e((string)$val) ?></textarea>
    <?php else: ?>
      <input class="form-control" name="<?= e($name) ?>" value="<?= e((string)$val) ?>">
    <?php endif; ?>
  </div>
<?php endforeach; ?>
</div>
