<?php use App\Core\View; ?>
<header class="page-head">
  <h1><?= $row ? 'Edit' : 'New' ?> — <?= View::e($module['label']) ?></h1>
  <p><a href="<?= View::url("/admin/$moduleKey") ?>">← Back to list</a></p>
</header>

<form class="panel form" method="post"
      action="<?= View::url("/admin/$moduleKey" . ($row ? '/' . (int) $row['id'] : '') . '/save') ?>">
  <input type="hidden" name="_csrf" value="<?= $csrf ?>">

  <?php foreach ($module['fields'] as $name => $field):
      $label = $field['label'] ?? ucwords(str_replace('_', ' ', $name));
      $value = $row[$name] ?? ($field['default'] ?? '');
      $required = !empty($field['required']) ? 'required' : '';
      $readonly = !empty($field['readonly']) ? 'readonly' : '';
  ?>
  <div class="field">
    <label for="f-<?= $name ?>"><?= View::e($label) ?><?= $required ? ' *' : '' ?></label>

    <?php if ($field['type'] === 'textarea'): ?>
      <textarea id="f-<?= $name ?>" name="<?= $name ?>" rows="4" <?= $required ?>><?= View::e($value) ?></textarea>

    <?php elseif ($field['type'] === 'richtext'): ?>
      <div class="rte-toolbar" data-for="f-<?= $name ?>">
        <button type="button" data-cmd="bold"><b>B</b></button>
        <button type="button" data-cmd="italic"><i>I</i></button>
        <button type="button" data-cmd="formatBlock" data-arg="h2">H2</button>
        <button type="button" data-cmd="formatBlock" data-arg="p">¶</button>
        <button type="button" data-cmd="insertUnorderedList">• List</button>
        <button type="button" data-cmd="createLink">Link</button>
      </div>
      <div class="rte" id="rte-<?= $name ?>" contenteditable="true"><?= $value ?></div>
      <textarea id="f-<?= $name ?>" name="<?= $name ?>" hidden><?= View::e($value) ?></textarea>

    <?php elseif ($field['type'] === 'json'): ?>
      <textarea id="f-<?= $name ?>" name="<?= $name ?>" rows="<?= (int) ($field['rows'] ?? 4) ?>"
                class="mono" <?= $required ?>><?= View::e($value) ?></textarea>

    <?php elseif ($field['type'] === 'checkbox'): ?>
      <label class="switch">
        <input type="hidden" name="<?= $name ?>" value="0">
        <input type="checkbox" name="<?= $name ?>" value="1" <?= $value ? 'checked' : '' ?>>
        <span>Enabled</span>
      </label>

    <?php elseif ($field['type'] === 'select'): ?>
      <select id="f-<?= $name ?>" name="<?= $name ?>">
        <?php foreach ($field['options'] as $opt => $optLabel): ?>
        <option value="<?= View::e($opt) ?>" <?= (string) $value === (string) $opt ? 'selected' : '' ?>>
          <?= View::e($optLabel) ?>
        </option>
        <?php endforeach; ?>
      </select>

    <?php elseif ($field['type'] === 'fk'): ?>
      <select id="f-<?= $name ?>" name="<?= $name ?>">
        <option value="">—</option>
        <?php foreach ($fks[$name] ?? [] as $opt): ?>
        <option value="<?= (int) $opt['id'] ?>" <?= (string) $value === (string) $opt['id'] ? 'selected' : '' ?>>
          <?= View::e($opt['name']) ?>
        </option>
        <?php endforeach; ?>
      </select>

    <?php elseif ($field['type'] === 'image'): ?>
      <div class="image-field">
        <input type="text" id="f-<?= $name ?>" name="<?= $name ?>" value="<?= View::e($value) ?>"
               placeholder="https://… or upload →" <?= $required ?>>
        <label class="btn sm upload-btn">
          Upload<input type="file" accept="image/*" data-target="f-<?= $name ?>" hidden>
        </label>
      </div>
      <?php if ($value): ?><img class="preview" src="<?= View::e($value) ?>" alt=""><?php endif; ?>

    <?php elseif ($field['type'] === 'password'): ?>
      <input type="password" id="f-<?= $name ?>" name="<?= $name ?>" autocomplete="new-password">

    <?php elseif ($field['type'] === 'number'): ?>
      <input type="number" id="f-<?= $name ?>" name="<?= $name ?>" value="<?= View::e($value) ?>"
             step="<?= View::e($field['step'] ?? '1') ?>" <?= $required ?>>

    <?php elseif ($field['type'] === 'date'): ?>
      <input type="date" id="f-<?= $name ?>" name="<?= $name ?>" value="<?= View::e($value) ?>" <?= $required ?>>

    <?php elseif ($field['type'] === 'datetime'): ?>
      <input type="datetime-local" id="f-<?= $name ?>" name="<?= $name ?>"
             value="<?= View::e($value ? str_replace(' ', 'T', substr((string) $value, 0, 16)) : '') ?>" <?= $required ?>>

    <?php else: ?>
      <input type="text" id="f-<?= $name ?>" name="<?= $name ?>" value="<?= View::e($value) ?>" <?= $required ?> <?= $readonly ?>>
    <?php endif; ?>

    <?php if (!empty($field['help'])): ?><small><?= View::e($field['help']) ?></small><?php endif; ?>
  </div>
  <?php endforeach; ?>

  <div class="field">
    <button class="btn primary" type="submit">Save</button>
  </div>
</form>
<script>window.__CSRF = <?= json_encode($csrf) ?>;</script>
