@extends('layouts.app')

@section('title', __('medregistry::lang.instance_settings'))

@section('content')
<section class="content-header">
    <h1>{{ $module->display_name }}
        <small>@lang('medregistry::lang.instance_settings') — {{ $instance->scopeLabel() }}</small>
    </h1>
</section>

<section class="content">
    @component('components.widget', ['class' => 'box-primary'])
        @if(empty($schema))
            <p class="text-muted">@lang('medregistry::lang.no_settings_schema')</p>
        @else
            {{--
              P1-T5 generic settings renderer: the form below is generated from
              the module's settings_schema json. Supported field types:
              boolean / select / text / number (labels + defaults).
            --}}
            <form method="post" action="{{ route('medreg.instances.settings.update', $instance->id) }}">
                @csrf

                @foreach($schema as $key => $field)
                    @php
                        $type = $field['type'] ?? 'text';
                        $label = $field['label'] ?? $key;
                        $value = array_key_exists($key, $values) ? $values[$key] : ($field['default'] ?? null);
                    @endphp

                    <div class="form-group">
                        @if($type === 'boolean')
                            <div class="checkbox">
                                <label>
                                    {{-- hidden 0 so unchecked posts false --}}
                                    <input type="hidden" name="settings[{{ $key }}]" value="0">
                                    <input type="checkbox" name="settings[{{ $key }}]" value="1" @if(!empty($value)) checked @endif>
                                    {{ $label }}
                                </label>
                            </div>
                        @elseif($type === 'select')
                            <label>{{ $label }}</label>
                            <select name="settings[{{ $key }}]" class="form-control">
                                @foreach(($field['options'] ?? []) as $optValue => $optLabel)
                                    <option value="{{ $optValue }}" @if((string) $value === (string) $optValue) selected @endif>
                                        {{ $optLabel }}
                                    </option>
                                @endforeach
                            </select>
                        @elseif($type === 'number')
                            <label>{{ $label }}</label>
                            <input type="number" step="any" name="settings[{{ $key }}]" class="form-control"
                                   value="{{ old('settings.' . $key, $value) }}">
                        @else
                            <label>{{ $label }}</label>
                            <input type="text" name="settings[{{ $key }}]" class="form-control"
                                   value="{{ old('settings.' . $key, $value) }}">
                        @endif
                    </div>
                @endforeach

                <button type="submit" class="btn btn-primary">@lang('messages.save')</button>
                <a href="{{ route('medreg.instances.index') }}" class="btn btn-default">@lang('messages.cancel')</a>
            </form>
        @endif
    @endcomponent
</section>
@endsection
