@extends('layouts.app')

@section('title', __('nx600Lab::lang.test_results'))

@section('content')
<section class="content-header">
    <h1><i class="fas fa-flask"></i> {{ __('nx600Lab::lang.test_results') }}</h1>
</section>

<section class="content">

    @if(session('status'))
        <div class="alert alert-{{ session('status')['success'] ? 'success' : 'danger' }}">
            {{ session('status')['msg'] }}
        </div>
    @endif

    <div class="row">
        <div class="col-md-12">
            @component('components.widget', ['class' => 'box-primary'])
                <form method="GET" class="form-inline" style="margin-bottom:12px;">
                    <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="Sample / patient...">
                    <input type="date" name="date_from" value="{{ request('date_from') }}" class="form-control">
                    <input type="date" name="date_to" value="{{ request('date_to') }}" class="form-control">
                    <label class="checkbox-inline">
                        <input type="checkbox" name="abnormal" value="1" {{ request('abnormal') == '1' ? 'checked' : '' }}> Abnormal only
                    </label>
                    <button class="btn btn-default"><i class="fa fa-search"></i> Filter</button>
                    <a href="{{ route('nx.results.index') }}" class="btn btn-link">Clear</a>
                </form>

                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Sample No</th>
                            <th>Patient</th>
                            <th>Species</th>
                            <th>Date / Time</th>
                            <th>Parameters</th>
                            <th>Result</th>
                            <th>Comm</th>
                            <th>{{ __('nx600Lab::lang.actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($sessions as $s)
                            @php $abn = $s->testResults->where('is_abnormal', true)->count(); @endphp
                            <tr>
                                <td><strong>{{ $s->sample_no }}</strong></td>
                                <td>
                                    {{ $s->patient_name ?: '—' }}
                                    @if($s->vc_pet_id)
                                        <span class="label label-success" title="VetClinic pet"><i class="fas fa-paw"></i></span>
                                    @endif
                                    @if($s->test_condition === 'CONTROL')
                                        <span class="label label-info">QC</span>
                                    @endif
                                </td>
                                <td>{{ $s->species_name }}</td>
                                <td>{{ $s->test_date?->format('Y-m-d') }} {{ $s->test_time }}</td>
                                <td>{{ $s->testResults->count() }}</td>
                                <td>
                                    @if($abn > 0)
                                        <span class="label label-warning">{{ $abn }} abnormal</span>
                                    @else
                                        <span class="label label-success">Normal</span>
                                    @endif
                                </td>
                                <td>{{ $s->comm_type }}</td>
                                <td>
                                    <a href="{{ route('nx.results.show', $s->id) }}" class="btn btn-xs btn-primary"><i class="fa fa-eye"></i></a>
                                    <a href="{{ route('nx.results.print', $s->id) }}" target="_blank" class="btn btn-xs btn-default"><i class="fa fa-print"></i></a>
                                    @can('nx600_lab.manage_results')
                                        <form method="POST" action="{{ route('nx.results.destroy', $s->id) }}" style="display:inline;"
                                              onsubmit="return confirm('{{ __('nx600Lab::lang.confirm_delete') }}');">
                                            @csrf @method('DELETE')
                                            <button class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
                                        </form>
                                    @endcan
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="8" class="text-center text-muted">No test sessions found.</td></tr>
                        @endforelse
                    </tbody>
                </table>

                {!! $sessions->links() !!}
            @endcomponent
        </div>
    </div>
</section>
@endsection
