@extends('layouts.app')
@section('title', 'Specimen ' . $specimen->specimen_no)
@section('content')
<section class="content-header"><h1><i class="fas fa-barcode"></i> Specimen <small>{{ $specimen->specimen_no }}</small></h1></section>
<section class="content">
    @includeIf('clinicLab::partials._tabs', ['active' => 'specimens'])
    <div class="row">
        <div class="col-md-7">
            @component('components.widget', ['class' => 'box-primary', 'title' => 'Details'])
                <table class="table table-sm">
                    <tr><th style="width:40%;">Status</th><td><span class="label label-{{ $specimen->status === 'rejected' ? 'danger' : ($specimen->status === 'resulted' ? 'success' : 'default') }}">{{ \Modules\ClinicLab\Entities\CllSpecimen::STATUSES[$specimen->status] ?? $specimen->status }}</span></td></tr>
                    <tr><th>Type</th><td>{{ ucfirst($specimen->specimen_type) }}</td></tr>
                    <tr><th>Test</th><td>{{ optional($specimen->catalog)->name ?? '—' }}</td></tr>
                    <tr><th>Worklist sample</th><td>{{ optional($specimen->worklist)->sample_no ?? '—' }}</td></tr>
                    <tr><th>Order item</th><td>{{ $specimen->clc_order_item_id ? '#' . $specimen->clc_order_item_id : '—' }}</td></tr>
                    <tr><th>Collected</th><td>{{ optional($specimen->collected_at)->format('d-m-Y H:i') ?? '—' }}</td></tr>
                    <tr><th>Received</th><td>{{ optional($specimen->received_at)->format('d-m-Y H:i') ?? '—' }}</td></tr>
                    @if($specimen->status === 'rejected')
                    <tr><th>Rejection</th><td class="text-danger">{{ \Modules\ClinicLab\Entities\CllSpecimen::REJECT_REASONS[$specimen->rejected_reason] ?? $specimen->rejected_reason }}
                        @if($specimen->rejection_notes) — {{ $specimen->rejection_notes }} @endif</td></tr>
                    @endif
                </table>
                <a href="{{ route('clinic-lab.specimens.barcode', $specimen->id) }}" target="_blank" class="btn btn-default btn-sm"><i class="fas fa-print"></i> Print barcode</a>
            @endcomponent
        </div>
        <div class="col-md-5">
            @can('cliniclab.specimen.manage')
            @component('components.widget', ['class' => 'box-default', 'title' => 'Actions'])
                @if(!in_array($specimen->status, ['rejected','disposed']))
                <div class="btn-group-vertical" style="width:100%;">
                    @if($specimen->status === 'awaiting_collection')
                    <form method="POST" action="{{ route('clinic-lab.specimens.collect', $specimen->id) }}">@csrf<button class="btn btn-primary btn-block"><i class="fas fa-hand-holding-medical"></i> Mark collected</button></form>
                    @endif
                    @if(in_array($specimen->status, ['collected','in_transit']))
                    <form method="POST" action="{{ route('clinic-lab.specimens.receive', $specimen->id) }}">@csrf<button class="btn btn-primary btn-block"><i class="fas fa-inbox"></i> Mark received</button></form>
                    @endif
                    @if($specimen->status === 'received')
                    <form method="POST" action="{{ route('clinic-lab.specimens.process', $specimen->id) }}">@csrf<button class="btn btn-primary btn-block"><i class="fas fa-flask"></i> Start processing</button></form>
                    @endif
                </div>
                <hr>
                <form method="POST" action="{{ route('clinic-lab.specimens.reject', $specimen->id) }}">
                    @csrf
                    <div class="form-group">
                        <label>Reject specimen</label>
                        <select name="rejected_reason" class="form-control input-sm" required>
                            @foreach(\Modules\ClinicLab\Entities\CllSpecimen::REJECT_REASONS as $k => $label)
                                <option value="{{ $k }}">{{ $label }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <input type="text" name="rejection_notes" class="form-control input-sm" placeholder="Notes (optional)">
                    </div>
                    <button class="btn btn-danger btn-sm" onclick="return confirm('Reject this specimen? The ordering context will be notified.');"><i class="fas fa-ban"></i> Reject</button>
                </form>
                @else
                <p class="text-muted">This specimen is {{ $specimen->status }} — no further actions.</p>
                @endif
            @endcomponent
            @endcan
        </div>
    </div>
</section>
@endsection
