@props([
    'definitions',
    'values' => [],
])

@if ($definitions !== null && $definitions->count() > 0)
    <div class="row g-3">
        @foreach ($definitions as $field)
            @php
                $name = 'custom_fields['.$field->slug.']';
                $current = old('custom_fields.'.$field->slug, $values[$field->slug] ?? null);
                $t = $field->field_type;
            @endphp
            <div class="col-12">
                @if ($t === 'textarea')
                    <x-ui.form.field :name="'custom_fields.'.$field->slug" :label="$field->name" :required="$field->is_required" :id="'cf_'.$field->slug">
                        <textarea id="cf_{{ $field->slug }}" name="{{ $name }}" rows="3"
                            class="form-control @error('custom_fields.'.$field->slug) is-invalid @enderror"
                            @if ($field->is_required) required @endif>{{ $current }}</textarea>
                    </x-ui.form.field>
                @elseif ($t === 'number')
                    <x-ui.form.input type="number" step="any" :name="$name" :label="$field->name" :value="$current"
                        :placeholder="$field->placeholder" :required="$field->is_required" />
                @elseif ($t === 'email')
                    <x-ui.form.input type="email" :name="$name" :label="$field->name" :value="$current"
                        :placeholder="$field->placeholder" :required="$field->is_required" />
                @elseif ($t === 'date')
                    <x-ui.form.input type="date" :name="$name" :label="$field->name" :value="$current"
                        :required="$field->is_required" />
                @elseif ($t === 'url')
                    <x-ui.form.input type="url" :name="$name" :label="$field->name" :value="$current"
                        :placeholder="$field->placeholder" :required="$field->is_required" />
                @elseif ($t === 'select')
                    @php
                        $opts = $field->options->pluck('label', 'value')->all();
                    @endphp
                    <x-ui.form.select :name="$name" :label="$field->name" :options="$opts" :selected="$current"
                        :placeholder="$field->placeholder" :required="$field->is_required" />
                @elseif ($t === 'radio')
                    <div class="mb-2">
                        <label class="form-label">{{ $field->name }}@if ($field->is_required)<span class="text-danger">*</span>@endif</label>
                        @foreach ($field->options as $opt)
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="{{ $name }}" id="cf_{{ $field->slug }}_{{ $loop->index }}"
                                    value="{{ $opt->value }}" @checked((string) $current === (string) $opt->value)
                                    @if ($field->is_required && $loop->first) required @endif>
                                <label class="form-check-label" for="cf_{{ $field->slug }}_{{ $loop->index }}">{{ $opt->label }}</label>
                            </div>
                        @endforeach
                        @error('custom_fields.'.$field->slug)
                            <div class="invalid-feedback d-block">{{ $message }}</div>
                        @enderror
                    </div>
                @elseif ($t === 'checkbox')
                    <x-ui.form.select :name="$name" :label="$field->name"
                        :options="[0 => __('common.no'), 1 => __('common.yes')]"
                        :selected="old('custom_fields.'.$field->slug, $current === '1' || $current === 1 || $current === true ? 1 : 0)"
                        :required="$field->is_required" />
                @elseif ($t === 'file')
                    <x-ui.form.field :name="'custom_fields.'.$field->slug" :label="$field->name" :required="false" :id="'cff_'.$field->slug">
                        @if (! empty($current))
                            <p class="text-muted small">{{ __('custom_fields.file_stored') }}</p>
                        @endif
                        <input type="file" id="cff_{{ $field->slug }}" name="{{ $name }}"
                            class="form-control @error('custom_fields.'.$field->slug) is-invalid @enderror">
                    </x-ui.form.field>
                @else
                    <x-ui.form.input :name="$name" :label="$field->name" :value="$current"
                        :placeholder="$field->placeholder" :required="$field->is_required" />
                @endif
                @if ($field->help_text)
                    <div class="form-text">{{ $field->help_text }}</div>
                @endif
            </div>
        @endforeach
    </div>
@endif
