@extends('layouts.app')
@section('title', 'Imaging Worklist')

@section('css')
@include('clinicCore::partials._mobile_responsive')
@endsection

@section('content')
<section class="content-header">
    <h1><i class="fa fa-x-ray"></i> Imaging Worklist <small>X-Ray, Ultrasound, CT, MRI</small></h1>
    @can('clinic.view_imaging')
    <a href="{{ route('clinic.imaging.worklist') }}" class="btn btn-sm btn-primary" style="margin-top:8px;">
        <i class="fa fa-th-list"></i> {{ __('clinicCore::lang.imgrep_worklist_sub') }}
    </a>
    @endcan
</section>

<section class="content">
    @if($requests->isEmpty())
        <div class="callout callout-info">No imaging requests yet. Create one from a patient or appointment.</div>
    @else
        <table class="table table-bordered table-condensed">
            <thead>
                <tr>
                    <th>Ref</th>
                    <th>Patient</th>
                    <th>Modality</th>
                    <th>Body region</th>
                    <th>Priority</th>
                    <th>Status</th>
                    <th>Studies</th>
                    <th>Requested</th>
                    <th>Report</th>
                </tr>
            </thead>
            <tbody>
                @foreach($requests as $r)
                    @php
                        $statusMap = [
                            'requested'   => 'default',
                            'in_progress' => 'warning',
                            'reported'    => 'success',
                            'cancelled'   => 'danger',
                        ];
                        $priMap = ['routine'=>'default','urgent'=>'warning','stat'=>'danger'];
                    @endphp
                    <tr>
                        <td><small>{{ $r->ref_no }}</small></td>
                        <td><strong>{{ optional($r->pet)->patient_name }}</strong>@if(config('cliniccore.show_animal_fields', false))<br>
                            <small class="text-muted">{{ optional($r->pet)->species }}</small>@endif</td>
                        <td>{{ optional($r->modality)->code }} · {{ optional($r->modality)->name }}</td>
                        <td>{{ $r->body_region ?: '—' }}</td>
                        <td><span class="label label-{{ $priMap[$r->priority] ?? 'default' }}">{{ $r->priority }}</span></td>
                        <td><span class="label label-{{ $statusMap[$r->status] ?? 'default' }}">{{ str_replace('_',' ',$r->status) }}</span></td>
                        <td>
                            @foreach($r->studies->take(4) as $s)
                                <a href="{{ $s->file_url }}" target="_blank" style="display:inline-block;margin-right:4px;">
                                    @if($s->is_image)
                                        <img src="{{ $s->file_url }}" alt="" style="width:42px;height:42px;object-fit:cover;border:1px solid #ddd;border-radius:3px;">
                                    @elseif($s->is_dicom)
                                        <span class="label label-primary">DICOM</span>
                                    @else
                                        <span class="label label-default">{{ strtoupper(pathinfo($s->original_name ?? '', PATHINFO_EXTENSION) ?: 'file') }}</span>
                                    @endif
                                </a>
                            @endforeach
                            @if($r->studies->count() > 4)<span class="text-muted">+{{ $r->studies->count() - 4 }}</span>@endif
                        </td>
                        <td><small>{{ optional($r->requested_at)->format('d M Y H:i') }}</small></td>
                        <td>
                            @can('clinic.imaging.report')
                            <a href="{{ route('clinic.imaging.report.edit', $r->id) }}" class="btn btn-xs btn-primary">
                                <i class="fa fa-file-medical"></i> {{ __('clinicCore::lang.imgrep_report') }}
                            </a>
                            @endcan
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
        {{ $requests->links() }}
    @endif
</section>
@endsection
