<?php
// $field = [anahtar, etiket, tip, seçenekler/alt alanlar, açıklama]
// $name = formdaki ad (örn. s[title]), $path = dosya alanı yolu (örn. items__3__image)
[$key, $label, $type] = $field;
$options = $field[3] ?? [];
$help = $field[4] ?? '';
$id = 'f_' . preg_replace('/[^a-z0-9]+/i', '_', $name);
$wide = in_array($type, ['textarea', 'code', 'items', 'image', 'categories', 'products', 'choice', 'range', 'font'], true);
?>
<div class="<?= $wide ? 'sm:col-span-2' : '' ?>" data-field="<?= e($key) ?>">
    <?php if ($type === 'toggle'): ?>
        <label class="flex cursor-pointer items-center justify-between gap-3 rounded-xl border border-zinc-200 bg-white px-4 py-3 text-sm font-medium">
            <span><?= e($label) ?><?php if ($help): ?><span class="block text-xs font-normal text-zinc-500"><?= e($help) ?></span><?php endif ?></span>
            <input type="hidden" name="<?= e($name) ?>" value="0">
            <input type="checkbox" name="<?= e($name) ?>" value="1" class="peer sr-only" <?= (string) $value === '1' ? 'checked' : '' ?>>
            <span class="relative h-6 w-11 shrink-0 rounded-full bg-zinc-200 transition after:absolute after:top-0.5 after:left-0.5 after:size-5 after:rounded-full after:bg-white after:shadow after:transition peer-checked:bg-ink peer-checked:after:translate-x-5"></span>
        </label>
    <?php else: ?>
        <?php if ($type !== 'items'): ?>
            <label for="<?= $id ?>" class="label"><?= e($label) ?></label>
        <?php endif ?>

        <?php if ($type === 'choice'): ?>
            <?php // Seçenekler: değer => [etiket, küçük çizim HTML'i] ?>
            <div class="grid gap-2 <?= count($options) > 3 ? 'grid-cols-2' : 'grid-cols-' . max(1, count($options)) ?>">
                <?php foreach ($options as $option => [$text, $art]): ?>
                    <label class="group cursor-pointer">
                        <input type="radio" name="<?= e($name) ?>" value="<?= e($option) ?>" class="peer sr-only" <?= (string) $value === (string) $option ? 'checked' : '' ?>>
                        <span class="block rounded-xl border-2 border-zinc-200 bg-white p-2 transition peer-checked:border-ink hover:border-zinc-300">
                            <?php if ($art): ?><span class="flex h-14 items-center justify-center overflow-hidden rounded-lg bg-zinc-50 text-zinc-400 group-has-checked:text-ink"><?= $art ?></span><?php endif ?>
                            <span class="mt-1.5 block text-center text-xs font-medium text-zinc-600"><?= e($text) ?></span>
                        </span>
                    </label>
                <?php endforeach ?>
            </div>
        <?php elseif ($type === 'range'): ?>
            <?php [$min, $max, $step, $unit] = $options + [0, 100, 1, ''] ?>
            <div class="flex items-center gap-3">
                <input id="<?= $id ?>" type="range" name="<?= e($name) ?>" value="<?= e($value) ?>" min="<?= $min ?>" max="<?= $max ?>" step="<?= $step ?>" data-range-output="<?= $id ?>_out" class="h-1.5 flex-1 cursor-pointer accent-ink">
                <span class="w-14 shrink-0 rounded-lg bg-zinc-100 py-1 text-center text-xs font-semibold"><span id="<?= $id ?>_out"><?= e($value) ?></span><?= e($unit) ?></span>
            </div>
        <?php elseif ($type === 'font'): ?>
            <div class="grid gap-1.5">
                <?php foreach ($options as $font): ?>
                    <label class="cursor-pointer">
                        <input type="radio" name="<?= e($name) ?>" value="<?= e($font) ?>" class="peer sr-only" <?= $value === $font ? 'checked' : '' ?>>
                        <span class="flex items-center justify-between rounded-xl border border-zinc-200 bg-white px-3.5 py-2.5 transition peer-checked:border-ink peer-checked:ring-1 peer-checked:ring-ink hover:border-zinc-300">
                            <span class="text-lg leading-none text-ink" style="font-family: '<?= e($font) ?>'">Aa Çğ</span>
                            <span class="text-xs font-medium text-zinc-500"><?= e($font) ?></span>
                        </span>
                    </label>
                <?php endforeach ?>
            </div>
        <?php elseif ($type === 'textarea'): ?>
            <textarea id="<?= $id ?>" name="<?= e($name) ?>" rows="3" class="input"><?= e($value) ?></textarea>
        <?php elseif ($type === 'code'): ?>
            <textarea id="<?= $id ?>" name="<?= e($name) ?>" rows="10" spellcheck="false" class="input font-mono text-xs leading-5"><?= e($value) ?></textarea>
        <?php elseif ($type === 'number'): ?>
            <input id="<?= $id ?>" type="number" min="0" name="<?= e($name) ?>" value="<?= e($value) ?>" class="input">
        <?php elseif ($type === 'datetime'): ?>
            <input id="<?= $id ?>" type="datetime-local" name="<?= e($name) ?>" value="<?= e($value) ?>" class="input">
        <?php elseif ($type === 'select'): ?>
            <select id="<?= $id ?>" name="<?= e($name) ?>" class="input">
                <?php foreach ($options as $option => $text): ?>
                    <option value="<?= e($option) ?>" <?= (string) $value === (string) $option ? 'selected' : '' ?>><?= e($text) ?></option>
                <?php endforeach ?>
            </select>
        <?php elseif ($type === 'icon'): ?>
            <select id="<?= $id ?>" name="<?= e($name) ?>" class="input">
                <?php foreach (['truck' => 'Kargo', 'shield' => 'Güvenlik', 'refresh' => 'İade', 'chat' => 'Destek', 'lock' => 'Kilit', 'tag' => 'Etiket', 'heart' => 'Kalp', 'check-circle' => 'Onay', 'clock' => 'Saat', 'banknote' => 'Para', 'phone' => 'Telefon', 'mail' => 'E-posta', 'map' => 'Konum', 'bag' => 'Çanta', 'cube' => 'Kutu', 'users' => 'Kişiler'] as $icon_name => $text): ?>
                    <option value="<?= $icon_name ?>" <?= $value === $icon_name ? 'selected' : '' ?>><?= $text ?></option>
                <?php endforeach ?>
            </select>
        <?php elseif ($type === 'color'): ?>
            <div class="flex items-center gap-2">
                <input type="color" value="<?= e(hex_color($value, '#ffffff')) ?>" data-color-for="<?= $id ?>" class="h-11 w-14 shrink-0 cursor-pointer rounded-xl border border-zinc-200 bg-white p-1">
                <input id="<?= $id ?>" name="<?= e($name) ?>" value="<?= e($value) ?>" placeholder="Boş = varsayılan" maxlength="7" class="input font-mono uppercase" data-color-text>
            </div>
        <?php elseif ($type === 'image'): ?>
            <div class="flex items-center gap-4 rounded-xl border border-dashed border-zinc-300 bg-white p-3" data-image-field>
                <input type="hidden" name="<?= e($name) ?>" value="<?= e($value) ?>" data-image-value>
                <div data-image-preview class="flex size-20 shrink-0 items-center justify-center overflow-hidden rounded-lg bg-zinc-100 text-zinc-300">
                    <?php if ($value): ?><img src="<?= e(upload_url($value)) ?>" alt="" class="h-full w-full object-cover"><?php else: ?><?= icon('photo', 'size-7') ?><?php endif ?>
                </div>
                <div class="min-w-0 flex-1 space-y-2">
                    <input id="<?= $id ?>" type="file" name="file__<?= e($path) ?>" accept="image/*" class="block w-full text-sm text-zinc-600 file:mr-3 file:rounded-lg file:border-0 file:bg-ink file:px-3 file:py-1.5 file:text-xs file:font-semibold file:text-white">
                    <button type="button" data-image-clear class="text-xs font-medium text-rose-600 hover:underline <?= $value ? '' : 'hidden' ?>">Görseli kaldır</button>
                </div>
            </div>
        <?php elseif ($type === 'category'): ?>
            <select id="<?= $id ?>" name="<?= e($name) ?>" class="input">
                <option value="">Seçin</option>
                <?php foreach (category_tree() as $category): ?>
                    <option value="<?= $category['id'] ?>" <?= (string) $value === (string) $category['id'] ? 'selected' : '' ?>><?= str_repeat('— ', $category['depth']) . e($category['name']) ?></option>
                <?php endforeach ?>
            </select>
        <?php elseif ($type === 'categories'): ?>
            <div class="flex flex-wrap gap-2 rounded-xl border border-zinc-200 bg-white p-3">
                <?php foreach (category_tree() as $category): ?>
                    <label class="cursor-pointer">
                        <input type="checkbox" name="<?= e($name) ?>[]" value="<?= $category['id'] ?>" class="peer sr-only" <?= in_array((int) $category['id'], array_map('intval', (array) $value), true) ? 'checked' : '' ?>>
                        <span class="inline-flex rounded-full border border-zinc-200 px-3 py-1.5 text-xs font-medium text-zinc-600 transition peer-checked:border-ink peer-checked:bg-ink peer-checked:text-white"><?= str_repeat('· ', $category['depth']) . e($category['name']) ?></span>
                    </label>
                <?php endforeach ?>
            </div>
        <?php elseif ($type === 'products'): ?>
            <?php $selected = array_map('intval', (array) $value) ?>
            <div class="rounded-xl border border-zinc-200 bg-white p-3" data-product-picker>
                <input type="search" placeholder="Ürün ara..." data-picker-search class="input mb-3 py-2">
                <div class="grid max-h-64 gap-1 overflow-y-auto sm:grid-cols-2">
                    <?php foreach (db_all('SELECT id, name, price, image FROM products ORDER BY name LIMIT 1000') as $product): ?>
                        <label data-picker-item="<?= e(mb_strtolower($product['name'])) ?>" class="flex cursor-pointer items-center gap-3 rounded-lg px-2 py-1.5 text-sm hover:bg-zinc-50 has-checked:bg-zinc-100">
                            <input type="checkbox" name="<?= e($name) ?>[]" value="<?= $product['id'] ?>" class="size-4 rounded accent-ink" <?= in_array((int) $product['id'], $selected, true) ? 'checked' : '' ?>>
                            <span class="size-8 shrink-0 overflow-hidden rounded bg-zinc-100"><?= product_image($product, 'h-full w-full object-cover', 'text-xs') ?></span>
                            <span class="truncate"><?= e($product['name']) ?></span>
                        </label>
                    <?php endforeach ?>
                </div>
            </div>
        <?php elseif ($type === 'items'): ?>
            <?php
            $rows = (array) $value;
            $sub_fields = $options;
            ?>
            <div data-repeater class="rounded-2xl border border-zinc-200 bg-zinc-50/60 p-3">
                <div class="flex items-center justify-between px-2 pt-1 pb-3">
                    <p class="text-sm font-semibold"><?= e($label) ?> <span class="font-normal text-zinc-400">(<span data-repeater-count><?= count($rows) ?></span>)</span></p>
                    <button type="button" data-repeater-add class="btn-light btn-sm"><?= icon('plus', 'size-4', '2') ?>Ekle</button>
                </div>
                <div data-repeater-list class="space-y-3">
                    <?php foreach ($rows as $index => $row): ?>
                        <?php admin_repeater_row($sub_fields, $row, $name, $path, $index) ?>
                    <?php endforeach ?>
                </div>
                <template data-repeater-template>
                    <?php admin_repeater_row($sub_fields, [], $name, $path, '%i%') ?>
                </template>
            </div>
        <?php else: ?>
            <input id="<?= $id ?>" type="<?= $type === 'url' ? 'text' : 'text' ?>" name="<?= e($name) ?>" value="<?= e($value) ?>" class="input" <?= $type === 'url' ? 'placeholder="/urunler ya da https://..."' : '' ?>>
        <?php endif ?>

        <?php if ($help && $type !== 'items'): ?>
            <p class="mt-1.5 text-xs text-zinc-500"><?= e($help) ?></p>
        <?php endif ?>
    <?php endif ?>
</div>
