@extends('layouts.app')
@section('title', $test->exists ? 'Edit Test' : 'New Test')
@section('content')
<section class="content-header"><h1><i class="fas fa-book-medical"></i> {{ $test->exists ? 'Edit Test' : 'New Test / Panel' }}</h1></section>
<section class="content">
    @includeIf('clinicLab::partials._tabs', ['active' => 'catalog'])
    <form method="POST" action="{{ $test->exists ? route('clinic-lab.catalog.update', $test->id) : route('clinic-lab.catalog.store') }}">
        @csrf
        @if($test->exists) @method('PUT') @endif

        @component('components.widget', ['class' => 'box-primary', 'title' => 'Test details'])
            <div class="row">
                <div class="col-md-3"><div class="form-group">
                    <label>Code *</label>
                    <input type="text" name="code" class="form-control" value="{{ old('code', $test->code) }}" required>
                </div></div>
                <div class="col-md-5"><div class="form-group">
                    <label>Name *</label>
                    <input type="text" name="name" class="form-control" value="{{ old('name', $test->name) }}" required>
                </div></div>
                <div class="col-md-4"><div class="form-group">
                    <label>Category *</label>
                    <select name="category" class="form-control">
                        @foreach(['hematology','chemistry','microbiology','serology','immunology','hormones','urine','stool','molecular','histopathology','other'] as $c)
                            <option value="{{ $c }}" @if(old('category', $test->category)===$c) selected @endif>{{ ucfirst($c) }}</option>
                        @endforeach
                    </select>
                </div></div>
            </div>
            <div class="row">
                <div class="col-md-3"><div class="form-group">
                    <label>Specimen type *</label>
                    <select name="specimen_type" class="form-control">
                        @foreach(['blood','serum','plasma','urine','stool','swab','csf','tissue','other'] as $s)
                            <option value="{{ $s }}" @if(old('specimen_type', $test->specimen_type ?: 'blood')===$s) selected @endif>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div></div>
                <div class="col-md-3"><div class="form-group">
                    <label>Container</label>
                    <input type="text" name="container" class="form-control" value="{{ old('container', $test->container) }}" placeholder="EDTA tube">
                </div></div>
                <div class="col-md-2"><div class="form-group">
                    <label>Min volume</label>
                    <input type="text" name="min_volume" class="form-control" value="{{ old('min_volume', $test->min_volume) }}" placeholder="5ml">
                </div></div>
                <div class="col-md-2"><div class="form-group">
                    <label>TAT (hours)</label>
                    <input type="number" name="tat_hours" class="form-control" value="{{ old('tat_hours', $test->tat_hours) }}" min="0">
                </div></div>
                <div class="col-md-2"><div class="form-group">
                    <label>Method</label>
                    <input type="text" name="method" class="form-control" value="{{ old('method', $test->method) }}">
                </div></div>
            </div>
            <div class="row">
                <div class="col-md-6"><div class="form-group">
                    <label>Billing product (ERP)</label>
                    <select name="product_id" class="form-control">
                        <option value="">— Auto-match by name / none —</option>
                        @foreach($products as $p)
                            <option value="{{ $p->id }}" @if((int)old('product_id', $test->product_id)===(int)$p->id) selected @endif>{{ $p->name }}</option>
                        @endforeach
                    </select>
                    <small class="text-muted">Leave blank to auto-link a product whose name matches this test.</small>
                </div></div>
                <div class="col-md-6"><div class="form-group">
                    <label>&nbsp;</label><br>
                    <label style="font-weight:normal;"><input type="checkbox" name="fasting_required" value="1" @if(old('fasting_required', $test->fasting_required)) checked @endif> Fasting required</label>
                    &nbsp;&nbsp;
                    <label style="font-weight:normal;"><input type="checkbox" name="is_panel" value="1" @if(old('is_panel', $test->is_panel)) checked @endif> Panel (multiple analytes)</label>
                    &nbsp;&nbsp;
                    <label style="font-weight:normal;"><input type="checkbox" name="is_active" value="1" @if(old('is_active', $test->exists ? $test->is_active : true)) checked @endif> Active</label>
                </div></div>
            </div>
        @endcomponent

        @component('components.widget', ['class' => 'box-default', 'title' => 'Analytes, reference & critical ranges'])
            <table class="table table-sm" id="analyte_table">
                <thead><tr>
                    <th>Code</th><th>Name</th><th>Unit</th><th>Ref low</th><th>Ref high</th>
                    <th>Critical low</th><th>Critical high</th><th></th>
                </tr></thead>
                <tbody>
                @php $existing = $test->exists ? $test->analytes : collect(); @endphp
                @forelse($existing as $i => $a)
                    <tr>
                        <td><input type="text" name="analytes[{{ $i }}][analyte_code]" class="form-control input-sm" value="{{ $a->analyte_code }}"></td>
                        <td><input type="text" name="analytes[{{ $i }}][analyte_name]" class="form-control input-sm" value="{{ $a->analyte_name }}"></td>
                        <td><input type="text" name="analytes[{{ $i }}][unit]" class="form-control input-sm" value="{{ $a->unit }}"></td>
                        <td><input type="text" name="analytes[{{ $i }}][ref_low]" class="form-control input-sm" value="{{ $a->ref_low }}"></td>
                        <td><input type="text" name="analytes[{{ $i }}][ref_high]" class="form-control input-sm" value="{{ $a->ref_high }}"></td>
                        <td><input type="text" name="analytes[{{ $i }}][critical_low]" class="form-control input-sm" value="{{ $a->critical_low }}"></td>
                        <td><input type="text" name="analytes[{{ $i }}][critical_high]" class="form-control input-sm" value="{{ $a->critical_high }}"></td>
                        <td><button type="button" class="btn btn-xs btn-danger" onclick="this.closest('tr').remove()">&times;</button></td>
                    </tr>
                @empty
                @endforelse
                </tbody>
            </table>
            <button type="button" class="btn btn-default btn-sm" id="add_analyte"><i class="fas fa-plus"></i> Add analyte</button>
            <small class="text-muted">Critical bounds drive the automatic critical-value alert on verification.</small>
        @endcomponent

        <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save</button>
        <a href="{{ route('clinic-lab.catalog.index') }}" class="btn btn-link">Cancel</a>
    </form>
</section>
@endsection

@section('javascript')
<script>
(function () {
    var idx = {{ $test->exists ? $test->analytes->count() : 0 }};
    document.getElementById('add_analyte').addEventListener('click', function () {
        var tb = document.querySelector('#analyte_table tbody');
        var tr = document.createElement('tr');
        var cols = ['analyte_code','analyte_name','unit','ref_low','ref_high','critical_low','critical_high'];
        var html = '';
        cols.forEach(function (c) {
            html += '<td><input type="text" name="analytes[' + idx + '][' + c + ']" class="form-control input-sm"></td>';
        });
        html += '<td><button type="button" class="btn btn-xs btn-danger" onclick="this.closest(\'tr\').remove()">&times;</button></td>';
        tr.innerHTML = html;
        tb.appendChild(tr);
        idx++;
    });
})();
</script>
@endsection
