<x-app-layout title="{{ $definition['label'] ?? $moduleKey }} · Module settings">
    <x-settings.page
        title="{{ $definition['label'] ?? ucfirst($moduleKey) }}"
        description="{{ $definition['description'] ?? 'Module settings and service prices for '.$organization->name.'.' }}"
    >
        <div class="flex items-center gap-3">
            <x-care.specialty-icon :module="$moduleKey" boxed />
            <p class="text-sm">
                <a href="{{ route('care.settings.modules') }}" class="font-medium text-indigo-600 hover:text-indigo-800">← Specialty modules</a>
            </p>
        </div>

        @if (session('success'))
            <p class="rounded-lg bg-emerald-50 px-3 py-2 text-sm text-emerald-800">{{ session('success') }}</p>
        @endif

        <x-settings.card title="Status" description="Activate modules from the modules list. Drug prices stay in Inventory; lab test prices are set by admins in Laboratory catalog.">
            <div class="flex flex-wrap items-center gap-3 text-sm">
                @if ($enabled)
                    <span class="rounded-full bg-emerald-50 px-2.5 py-1 text-xs font-semibold text-emerald-800">Active</span>
                @else
                    <span class="rounded-full bg-slate-100 px-2.5 py-1 text-xs font-semibold text-slate-600">Inactive</span>
                @endif
                @if ($canOpenWorkspace)
                    <a href="{{ route('care.specialty.show', $moduleKey) }}" class="font-medium text-indigo-600 hover:text-indigo-800">Open module workspace →</a>
                @endif
                <a href="{{ route('care.settings.modules') }}" class="font-medium text-slate-600 hover:text-slate-900">Manage activation</a>
            </div>
        </x-settings.card>

        @if (! empty($serviceLines) && $canUseSpecialtyModules)
            <x-settings.card title="Service lines" description="Enable only the clinics and services this facility offers under {{ $definition['label'] ?? $moduleKey }}.">
                <form method="POST" action="{{ route('care.settings.modules.service-lines.update', $moduleKey) }}" class="space-y-4">
                    @csrf
                    @method('PUT')
                    <div class="grid gap-2 sm:grid-cols-2">
                        @foreach ($serviceLines as $lineKey => $line)
                            <label class="flex items-start gap-2 rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-800">
                                <input
                                    type="checkbox"
                                    name="lines[{{ $lineKey }}]"
                                    value="1"
                                    class="mt-0.5 rounded border-slate-300 text-indigo-600"
                                    @checked(old('lines.'.$lineKey, $enabledServiceLines[$lineKey] ?? ($line['default_on'] ?? true)))
                                    @disabled(! $canManage)
                                >
                                <span>
                                    <span class="font-medium">{{ $line['label'] }}</span>
                                </span>
                            </label>
                        @endforeach
                    </div>
                    @if ($canManage)
                        <div class="flex justify-end">
                            <button type="submit" class="btn-primary">Save service lines</button>
                        </div>
                    @endif
                </form>
            </x-settings.card>
        @endif

        @if (! $canUseSpecialtyModules)
            <x-settings.card title="Pro feature" description="Specialty module pricing requires Care Pro or Enterprise.">
                <a href="{{ route('care.pro.index') }}" class="btn-primary mt-2 inline-flex text-sm">View plans</a>
            </x-settings.card>
        @else
            <x-settings.card title="Service prices" description="Amounts billed when these services are added from the specialty workspace. Currency: {{ $currency }}.">
                <form method="POST" action="{{ route('care.settings.modules.services.update', $moduleKey) }}" class="space-y-4">
                    @csrf
                    @method('PUT')
                    <div class="overflow-x-auto">
                        <table class="min-w-full text-left text-sm">
                            <thead>
                                <tr class="border-b border-slate-200 text-xs uppercase tracking-wide text-slate-500">
                                    <th class="py-2 pr-3">Service</th>
                                    <th class="py-2 pr-3">Type</th>
                                    <th class="py-2">Price ({{ $currency }})</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($services as $i => $service)
                                    <tr class="border-b border-slate-100">
                                        <td class="py-2.5 pr-3">
                                            <input type="hidden" name="services[{{ $i }}][code]" value="{{ $service['code'] }}">
                                            <input type="hidden" name="services[{{ $i }}][label]" value="{{ $service['label'] }}">
                                            <p class="font-medium text-slate-900">{{ $service['label'] }}</p>
                                            <p class="text-xs text-slate-400">{{ $service['code'] }}</p>
                                        </td>
                                        <td class="py-2.5 pr-3 text-slate-600">{{ ucfirst($service['type'] ?? 'misc') }}</td>
                                        <td class="py-2.5">
                                            <input
                                                type="number"
                                                name="services[{{ $i }}][amount]"
                                                step="0.01"
                                                min="0"
                                                value="{{ old('services.'.$i.'.amount', number_format(((int) ($service['amount_minor'] ?? 0)) / 100, 2, '.', '')) }}"
                                                @disabled(! $canManage)
                                                class="w-32 rounded-lg border-slate-300 text-sm tabular-nums"
                                                required
                                            >
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    @if ($canManage)
                        <div class="flex justify-end">
                            <button type="submit" class="btn-primary">Save prices</button>
                        </div>
                    @endif
                </form>
            </x-settings.card>
        @endif
    </x-settings.page>
</x-app-layout>
