@php
    $key = 'field_'.$field->id;
    $readOnly = $readOnly ?? false;
    $prefilled = $prefilled ?? [];
    if ($field->type === 'table') {
        $value = old($key.'.rows', $prefilled[$key] ?? []);
    } elseif ($field->type !== 'group') {
        $value = old($key, $prefilled[$key] ?? null);
    } else {
        $value = null;
    }
    $checkboxGroupValues = is_array($value) ? $value : (is_string($value) ? json_decode($value, true) ?? [] : []);
@endphp

@if($field->type === 'group')
    @if($field->children->isEmpty())
        <fieldset class="mb-6 rounded-xl border border-indigo-200 bg-indigo-50/30 p-5">
            <legend class="px-3 py-1 text-sm font-semibold text-indigo-800 bg-white border border-indigo-200 rounded-md">{{ $field->label }}</legend>
            @if(!empty($field->description))
                <p class="text-sm text-slate-500 mt-2">{{ $field->description }}</p>
            @endif
            <p class="text-sm text-slate-400 italic mt-3">Aucun champ dans ce groupe.</p>
        </fieldset>
    @else
        <div class="mb-6">
            @if(!empty($field->description))
                <p class="text-sm text-slate-500 mb-3 italic">{{ $field->description }}</p>
            @endif
            @include('partials.dynamic-fields-list', [
                'fields' => $field->children,
                'readOnly' => $readOnly,
                'prefilled' => $prefilled,
                'scopeId' => 'group_'.$field->id,
            ])
        </div>
    @endif
@elseif($field->type === 'table')
    @include('partials.dynamic-table-field', ['field' => $field, 'value' => $value, 'readOnly' => $readOnly])
@else
<div class="mb-5">
    <label class="block text-sm font-medium text-slate-700 mb-1.5">
        {{ $field->label }}
        @if($field->required && ! $readOnly)<span class="text-red-500">*</span>@endif
    </label>
    @if(!empty($field->description))
        <p class="text-xs text-slate-500 mb-2 -mt-0.5">{{ $field->description }}</p>
    @endif

    @if($readOnly)
        <div class="rounded-lg bg-slate-50 border border-slate-100 px-4 py-3 text-sm text-slate-800">
            @switch($field->type)
                @case('checkbox')
                    {{ ($value === '1' || $value === true) ? 'Oui' : 'Non' }}
                    @break
                @case('checkbox_group')
                    {{ !empty($checkboxGroupValues) ? implode(', ', $checkboxGroupValues) : '—' }}
                    @break
                @case('file')
                    <x-file-preview :path="$value" />
                    @break
                @default
                    {{ $value ?: '—' }}
            @endswitch
        </div>
    @else
        @switch($field->type)
            @case('textarea')
                <textarea name="{{ $key }}" rows="4" class="input-modern" {{ $field->required ? 'required' : '' }}>{{ $value }}</textarea>
                @break
            @case('select')
                <select name="{{ $key }}" class="input-modern" {{ $field->required ? 'required' : '' }}>
                    <option value="">— Sélectionner —</option>
                    @foreach($field->options ?? [] as $option)
                        <option value="{{ $option }}" @selected($value == $option)>{{ $option }}</option>
                    @endforeach
                </select>
                @break
            @case('radio')
                <div class="flex flex-wrap gap-4 mt-1">
                    @foreach($field->options ?? [] as $option)
                        <label class="inline-flex items-center gap-2 cursor-pointer">
                            <input type="radio" name="{{ $key }}" value="{{ $option }}" class="text-transtev-600 focus:ring-transtev-500" @checked($value == $option) {{ $field->required ? 'required' : '' }}>
                            <span class="text-sm text-slate-600">{{ $option }}</span>
                        </label>
                    @endforeach
                </div>
                @break
            @case('checkbox')
                <label class="inline-flex items-center gap-2 cursor-pointer mt-1">
                    <input type="checkbox" name="{{ $key }}" value="1" class="rounded border-slate-300 text-transtev-600" @checked($value === '1' || $value === true)>
                    <span class="text-sm text-slate-600">Oui</span>
                </label>
                @break
            @case('checkbox_group')
                <div class="space-y-2 mt-1">
                    @foreach($field->options ?? [] as $option)
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="checkbox" name="{{ $key }}[]" value="{{ $option }}" class="rounded border-slate-300 text-transtev-600" @checked(in_array($option, $checkboxGroupValues))>
                            <span class="text-sm text-slate-600">{{ $option }}</span>
                        </label>
                    @endforeach
                </div>
                @break
            @case('file')
                <x-file-input :name="$key" :existing="$value" :required="$field->required" />
                @break
            @default
                <input type="{{ $field->type }}" name="{{ $key }}" value="{{ $value }}" class="input-modern" {{ $field->required ? 'required' : '' }}>
        @endswitch
    @endif

    @error($key)
        <p class="mt-1.5 text-sm text-red-600">{{ $message }}</p>
    @enderror
</div>
@endif
