{{--
    SIFA P3-T2 / P3-T3 / P3-T4 — encounter panel for the consult screen.
    Self-contained: renders the encounter meta selects, check-in + follow-up
    actions, the structured diagnoses list (assessment area), SOAP sign/lock +
    amend controls, typed clinical notes, the extended vitals capture (BP / SpO2
    / pain / glucose / GCS) with an optional AiCore dictation mic, and the
    vitals-trend link. Every action is an AJAX POST to the P3 route block.

    Expects: $appointment (ClcAppointment, with patient_id + relations loaded).
--}}
@php
    $encUser        = auth()->user();
    $canDiagnose    = $encUser && $encUser->can('clinic.encounter.diagnose');
    $canSign        = $encUser && $encUser->can('clinic.note.sign');
    $canAmend       = $encUser && $encUser->can('clinic.note.amend');
    $canVitals      = $encUser && ($encUser->can('clinic.vitals.record') || $encUser->can('clinic.manage_appointments'));
    $canTrends      = $encUser && ($encUser->can('clinic.vitals.view_trends') || $encUser->can('clinic.view_appointments'));
    $encManage      = $encUser && $encUser->can('clinic.manage_appointments');

    $encTypes       = \Modules\ClinicCore\Entities\ClcAppointment::ENCOUNTER_TYPES;
    $encPriorities  = \Modules\ClinicCore\Entities\ClcAppointment::PRIORITIES;
    $encDiagnoses   = $appointment->relationLoaded('diagnoses') ? $appointment->diagnoses : $appointment->diagnoses()->get();
    $encAmendCount  = $appointment->noteAmendments()->count();
    $encSoapLocked  = $appointment->isSoapLocked();

    // AiCore dictation availability (module installed + a server-side driver).
    $encDictate = false;
    try {
        $mu = app(\App\Utils\ModuleUtil::class);
        if ($mu->isModuleInstalled('AiCore') && class_exists('\Modules\AiCore\Entities\AiProvider')) {
            $ap = \Modules\AiCore\Entities\AiProvider::forBusiness((int) session('user.business_id'));
            $tc = method_exists($ap, 'transcriptionConfig') ? $ap->transcriptionConfig() : [];
            $encDictate = !empty($tc) && (($tc['provider'] ?? 'webspeech') !== 'webspeech');
        }
    } catch (\Throwable $e) { $encDictate = false; }

    // Follow-up create link — reuse the existing appointment-create flow with
    // query params (no new form). parent_appointment_id links the two.
    $followUpUrl = route('clinic.appointments.create', [
        'patient_id'            => $appointment->patient_id,
        'contact_id'            => $appointment->contact_id,
        'doctor_id'             => $appointment->doctor_id,
        'parent_appointment_id' => $appointment->id,
    ]);
@endphp

