@extends('layouts.app')
@section('title', 'Lab Results — ' . $patient->patient_name)
@section('content')
<section class="content-header"><h1><i class="fas fa-flask"></i> Lab Results
    <small>{{ $patient->patient_name }}</small></h1></section>
<section class="content">
    @includeIf('clinicLab::partials._tabs', ['active' => 'worklist'])
    <p><a href="{{ route('clinic-lab.order', $patient->id) }}" class="btn btn-primary btn-sm"><i class="fas fa-plus"></i> New order</a></p>

    @component('components.widget', ['class' => 'box-default', 'title' => 'Orders'])
        <table class="table table-sm">
            <thead><tr><th>Sample</th><th>Vendor</th><th>Tests</th><th>Status</th><th>Ordered</th></tr></thead>
            <tbody>
            @forelse($orders as $o)
                <tr>
                    <td>{{ $o->sample_no }}</td>
                    <td>{{ $o->vendor }}</td>
                    <td>{{ is_array($o->tests) ? implode(', ', $o->tests) : '—' }}</td>
                    <td><span class="label label-{{ $o->status === 'completed' ? 'success' : ($o->status === 'cancelled' ? 'danger' : 'warning') }}">{{ ucfirst($o->status) }}</span></td>
                    <td>{{ optional($o->created_at)->format('d-m-Y H:i') }}</td>
                </tr>
            @empty
                <tr><td colspan="5" class="text-muted">No orders yet.</td></tr>
            @endforelse
            </tbody>
        </table>
    @endcomponent

    @component('components.widget', ['class' => 'box-success', 'title' => 'Results'])
        @if(class_exists(\Modules\AiCore\Entities\Services\AiClient::class) && $sessions->count())
        <div style="margin-bottom:10px;">
            <button type="button" class="btn btn-info btn-sm" id="lab_ai_interpret"><i class="fa fa-brain"></i> AI interpret results</button>
            <div id="lab_ai_panel" style="margin-top:10px;"></div>
        </div>
        @endif
        @forelse($sessions as $s)
            <div style="border:1px solid #e2e8f0;border-radius:6px;padding:8px 12px;margin-bottom:8px;">
                <strong>{{ $s->sample_no }}</strong>
                <span class="text-muted">· {{ $s->vendor }} · {{ $s->test_date }} {{ $s->test_time }}</span>
                <table class="table table-sm" style="margin-top:6px;margin-bottom:0;">
                    <thead><tr><th>Analyte</th><th>Result</th><th>Unit</th><th>Reference</th><th>Flag</th></tr></thead>
                    <tbody>
                    @foreach($s->results as $r)
                        <tr @if($r->is_abnormal) style="color:#dc3545;font-weight:600;" @endif>
                            <td>{{ $r->analyte_name ?? $r->analyte_code }}</td>
                            <td><strong>{{ $r->value }}</strong></td>
                            <td>{{ $r->unit }}</td>
                            <td>{{ $r->ref_lower }}{{ $r->ref_lower !== null && $r->ref_upper !== null ? ' – ' : '' }}{{ $r->ref_upper }}</td>
                            <td>{{ $r->flags ?? $r->raw_flag }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        @empty
            <p class="text-muted">No results received yet. Results appear here once the analyzer reports back.</p>
        @endforelse
    @endcomponent
</section>
@endsection

@section('javascript')
<script>
var LAB_URL   = '{{ route('aicore.interpret-lab') }}';
var LAB_TOKEN = '{{ csrf_token() }}';
@php
    $labResultsData = $sessions->flatMap(function ($s) {
        return collect($s->results)->map(function ($r) {
            return [
                'test'  => ($r->analyte_name ?? $r->analyte_code),
                'value' => $r->value,
                'unit'  => $r->unit,
                'ref'   => trim(($r->ref_lower ?? '') . ' - ' . ($r->ref_upper ?? '')),
                'flag'  => $r->is_abnormal ? 'Abnormal' : (string) ($r->flags ?? $r->raw_flag ?? ''),
            ];
        });
    })->values();
@endphp
var LAB_RESULTS = @json($labResultsData);
var LAB_CONTEXT = @json('Patient: ' . ($patient->patient_name ?? ''));
@include('aiCore::partials._lab_interpret_js')
</script>
@endsection
