@extends('layouts.app')
@section('title', 'Clinic Lab Orders')
@section('content')
<section class="content-header"><h1><i class="fas fa-vials"></i> Clinic Lab</h1></section>
<section class="content">
    @includeIf('clinicLab::partials._tabs', ['active' => 'worklist'])
    @component('components.widget', ['class' => 'box-primary', 'title' => 'Recent lab orders'])
        <table class="table table-sm">
            <thead><tr>
                <th>Patient</th><th>Sample</th><th>Vendor</th><th>Tests</th><th>Status</th>
                <th>Specimen</th><th>Review</th><th>Ordered</th><th></th>
            </tr></thead>
            <tbody>
            @forelse($orders as $o)
                @php
                    $spx = isset($specimenByWorklist) ? ($specimenByWorklist[$o->id] ?? null) : null;
                    $rev = isset($reviewBySample) ? ($reviewBySample[$o->sample_no] ?? null) : null;
                @endphp
                <tr>
                    <td>{{ $o->patient_name }}</td>
                    <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>
                        @if($spx)
                            <a href="{{ route('clinic-lab.specimens.show', $spx->id) }}">{{ $spx->specimen_no }}</a>
                            <span class="label label-{{ $spx->status === 'rejected' ? 'danger' : ($spx->status === 'resulted' ? 'success' : 'default') }}">{{ \Modules\ClinicLab\Entities\CllSpecimen::STATUSES[$spx->status] ?? $spx->status }}</span>
                        @else
                            @can('cliniclab.specimen.manage')
                            <form method="POST" action="{{ route('clinic-lab.specimens.generate', $o->id) }}" style="display:inline;">
                                @csrf
                                <button class="btn btn-xs btn-default"><i class="fas fa-barcode"></i> Generate specimen</button>
                            </form>
                            @else <span class="text-muted">—</span> @endcan
                        @endif
                    </td>
                    <td>
                        @if($rev)
                            <span class="label label-{{ $rev->status === 'verified' ? 'success' : ($rev->status === 'corrected' ? 'warning' : 'default') }}">{{ \Modules\ClinicLab\Entities\CllResultReview::STATUSES[$rev->status] ?? $rev->status }}</span>
                        @else <span class="text-muted">—</span> @endif
                    </td>
                    <td>{{ optional($o->created_at)->format('d-m-Y H:i') }}</td>
                    <td>@if($o->clc_patient_id)<a href="{{ route('clinic-lab.results', $o->clc_patient_id) }}" class="btn btn-xs btn-default">Results</a>@endif</td>
                </tr>
            @empty
                <tr><td colspan="9" class="text-muted">No clinic lab orders yet.</td></tr>
            @endforelse
            </tbody>
        </table>
    @endcomponent
</section>
@endsection