<style>
    .enc-card{background:#fff;border:1px solid #e8ecf0;border-radius:8px;padding:14px 16px;margin-bottom:14px;}
    .enc-card h4{font-size:12px;font-weight:700;text-transform:uppercase;letter-spacing:.5px;color:#64748b;margin:0 0 10px;}
    .enc-row{display:flex;flex-wrap:wrap;gap:10px;align-items:flex-end;}
    .enc-field{flex:1;min-width:140px;}
    .enc-field label{font-size:11px;color:#94a3b8;display:block;margin-bottom:3px;}
    .enc-diag-table td{vertical-align:middle;}
    .enc-sev-critical{color:#dc2626;font-weight:700;}
    .enc-pill{display:inline-block;font-size:10px;padding:1px 7px;border-radius:10px;background:#eef2ff;color:#4f46e5;}
    .enc-lock-note{font-size:11px;color:#b45309;background:#fffbeb;border:1px solid #fde68a;border-radius:6px;padding:6px 10px;}
</style>

{{-- Alert banner at the very top of the consult screen (Wave A). --}}
@include('clinicCore::partials.patient_alert_banner', ['patientId' => $appointment->patient_id])

{{-- ── P3-T2 · Encounter meta + check-in + follow-up ─────────────────── --}}
<div class="enc-card" id="enc_meta_card" data-appt="{{ $appointment->id }}">
    <h4><i class="fa fa-stethoscope"></i> {{ __('clinicCore::lang.encounter') }}</h4>
    <div class="enc-row">
        <div class="enc-field">
            <label>{{ __('clinicCore::lang.encounter_type') }}</label>
            <select id="enc_type" class="form-control input-sm" {{ $encManage ? '' : 'disabled' }}>
                <option value="">—</option>
                @foreach($encTypes as $t)
                    <option value="{{ $t }}" @selected($appointment->encounter_type === $t)>{{ __('clinicCore::lang.enc_'.$t) }}</option>
                @endforeach
            </select>
        </div>
        <div class="enc-field">
            <label>{{ __('clinicCore::lang.priority') }}</label>
            <select id="enc_priority" class="form-control input-sm" {{ $encManage ? '' : 'disabled' }}>
                <option value="">—</option>
                @foreach($encPriorities as $p)
                    <option value="{{ $p }}" @selected($appointment->priority === $p)>{{ __('clinicCore::lang.prio_'.$p) }}</option>
                @endforeach
            </select>
        </div>
        <div class="enc-field" style="flex:0 0 auto;">
            @if($encManage)
                <button type="button" id="enc_checkin_btn" class="btn btn-sm btn-default"
                    style="{{ $appointment->check_in_time ? 'display:none;' : '' }}">
                    <i class="fa fa-sign-in"></i> {{ __('clinicCore::lang.check_in') }}
                </button>
            @endif
            <span id="enc_checkin_stamp" class="text-muted" style="font-size:11px;{{ $appointment->check_in_time ? '' : 'display:none;' }}">
                <i class="fa fa-check-circle text-success"></i>
                {{ __('clinicCore::lang.checked_in') }}: <span id="enc_checkin_time">{{ optional($appointment->check_in_time)->format('d M Y H:i') }}</span>
            </span>
        </div>
        <div class="enc-field" style="flex:0 0 auto;">
            @if($encManage)
                <a href="{{ $followUpUrl }}" class="btn btn-sm btn-link">
                    <i class="fa fa-calendar-plus-o"></i> {{ __('clinicCore::lang.create_follow_up') }}
                </a>
            @endif
        </div>
    </div>
    @if($appointment->parent_appointment_id)
        <div style="margin-top:8px;font-size:11px;">
            <span class="enc-pill"><i class="fa fa-link"></i> {{ __('clinicCore::lang.follow_up_of') }}
                <a href="{{ route('clinic.appointments.show', $appointment->parent_appointment_id) }}">#{{ $appointment->parent_appointment_id }}</a>
            </span>
        </div>
    @endif
</div>

{{-- ── P3-T2 · Encounter diagnoses (assessment area) ─────────────────── --}}
<div class="enc-card" id="enc_diag_card">
    <h4><i class="fa fa-notes-medical"></i> {{ __('clinicCore::lang.diagnoses') }}</h4>
    <div class="table-responsive">
        <table class="table table-condensed enc-diag-table" style="font-size:12px;margin-bottom:8px;">
            <thead><tr>
                <th>{{ __('clinicCore::lang.icd_code') }}</th>
                <th>{{ __('clinicCore::lang.description') }}</th>
                <th>{{ __('clinicCore::lang.type') }}</th>
                <th>{{ __('clinicCore::lang.confidence') }}</th>
                <th style="width:120px;"></th>
            </tr></thead>
            <tbody id="enc_diag_body">
            @forelse($encDiagnoses as $d)
                <tr data-id="{{ $d->id }}">
                    <td>{{ $d->icd_code ?: '—' }}</td>
                    <td>{{ $d->description }}</td>
                    <td><span class="enc-pill">{{ __('clinicCore::lang.dx_'.$d->diagnosis_type) }}</span></td>
                    <td>{{ __('clinicCore::lang.cf_'.$d->confidence) }}</td>
                    <td>
                        @if($canDiagnose)
                            <button class="btn btn-xs btn-link enc-diag-problem" title="{{ __('clinicCore::lang.add_to_problem_list') }}"><i class="fa fa-list-ul"></i></button>
                            <button class="btn btn-xs btn-link text-danger enc-diag-del" title="{{ __('clinicCore::lang.delete') }}"><i class="fa fa-trash"></i></button>
                        @endif
                    </td>
                </tr>
            @empty
                <tr id="enc_diag_empty"><td colspan="5" class="text-muted">{{ __('clinicCore::lang.no_diagnoses') }}</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($canDiagnose)
    <div class="enc-row">
        <div class="enc-field" style="min-width:100px;flex:0 0 110px;">
            <input type="text" id="enc_dx_icd" class="form-control input-sm" placeholder="{{ __('clinicCore::lang.icd_code') }}">
        </div>
        <div class="enc-field" style="flex:2;">
            <input type="text" id="enc_dx_desc" class="form-control input-sm" placeholder="{{ __('clinicCore::lang.description') }}">
        </div>
        <div class="enc-field" style="flex:0 0 130px;">
            <select id="enc_dx_type" class="form-control input-sm">
                <option value="primary">{{ __('clinicCore::lang.dx_primary') }}</option>
                <option value="secondary">{{ __('clinicCore::lang.dx_secondary') }}</option>
                <option value="differential">{{ __('clinicCore::lang.dx_differential') }}</option>
            </select>
        </div>
        <div class="enc-field" style="flex:0 0 130px;">
            <select id="enc_dx_conf" class="form-control input-sm">
                <option value="confirmed">{{ __('clinicCore::lang.cf_confirmed') }}</option>
                <option value="probable">{{ __('clinicCore::lang.cf_probable') }}</option>
                <option value="possible">{{ __('clinicCore::lang.cf_possible') }}</option>
            </select>
        </div>
        <div class="enc-field" style="flex:0 0 auto;">
            <button type="button" id="enc_dx_add" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> {{ __('clinicCore::lang.add') }}</button>
        </div>
    </div>
    @endif
</div>

{{-- ── P3-T4 · Extended vitals capture ──────────────────────────────────
     REMOVED (duplicate). The legacy vitals grid in appointments/show.blade.php
     is the single vitals-entry surface — it saves through saveConsultation
     (range warnings + no_vitals 422 guard) and now also upserts a clc_vitals
     trend row on each consultation save, so the Wave-B trend table stays fed
     without a second on-screen vitals card. The "Vitals trend" link moved into
     the legacy vitals section header. --}}

{{-- ── P3-T3 · SOAP sign-off / lock + amendment history ─────────────────
     Acts on the legacy SOAP note below: signing locks S/O/A/P read-only
     (edits then go through the amendment workflow). No note textarea here —
     the legacy SOAP S/O/A/P cards are the single note-entry surface. --}}
<div class="enc-card" id="enc_sign_card">
    <h4><i class="fa fa-pencil-square-o"></i> {{ __('clinicCore::lang.encounter_signoff') }}</h4>
    <div style="display:flex;flex-wrap:wrap;align-items:center;gap:10px;">
        <div id="enc_sign_state" style="font-size:12px;">
            @if($encSoapLocked)
                <span class="text-success"><i class="fa fa-lock"></i>
                    {{ __('clinicCore::lang.note_signed') }}
                    @if($appointment->soap_signed_at) — {{ $appointment->soap_signed_at->format('d M Y H:i') }} @endif
                </span>
                @if($appointment->soap_cosigned_by)
                    <span class="text-muted">· {{ __('clinicCore::lang.cosigned') }}</span>
                @endif
            @else
                <span class="text-muted"><i class="fa fa-unlock-alt"></i> {{ __('clinicCore::lang.note_unsigned') }}</span>
            @endif
        </div>
        <div style="flex:1;"></div>
        @if($canSign)
            <button type="button" id="enc_sign_btn" class="btn btn-sm btn-success">
                <i class="fa fa-check"></i>
                {{ $encSoapLocked ? __('clinicCore::lang.cosign_note') : __('clinicCore::lang.sign_note') }}
            </button>
        @endif
        @if($canAmend && $encSoapLocked)
            <button type="button" id="enc_amend_btn" class="btn btn-sm btn-warning" data-toggle="modal" data-target="#enc_amend_modal">
                <i class="fa fa-edit"></i> {{ __('clinicCore::lang.amend') }}
            </button>
        @endif
        <button type="button" id="enc_amend_history_btn" class="btn btn-sm btn-default">
            <i class="fa fa-history"></i> {{ __('clinicCore::lang.amendments') }} (<span id="enc_amend_count">{{ $encAmendCount }}</span>)
        </button>
    </div>
    @if($encSoapLocked)
        <div class="enc-lock-note" style="margin-top:8px;"><i class="fa fa-info-circle"></i> {{ __('clinicCore::lang.soap_locked_hint') }}</div>
    @endif
    <div id="enc_amend_list" style="display:none;margin-top:10px;"></div>
</div>

{{-- ── P3-T3 · Typed clinical notes ─────────────────────────────────── --}}
<div class="enc-card" id="enc_typed_note_card">
    <h4><i class="fa fa-sticky-note-o"></i> {{ __('clinicCore::lang.typed_notes') }}</h4>
    <div id="enc_typed_note_list" style="margin-bottom:10px;">
        @foreach($appointment->appointmentNotes as $n)
            @if(!empty($n->note_type))
            <div style="border:1px solid #eef;border-radius:6px;padding:8px 10px;margin-bottom:6px;font-size:12px;">
                <span class="enc-pill">{{ __('clinicCore::lang.nt_'.$n->note_type) }}</span>
                @if($n->title)<strong>{{ $n->title }}</strong>@endif
                <span class="text-muted" style="float:right;font-size:11px;">{{ optional($n->created_at)->format('d M Y H:i') }}</span>
                <div style="margin-top:4px;">{{ $n->notes }}</div>
            </div>
            @endif
        @endforeach
    </div>
    <div class="enc-row">
        <div class="enc-field" style="flex:0 0 150px;">
            <select id="enc_note_type" class="form-control input-sm">
                @foreach(\Modules\ClinicCore\Entities\ClcAppointmentNote::NOTE_TYPES as $nt)
                    <option value="{{ $nt }}">{{ __('clinicCore::lang.nt_'.$nt) }}</option>
                @endforeach
            </select>
        </div>
        <div class="enc-field" style="flex:0 0 160px;">
            <input type="text" id="enc_note_title" class="form-control input-sm" placeholder="{{ __('clinicCore::lang.title') }}">
        </div>
        <div class="enc-field" style="flex:2;">
            <input type="text" id="enc_note_body" class="form-control input-sm" placeholder="{{ __('clinicCore::lang.notes') }}">
        </div>
        <div class="enc-field" style="flex:0 0 auto;">
            <button type="button" id="enc_note_add" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> {{ __('clinicCore::lang.add') }}</button>
        </div>
    </div>
</div>

{{-- Amend modal --}}
@if($canAmend)
<div class="modal fade" id="enc_amend_modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document"><div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title"><i class="fa fa-edit"></i> {{ __('clinicCore::lang.amend_note') }}</h4>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <label>{{ __('clinicCore::lang.section') }}</label>
                <select id="enc_amend_section" class="form-control">
                    <option value="subjective">S — {{ __('clinicCore::lang.subjective') }}</option>
                    <option value="objective">O — {{ __('clinicCore::lang.objective') }}</option>
                    <option value="assessment">A — {{ __('clinicCore::lang.assessment') }}</option>
                    <option value="plan">P — {{ __('clinicCore::lang.plan') }}</option>
                    <option value="investigations">{{ __('clinicCore::lang.investigations') }}</option>
                    <option value="other">{{ __('clinicCore::lang.other') }}</option>
                </select>
            </div>
            <div class="form-group">
                <label>{{ __('clinicCore::lang.new_text') }}</label>
                <textarea id="enc_amend_text" class="form-control" rows="4"></textarea>
            </div>
            <div class="form-group">
                <label>{{ __('clinicCore::lang.reason') }}</label>
                <input type="text" id="enc_amend_reason" class="form-control">
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">{{ __('clinicCore::lang.cancel') }}</button>
            <button type="button" id="enc_amend_save" class="btn btn-warning">{{ __('clinicCore::lang.save_amendment') }}</button>
        </div>
    </div></div>
</div>
@endif

{{-- ── P3-T6 · Unified clinical orders (lab / imaging / procedure) ────── --}}
@php
    $canOrderView   = $encUser && ($encUser->can('clinic.order.view') || $encUser->can('clinic.order.create'));
    $canOrderCreate = $encUser && $encUser->can('clinic.order.create');
    $encOrders = $canOrderView
        ? \Modules\ClinicCore\Entities\ClcOrder::with('items')
            ->where('business_id', (int) session('user.business_id'))
            ->where('appointment_id', $appointment->id)
            ->orderByDesc('id')->get()
        : collect();
@endphp
@if($canOrderView)
<div class="enc-card" id="enc_orders_card" data-appt="{{ $appointment->id }}" data-patient="{{ (int) $appointment->patient_id }}">
    <h4>
        <i class="fa fa-clipboard-list"></i> {{ __('clinicCore::lang.orders') }}
        @if($canOrderCreate)
            <button type="button" id="enc_order_add_btn" class="btn btn-xs btn-primary" style="float:right;">
                <i class="fa fa-plus"></i> {{ __('clinicCore::lang.add_order') }}
            </button>
        @endif
    </h4>
    <div class="table-responsive">
        <table class="table table-condensed" style="font-size:12px;margin-bottom:8px;">
            <thead><tr>
                <th>{{ __('clinicCore::lang.order_no') }}</th>
                <th>{{ __('clinicCore::lang.order_type') }}</th>
                <th>{{ __('clinicCore::lang.order_priority') }}</th>
                <th>{{ __('clinicCore::lang.status') }}</th>
                <th>{{ __('clinicCore::lang.service_name') }}</th>
            </tr></thead>
            <tbody id="enc_orders_body">
            @forelse($encOrders as $o)
                <tr>
                    <td><a href="{{ route('clinic.orders.print', $o->id) }}" target="_blank" style="font-family:monospace;font-weight:700;">{{ $o->order_no }}</a></td>
                    <td>{{ __('clinicCore::lang.order_type_'.$o->order_type) }}</td>
                    <td>{{ ucfirst($o->priority) }}</td>
                    <td>{{ ucfirst(str_replace('_',' ', $o->status)) }}</td>
                    <td>{{ $o->items->pluck('service_name')->implode(', ') }}</td>
                </tr>
            @empty
                <tr id="enc_orders_empty"><td colspan="5" class="text-muted">{{ __('clinicCore::lang.no_orders') }}</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
</div>

@if($canOrderCreate)
{{-- Add-order modal (encounter-scoped: patient is pre-bound to this encounter) --}}
<div class="modal fade" id="enc_order_modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document"><div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><i class="fa fa-plus"></i> {{ __('clinicCore::lang.new_order') }}</h4>
    </div>
    <div class="modal-body">
        <div class="enc-row">
            <div class="enc-field">
                <label>{{ __('clinicCore::lang.order_type') }}</label>
                <select id="enc_ord_type" class="form-control input-sm">
                    @foreach(['lab','imaging','procedure','consultation'] as $t)
                    <option value="{{ $t }}">{{ __('clinicCore::lang.order_type_'.$t) }}</option>
                    @endforeach
                </select>
            </div>
            <div class="enc-field">
                <label>{{ __('clinicCore::lang.order_priority') }}</label>
                <select id="enc_ord_priority" class="form-control input-sm">
                    @foreach(['routine','urgent','stat'] as $p)
                    <option value="{{ $p }}">{{ ucfirst($p) }}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="form-group" style="margin-top:8px;">
            <label style="font-size:11px;color:#94a3b8;">{{ __('clinicCore::lang.clinical_indication') }}</label>
            <textarea id="enc_ord_indication" class="form-control input-sm" rows="2"></textarea>
        </div>
        <table class="table table-condensed table-bordered" style="font-size:12px;">
            <thead><tr>
                <th>{{ __('clinicCore::lang.service_name') }}</th>
                <th style="width:130px;">{{ __('clinicCore::lang.laterality') }}</th>
                <th style="width:34px;"></th>
            </tr></thead>
            <tbody id="enc_ord_items"></tbody>
        </table>
        <button type="button" class="btn btn-default btn-xs" id="enc_ord_add_item"><i class="fa fa-plus"></i> {{ __('clinicCore::lang.add') }}</button>
        <div id="enc_ord_warnings" style="margin-top:8px;font-size:11px;"></div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">{{ __('clinicCore::lang.cancel') }}</button>
        <button type="button" class="btn btn-primary" id="enc_ord_save"><i class="fa fa-save"></i> {{ __('clinicCore::lang.save') }}</button>
    </div>
  </div></div>
</div>

@once
<script>
window.addEventListener('load', function () {
    if (typeof $ === 'undefined') { return; }
    var ETOKEN = '{{ csrf_token() }}';
    var etoast = (window.toastr) ? window.toastr : { success:function(){}, error:function(m){alert(m);}, warning:function(m){alert(m);} };

    function encOrdItemRow() {
        return '<tr class="enc-ord-item">'
            + '<td><input type="text" class="form-control input-sm eoi-svc" placeholder="{{ __('clinicCore::lang.service_name') }}"></td>'
            + '<td><select class="form-control input-sm eoi-lat">'
            +   '<option value="na">{{ __('clinicCore::lang.lat_na') }}</option>'
            +   '<option value="left">{{ __('clinicCore::lang.lat_left') }}</option>'
            +   '<option value="right">{{ __('clinicCore::lang.lat_right') }}</option>'
            +   '<option value="bilateral">{{ __('clinicCore::lang.lat_bilateral') }}</option>'
            + '</select></td>'
            + '<td><button type="button" class="btn btn-danger btn-xs eoi-del"><i class="fa fa-trash"></i></button></td>'
            + '</tr>';
    }

    $('#enc_order_add_btn').on('click', function () {
        $('#enc_ord_indication').val('');
        $('#enc_ord_warnings').html('');
        $('#enc_ord_items').html(encOrdItemRow());
        $('#enc_order_modal').modal('show');
    });
    $('#enc_ord_add_item').on('click', function () { $('#enc_ord_items').append(encOrdItemRow()); });
    $(document).on('click', '.eoi-del', function () { $(this).closest('tr').remove(); });

    $('#enc_ord_save').on('click', function () {
        var patientId = $('#enc_orders_card').data('patient');
        var apptId    = $('#enc_orders_card').data('appt');
        var items = [];
        $('#enc_ord_items tr.enc-ord-item').each(function () {
            var svc = ($(this).find('.eoi-svc').val() || '').trim();
            if (!svc) return;
            items.push({ service_name: svc, laterality: $(this).find('.eoi-lat').val(), quantity: 1 });
        });
        if (!items.length) { etoast.warning('Add at least one service.'); return; }

        var $btn = $(this).prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i>');
        $.post('{{ route('clinic.orders.store') }}', {
            _token: ETOKEN,
            patient_id: patientId,
            appointment_id: apptId,
            order_type: $('#enc_ord_type').val(),
            priority: $('#enc_ord_priority').val(),
            clinical_indication: $('#enc_ord_indication').val(),
            items: items
        }).done(function (res) {
            if (res.success) {
                etoast.success(res.msg);
                var svcNames = items.map(function (i) { return i.service_name; }).join(', ');
                var esc = function (s) { return $('<div>').text(s).html(); };
                var row = '<tr><td><a href="{{ url('clinic/orders') }}/' + res.order_id + '/print" target="_blank" style="font-family:monospace;font-weight:700;">' + esc(res.order_no) + '</a></td>'
                    + '<td>' + esc($('#enc_ord_type option:selected').text()) + '</td>'
                    + '<td>' + esc($('#enc_ord_priority').val()) + '</td>'
                    + '<td>ordered</td><td>' + esc(svcNames) + '</td></tr>';
                $('#enc_orders_empty').remove();
                $('#enc_orders_body').prepend(row);
                if (res.warnings && res.warnings.length) {
                    var w = '';
                    res.warnings.forEach(function (m) { w += '<div class="text-warning"><i class="fa fa-exclamation-triangle"></i> ' + $('<div>').text(m).html() + '</div>'; });
                    $('#enc_ord_warnings').html(w);
                    setTimeout(function () { $('#enc_order_modal').modal('hide'); }, 1200);
                } else {
                    $('#enc_order_modal').modal('hide');
                }
            }
        }).fail(function (xhr) {
            etoast.error((xhr.responseJSON && xhr.responseJSON.msg) || 'Could not place order.');
        }).always(function () {
            $btn.prop('disabled', false).html('<i class="fa fa-save"></i> {{ __('clinicCore::lang.save') }}');
        });
    });
});
</script>
@endonce
@endif
@endif

@include('clinicCore::partials.encounter_panel_js', [
    'appointment' => $appointment,
    'encDictate'  => $encDictate,
])
