<x-app-layout :title="$investigation->investigationType->name">
    <x-care.consultation-return :consultation="$returnConsultation ?? null" />

    <div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
        <div>
            <p class="text-xs font-medium uppercase text-slate-500">{{ $statuses[$investigation->status] ?? $investigation->status }}</p>
            <h1 class="text-2xl font-semibold text-slate-900">{{ $investigation->investigationType->name }}</h1>
            <p class="mt-1 text-sm text-slate-500">{{ $investigation->patient->fullName() }} · {{ $investigation->patient->patient_number }}</p>
        </div>
        <div class="flex flex-wrap gap-2">
            @if ($canManage && $investigation->status === \App\Models\InvestigationRequest::STATUS_PENDING)
                <form method="POST" action="{{ route('care.lab.requests.collect-sample', $investigation) }}" class="flex flex-wrap items-end gap-2">
                    @csrf
                    <div>
                        <label for="sample_barcode" class="block text-xs font-medium text-slate-600">Sample barcode</label>
                        <input type="text" id="sample_barcode" name="sample_barcode" autocomplete="off"
                               placeholder="Scan or type barcode…"
                               class="mt-1 w-48 rounded-lg border-slate-300 font-mono text-sm"
                               autofocus>
                    </div>
                    <button class="btn-primary">Collect sample</button>
                </form>
            @endif
            @if ($canManage && $investigation->status === \App\Models\InvestigationRequest::STATUS_SAMPLE_COLLECTED)
                <form method="POST" action="{{ route('care.lab.requests.start', $investigation) }}">@csrf<button class="btn-primary">Start processing</button></form>
            @endif
            @if ($canManage && $investigation->status === \App\Models\InvestigationRequest::STATUS_AWAITING_REVIEW)
                <form method="POST" action="{{ route('care.lab.requests.approve', $investigation) }}">@csrf<button class="btn-primary">Approve results</button></form>
            @endif
            @if ($canManage && $investigation->status === \App\Models\InvestigationRequest::STATUS_COMPLETED)
                <form method="POST" action="{{ route('care.lab.requests.deliver', $investigation) }}">@csrf<button class="btn-primary">Deliver to record</button></form>
            @endif
        </div>
    </div>

    <div class="mt-6 grid gap-6 lg:grid-cols-2">
        <section class="rounded-2xl border border-slate-200 bg-white p-6 text-sm">
            <h2 class="font-semibold uppercase text-xs text-slate-500">Request details</h2>
            <dl class="mt-4 space-y-2">
                <div><dt class="text-slate-500">Priority</dt><dd class="font-medium capitalize">{{ $investigation->priority }}</dd></div>
                <div><dt class="text-slate-500">Sample barcode</dt><dd class="font-medium font-mono">{{ $investigation->sample_barcode ?? '—' }}</dd></div>
                <div><dt class="text-slate-500">Clinical notes</dt><dd class="font-medium">{{ $investigation->clinical_notes ?? '—' }}</dd></div>
            </dl>
        </section>

        @if ($canManage && in_array($investigation->status, [\App\Models\InvestigationRequest::STATUS_IN_PROGRESS, \App\Models\InvestigationRequest::STATUS_AWAITING_REVIEW], true))
            <section class="rounded-2xl border border-slate-200 bg-white p-6">
                <h2 class="text-sm font-semibold uppercase text-slate-500">Enter results</h2>
                <form method="POST" action="{{ route('care.lab.requests.results', $investigation) }}" enctype="multipart/form-data" class="mt-4 space-y-4">
                    @csrf
                    <div>
                        <label class="block text-sm text-slate-700">Value</label>
                        <input type="text" name="value" class="mt-1 w-full rounded-lg border-slate-300 text-sm" placeholder="e.g. 5.2">
                        <p class="mt-1 text-xs text-slate-400">Ref: {{ $investigation->investigationType->reference_text ?? ($investigation->investigationType->reference_low.'–'.$investigation->investigationType->reference_high) }} {{ $investigation->investigationType->unit }}</p>
                    </div>
                    <div>
                        <label class="block text-sm text-slate-700">Summary</label>
                        <textarea name="result_summary" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm"></textarea>
                    </div>
                    <div>
                        <label class="block text-sm text-slate-700">Interpretation</label>
                        <textarea name="interpretation" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm"></textarea>
                    </div>
                    <input type="file" name="attachments[]" multiple class="text-sm">
                    <button type="submit" class="btn-primary">Save results</button>
                </form>
            </section>
        @endif

        @if ($investigation->result && ($canViewResults || $canManage))
            <section class="rounded-2xl border border-slate-200 bg-white p-6 lg:col-span-2">
                <h2 class="text-sm font-semibold uppercase text-slate-500">Results @if ($investigation->result->is_abnormal)<span class="text-red-600">· Abnormal</span>@endif</h2>
                @foreach ($investigation->result->values as $val)
                    <p class="mt-3 text-sm">
                        <span class="font-medium {{ $val->is_abnormal ? 'text-red-600' : '' }}">{{ $val->parameter }}: {{ $val->value }} {{ $val->unit }}</span>
                        @if ($val->reference_text || $val->reference_low) <span class="text-slate-400">(ref: {{ $val->reference_text ?? $val->reference_low.'–'.$val->reference_high }})</span> @endif
                    </p>
                @endforeach
                @if ($investigation->result->result_summary)<p class="mt-3 text-sm">{{ $investigation->result->result_summary }}</p>@endif
                @if ($investigation->result->interpretation)<p class="mt-2 text-sm text-slate-600">{{ $investigation->result->interpretation }}</p>@endif
            </section>
        @endif
    </div>
</x-app-layout>
