@extends('layouts.app')

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

@section('content')
<section class="content-header">
    <h1><i class="fas fa-list"></i> {{ __('nx600Lab::lang.worklist') }}</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" style="margin-bottom:10px;">
        <div class="col-md-12">
            <a href="{{ route('nx.worklist.create') }}" class="btn btn-primary pull-right">
                <i class="fa fa-plus"></i> {{ __('nx600Lab::lang.add_new') }}
            </a>
            <p class="text-muted">
                Entries waiting here are sent to the NX600 analyzer when it issues a Worklist request.
            </p>
        </div>
    </div>

    <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...">
                    <select name="status" class="form-control">
                        <option value="">All statuses</option>
                        <option value="pending" {{ request('status') === 'pending' ? 'selected' : '' }}>Pending</option>
                        <option value="sent" {{ request('status') === 'sent' ? 'selected' : '' }}>Retrieved by device</option>
                    </select>
                    <button class="btn btn-default"><i class="fa fa-search"></i> Filter</button>
                </form>

                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Sample No</th>
                            <th>Patient</th>
                            <th>Patient ID</th>
                            <th>Species / Sex / Age</th>
                            <th>Tests</th>
                            <th>Status</th>
                            <th>Created</th>
                            <th>{{ __('nx600Lab::lang.actions') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($entries as $e)
                            <tr>
                                <td><strong>{{ $e->sample_no }}</strong></td>
                                <td>
                                    {{ $e->patient_name ?: '—' }}
                                    @if($e->vc_pet_id)
                                        <span class="label label-success" title="Linked to a VetClinic pet"><i class="fas fa-paw"></i></span>
                                    @endif
                                </td>
                                <td>{{ $e->patient_id ?: '—' }}</td>
                                <td>{{ $e->species_name }} / {{ $e->sex_label }} / {{ $e->age === 999 ? 'N/A' : $e->age }}</td>
                                <td>
                                    @if(empty($e->test_names))
                                        <span class="text-muted">All (device default)</span>
                                    @else
                                        {{ implode(', ', $e->test_names) }}
                                    @endif
                                </td>
                                <td>
                                    @if($e->is_sent)
                                        <span class="label label-default">Retrieved {{ $e->sent_at?->diffForHumans() }}</span>
                                    @else
                                        <span class="label label-warning">Pending</span>
                                    @endif
                                </td>
                                <td>{{ $e->created_at?->format('Y-m-d H:i') }}</td>
                                <td>
                                    @if($e->is_sent)
                                        <form method="POST" action="{{ route('nx.worklist.reset', $e->id) }}" style="display:inline;">
                                            @csrf @method('PATCH')
                                            <button class="btn btn-xs btn-default" title="Let the device pick this up again">
                                                <i class="fa fa-undo"></i> Reset
                                            </button>
                                        </form>
                                    @endif
                                    <form method="POST" action="{{ route('nx.worklist.destroy', $e->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>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="8" class="text-center text-muted">No worklist entries.</td></tr>
                        @endforelse
                    </tbody>
                </table>

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