@extends('layouts.app')

@section('title', isset($report) ? __('clinicCore::lang.edit') : __('clinicCore::lang.add_new'))

@section('css')
@include('clinicCore::partials._mobile_responsive')
@endsection

@section('content')
<section class="content-header">
    <h1><i class="fas fa-file-medical"></i>
        {{ isset($report) ? 'Edit Medical Report' : 'New Medical Report' }}
    </h1>
    <ol class="breadcrumb">
        <li><a href="{{ route('clinic.medical-reports.index') }}">@lang('clinicCore::lang.medical_reports')</a></li>
        <li class="active">{{ isset($report) ? 'Edit' : 'New' }}</li>
    </ol>
</section>

<section class="content">
    <form method="POST"
          action="{{ isset($report) ? route('clinic.medical-reports.update', $report->id) : route('clinic.medical-reports.store') }}">
        @csrf
        @if(isset($report)) @method('PUT') @endif

        <div class="row">
            <div class="col-md-8">
                @component('components.widget', ['class' => 'box-primary', 'title' => 'Report Details'])
                    <div class="row">
                        @if(!isset($report))
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('clinicCore::lang.pet_owner') <span class="text-danger">*</span></label>
                                <select name="contact_id" id="mr_contact_id" class="form-control select2-owner" style="width:100%" required>
                                    @if(request('contact_id'))
                                        <option value="{{ request('contact_id') }}" selected>{{ optional(\App\Contact::find(request('contact_id')))->name }}</option>
                                    @endif
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Patient <span class="text-danger">*</span></label>
                                <select name="patient_id" id="mr_patient_id" class="form-control select2" style="width:100%" required>
                                    @if(request('patient_id'))
                                        <option value="{{ request('patient_id') }}" selected>{{ optional(\Modules\ClinicCore\Entities\ClcPatient::find(request('patient_id')))->patient_name }}</option>
                                    @endif
                                </select>
                            </div>
                        </div>
                        @else
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Patient</label>
                                <input type="text" class="form-control" readonly value="{{ optional($report->pet)->patient_name }}">
                            </div>
                        </div>
                        @endif
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Report Type <span class="text-danger">*</span></label>
                                <select name="report_type" class="form-control" required>
                                    @foreach(['lab','xray','ultrasound','ecg','other'] as $type)
                                    <option value="{{ $type }}" {{ old('report_type', isset($report) ? $report->report_type : '') == $type ? 'selected' : '' }}>
                                        {{ ucfirst($type) }}
                                    </option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('clinicCore::lang.doctor')</label>
                                <select name="doctor_id" class="form-control select2" style="width:100%">
                                    <option value="">— None —</option>
                                    @foreach($doctors as $doc)
                                    <option value="{{ $doc->id }}" {{ old('doctor_id', isset($report) ? $report->doctor_id : '') == $doc->id ? 'selected' : '' }}>
                                        {{ $doc->name }}
                                    </option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Report Title <span class="text-danger">*</span></label>
                        <input type="text" name="report_title" class="form-control" required
                            value="{{ old('report_title', isset($report) ? $report->report_title : '') }}"
                            placeholder="e.g. CBC Blood Test Results, Chest X-Ray">
                    </div>

                    @if(request('appointment_id'))
                    <input type="hidden" name="appointment_id" value="{{ request('appointment_id') }}">
                    @endif

                    <div class="form-group">
                        <label>Findings / Results</label>
                        <textarea name="findings" class="form-control" rows="6"
                            placeholder="Describe findings, test results, observations...">{{ old('findings', isset($report) ? $report->findings : '') }}</textarea>
                    </div>
                @endcomponent
            </div>

            <div class="col-md-4">
                <div class="form-group" style="margin-top:20px;">
                    <button type="submit" class="btn btn-primary btn-block">
                        <i class="fa fa-save"></i>
                        {{ isset($report) ? __('clinicCore::lang.update') : __('clinicCore::lang.save') }}
                    </button>
                    <a href="{{ route('clinic.medical-reports.index') }}" class="btn btn-default btn-block">@lang('clinicCore::lang.cancel')</a>
                </div>
            </div>
        </div>
    </form>
</section>
@endsection

@section('javascript')
<script>
$('.select2').select2({ width: '100%' });

$('#mr_contact_id').select2({
    width: '100%', minimumInputLength: 2,
    ajax: {
        url: '{{ route('clinic.patient-contacts.search') }}',
        dataType: 'json', delay: 300,
        data: function (p) { return { q: p.term }; },
        processResults: function (d) { return { results: d }; }
    },
    placeholder: 'Search owner...'
}).on('select2:select', function (e) {
    var contactId = e.params.data.id;
    $('#mr_patient_id').empty().append('<option value="">Loading...</option>');
    $.get('{{ url('clinic/patients/by-owner') }}/' + contactId, function (data) {
        $('#mr_patient_id').empty().append('<option value="">— Select Patient —</option>');
        $.each(data, function (i, patient) {
            $('#mr_patient_id').append('<option value="' + pet.id + '">' + pet.text + '</option>');
        });
        $('#mr_patient_id').trigger('change');
    });
});
</script>
@endsection
