@php
    $activeFields = \App\Models\PatientCustomField::where('active', true)->orderBy('order')->get();
@endphp

@if(count($activeFields) > 0)
<div>
    <div class="mb-4">
        <h5 class="text-primary">{{ __('patient-custom-fields.additional_information') }}</h5>
        <p class="text-muted">{{ __('patient-custom-fields.form_description') }}</p>
    </div>
        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        @endif
        
        <form action="{{ route('patient.custom-fields.store', $model->id) }}" method="POST">
            @csrf
            <div class="row">
                @foreach($activeFields as $field)
                    @php
                        $fieldValue = $model->getCustomFieldValue($field->id);
                        $value = $fieldValue ? $fieldValue->value : '';
                    @endphp
                    
                    <div class="col-md-6 mb-3">
                        <div class="form-group">
                            <label for="field_{{ $field->id }}" class="form-label">
                                {{ $field->name }} {!! $field->required ? '<span class="text-danger">*</span>' : '' !!}
                            </label>
                            
                            @switch($field->type)
                                @case('text')
                                    <input type="text" 
                                        class="form-control @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}" 
                                        value="{{ old('field_'.$field->id, $value) }}"
                                        placeholder="{{ $field->placeholder }}"
                                        {{ $field->required ? 'required' : '' }}>
                                    @break
                                    
                                @case('email')
                                    <input type="email" 
                                        class="form-control @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}" 
                                        value="{{ old('field_'.$field->id, $value) }}"
                                        placeholder="{{ $field->placeholder }}"
                                        {{ $field->required ? 'required' : '' }}>
                                    @break
                                    
                                @case('phone')
                                    <input type="tel" 
                                        class="form-control @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}" 
                                        value="{{ old('field_'.$field->id, $value) }}"
                                        placeholder="{{ $field->placeholder }}"
                                        {{ $field->required ? 'required' : '' }}>
                                    @break
                                    
                                @case('number')
                                    <input type="number" 
                                        class="form-control @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}" 
                                        value="{{ old('field_'.$field->id, $value) }}"
                                        placeholder="{{ $field->placeholder }}"
                                        {{ $field->required ? 'required' : '' }}>
                                    @break
                                    
                                @case('date')
                                    <input type="date" 
                                        class="form-control @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}" 
                                        value="{{ old('field_'.$field->id, $value) }}"
                                        {{ $field->required ? 'required' : '' }}>
                                    @break
                                    
                                @case('textarea')
                                    <textarea 
                                        class="form-control @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}" 
                                        rows="3"
                                        placeholder="{{ $field->placeholder }}"
                                        {{ $field->required ? 'required' : '' }}>{{ old('field_'.$field->id, $value) }}</textarea>
                                    @break
                                    
                                @case('select')
                                    <select 
                                        class="form-select @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}"
                                        {{ $field->required ? 'required' : '' }}>
                                        <option value="">{{ $field->placeholder ?: __('patient-custom-fields.select_option') }}</option>
                                        @foreach($field->options as $option)
                                            <option value="{{ $option }}" {{ old('field_'.$field->id, $value) == $option ? 'selected' : '' }}>
                                                {{ $option }}
                                            </option>
                                        @endforeach
                                    </select>
                                    @break
                                    
                                @case('checkbox')
                                    <div class="form-check">
                                        <input type="checkbox" 
                                            class="form-check-input @error('field_'.$field->id) is-invalid @enderror" 
                                            id="field_{{ $field->id }}" 
                                            name="field_{{ $field->id }}" 
                                            value="1"
                                            {{ old('field_'.$field->id, $value) == '1' ? 'checked' : '' }}
                                            {{ $field->required ? 'required' : '' }}>
                                        <label class="form-check-label" for="field_{{ $field->id }}">
                                            {{ $field->placeholder ?: __('patient-custom-fields.yes') }}
                                        </label>
                                    </div>
                                    @break
                                    
                                @case('radio')
                                    @foreach($field->options as $option)
                                        <div class="form-check">
                                            <input type="radio" 
                                                class="form-check-input @error('field_'.$field->id) is-invalid @enderror" 
                                                id="field_{{ $field->id }}_{{ $loop->index }}" 
                                                name="field_{{ $field->id }}" 
                                                value="{{ $option }}"
                                                {{ old('field_'.$field->id, $value) == $option ? 'checked' : '' }}
                                                {{ $field->required ? 'required' : '' }}>
                                            <label class="form-check-label" for="field_{{ $field->id }}_{{ $loop->index }}">
                                                {{ $option }}
                                            </label>
                                        </div>
                                    @endforeach
                                    @break
                                    
                                @default
                                    <input type="text" 
                                        class="form-control @error('field_'.$field->id) is-invalid @enderror" 
                                        id="field_{{ $field->id }}" 
                                        name="field_{{ $field->id }}" 
                                        value="{{ old('field_'.$field->id, $value) }}"
                                        placeholder="{{ $field->placeholder }}"
                                        {{ $field->required ? 'required' : '' }}>
                            @endswitch
                            
                            @error('field_'.$field->id)
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>
                @endforeach
            </div>
            
            <div class="d-flex justify-content-end mt-3">
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-save me-1"></i> {{ __('patient-custom-fields.save_values') }}
                </button>
            </div>
        </form>
</div>
@endif
