@extends('layouts.app')

@section('title', 'New Worklist Entry')

@section('content')
<section class="content-header">
    <h1><i class="fas fa-list"></i> {{ __('nx600Lab::lang.worklist') }} — New Entry</h1>
</section>

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

    <form method="POST" action="{{ route('nx.worklist.store') }}">
        @csrf
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                @component('components.widget', ['class' => 'box-primary', 'title' => 'Worklist Entry Details'])

                    <div class="form-group">
                        <label>Sample Number</label>
                        <input type="text" name="sample_no" maxlength="13" class="form-control"
                               value="{{ old('sample_no') }}" placeholder="Leave blank to auto-generate">
                        <small class="text-muted">Max 13 characters. Must be unique.</small>
                    </div>

                    @if($syncEnabled)
                        <div class="form-group">
                            <label>Order For</label><br>
                            <label class="radio-inline">
                                <input type="radio" name="order_mode" value="pet" checked> VetClinic Pet
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="order_mode" value="manual"> Manual entry
                            </label>
                        </div>

                        <div id="petBlock">
                            <div class="form-group">
                                <label>Pet</label>
                                <select name="vc_pet_id" class="form-control">
                                    <option value="">— Select a pet —</option>
                                    @foreach($pets as $pet)
                                        <option value="{{ $pet->id }}">
                                            {{ $pet->petname }}
                                            @if($pet->species) ({{ ucfirst($pet->species) }}) @endif
                                            @if($pet->owner) — {{ $pet->owner->name ?? $pet->owner->first_name }} @endif
                                        </option>
                                    @endforeach
                                </select>
                                <small class="text-muted">Patient name, species, sex and age are pulled from the pet record.</small>
                            </div>
                        </div>
                    @else
                        <input type="hidden" name="order_mode" value="manual">
                    @endif

                    <div id="manualBlock" style="{{ $syncEnabled ? 'display:none;' : '' }}">
                        <div class="row">
                            <div class="col-md-6 form-group">
                                <label>Patient Name</label>
                                <input type="text" name="patient_name" maxlength="60" class="form-control" value="{{ old('patient_name') }}">
                            </div>
                            <div class="col-md-6 form-group">
                                <label>Patient ID</label>
                                <input type="text" name="patient_id" maxlength="13" class="form-control" value="{{ old('patient_id') }}">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-4 form-group">
                                <label>Species</label>
                                <select name="species" class="form-control">
                                    <option value="0">Unknown</option>
                                    @foreach($speciesMap as $code => $label)
                                        <option value="{{ $code }}">{{ $label }} ({{ $code }})</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-4 form-group">
                                <label>Sex</label>
                                <select name="sex" class="form-control">
                                    <option value="9">Unknown</option>
                                    <option value="0">Male</option>
                                    <option value="1">Female</option>
                                </select>
                            </div>
                            <div class="col-md-4 form-group">
                                <label>Age (years)</label>
                                <input type="number" name="age" min="0" max="999" class="form-control" value="{{ old('age') }}" placeholder="Unknown">
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Tests to Run</label>
                        <textarea name="test_names" rows="2" class="form-control"
                                  placeholder="Comma-separated test names, e.g. GLU-P, BUN-P, CRE-P. Leave blank for the device default panel.">{{ old('test_names') }}</textarea>
                        <small class="text-muted">Up to 30 tests, max 8 characters each.</small>
                    </div>

                    <div class="text-right">
                        <a href="{{ route('nx.worklist.index') }}" class="btn btn-default">{{ __('nx600Lab::lang.cancel') }}</a>
                        <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> {{ __('nx600Lab::lang.save') }}</button>
                    </div>
                @endcomponent
            </div>
        </div>
    </form>
</section>
@endsection

@section('javascript')
<script>
$(function () {
    $('input[name="order_mode"]').on('change', function () {
        var pet = this.value === 'pet';
        $('#petBlock').toggle(pet);
        $('#manualBlock').toggle(!pet);
    });
});
</script>
@endsection
