@php
    $openConsultation = $workspaceVisit->appointment?->consultation
        ?? $workspaceVisit->consultations->firstWhere('status', '!=', \App\Models\Consultation::STATUS_COMPLETED);
    $canRequestLabs = ($canConsult ?? false) && $openConsultation;
    $visitLabRequests = $workspaceVisit->consultations
        ->flatMap(fn ($c) => $c->investigationRequests)
        ->sortByDesc('id')
        ->values();
@endphp

<section class="rounded-2xl border border-slate-200 bg-white p-5">
    <div class="flex flex-wrap items-start justify-between gap-3">
        <div>
            <h3 class="text-sm font-semibold text-slate-900">Orders</h3>
            <p class="mt-0.5 text-xs text-slate-500">Lab and imaging requests for this visit.</p>
        </div>
        @if ($canRequestLabs)
            <button
                type="button"
                class="rounded-lg bg-indigo-600 px-3 py-1.5 text-sm font-semibold text-white hover:bg-indigo-700"
                @click="$dispatch('open-modal', {{ \Illuminate\Support\Js::from('specialty-request-investigations-'.$openConsultation->id) }})"
            >
                Request investigations
            </button>
        @endif
    </div>

    @if (! $openConsultation)
        <p class="mt-3 text-sm text-amber-800">Start the encounter to order lab / imaging from this visit.</p>
    @endif

    <ul class="mt-4 space-y-2 text-sm">
        @forelse ($visitLabRequests as $req)
            <li class="flex flex-wrap items-center justify-between gap-2 rounded-xl border border-slate-100 bg-slate-50 px-3 py-2">
                <div>
                    <p class="font-medium text-slate-800">{{ $req->investigationType?->name ?? 'Investigation' }}</p>
                    <p class="text-xs text-slate-500">{{ config('care.investigation_statuses')[$req->status] ?? $req->status }}</p>
                </div>
                <a href="{{ route('care.lab.requests.show', $req) }}" class="text-xs font-medium text-indigo-600">Open</a>
            </li>
        @empty
            <li class="text-slate-500">No lab requests on this visit yet.</li>
        @endforelse
    </ul>

    <div class="mt-4 flex flex-wrap gap-3 border-t border-slate-100 pt-4">
        @if ($workspaceVisit->appointment)
            <a href="{{ route('care.appointments.show', $workspaceVisit->appointment) }}" class="text-sm font-medium text-indigo-600">Appointment</a>
        @endif
    </div>
</section>

@if ($canRequestLabs && ($investigationTypes ?? collect())->isNotEmpty())
    <x-modal :name="'specialty-request-investigations-'.$openConsultation->id" maxWidth="2xl">
        <div class="px-5 pb-6 pt-2 sm:px-6 sm:pb-6 sm:pt-4">
            <h2 class="pr-8 text-lg font-semibold text-slate-900">Request investigations</h2>
            <p class="mt-1 text-sm text-slate-500">Select tests to send to the lab for this visit.</p>
            <form method="POST" action="{{ route('care.lab.requests.store', $openConsultation) }}" class="mt-4 space-y-4">
                @csrf
                <div class="grid max-h-[40vh] gap-2 overflow-y-auto sm:grid-cols-2">
                    @foreach ($investigationTypes as $type)
                        <label class="flex items-center gap-2 rounded-lg border border-slate-100 bg-slate-50 px-3 py-2 text-sm">
                            <input type="checkbox" name="investigation_type_ids[]" value="{{ $type->id }}" class="rounded border-slate-300">
                            <span>{{ $type->name }} <span class="text-slate-400">({{ config('care.investigation_categories')[$type->category] ?? $type->category }})</span></span>
                        </label>
                    @endforeach
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-600">Clinical notes</label>
                    <textarea name="clinical_notes" rows="2" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm"></textarea>
                </div>
                <div>
                    <label class="block text-xs font-medium text-slate-600">Priority</label>
                    <select name="priority" class="mt-1 w-full rounded-xl border border-slate-200 px-3 py-2 text-sm">
                        <option value="routine">Routine</option>
                        <option value="urgent">Urgent</option>
                    </select>
                </div>
                <div class="flex justify-end gap-2">
                    <button type="button" class="rounded-xl border border-slate-200 px-4 py-2.5 text-sm font-semibold text-slate-700 hover:bg-slate-50" @click="$dispatch('close-modal', {{ \Illuminate\Support\Js::from('specialty-request-investigations-'.$openConsultation->id) }})">
                        Cancel
                    </button>
                    <button type="submit" class="rounded-xl bg-indigo-600 px-4 py-2.5 text-sm font-semibold text-white hover:bg-indigo-700">Submit requests</button>
                </div>
            </form>
        </div>
    </x-modal>
@endif
