{{--
    SIFA P3-T1 â€” structured clinical baseline for a patient: allergies, current
    (home) medications, and insurance cards. Self-contained partial (style +
    markup + modals + JS) matching the _plans_tab / _photos_tab include pattern.
    All CRUD is AJAX to the P3 routes; gated by clinic.allergy.manage.
--}}
@php $clcCanManageBaseline = auth()->user()->can('clinic.allergy.manage'); @endphp
<style>
  .bl-section { margin-bottom:26px; }
  .bl-head { display:flex; align-items:center; gap:8px; margin-bottom:10px; }
  .bl-head h4 { margin:0; font-size:14px; font-weight:700; color:#1e293b; }
  .bl-head .bl-count { font-size:11px; color:#94a3b8; }
  .bl-head .btn { margin-left:auto; }
  .bl-card { display:flex; align-items:flex-start; gap:10px; border:1px solid #e2e8f0; border-left:4px solid #cbd5e1; border-radius:8px; padding:10px 12px; margin-bottom:8px; background:#fff; }
  .bl-card.sev-severe, .bl-card.sev-life_threatening { border-left-color:#e74c3c; }
  .bl-card.sev-moderate { border-left-color:#f39c12; }
  .bl-card.sev-mild { border-left-color:#3498db; }
  .bl-card .bl-body { flex:1; min-width:0; }
  .bl-title { font-weight:700; font-size:13px; color:#1e293b; }
  .bl-meta { font-size:11px; color:#64748b; display:flex; flex-wrap:wrap; gap:10px; margin-top:3px; }
  .bl-actions { display:flex; flex-direction:column; gap:4px; flex-shrink:0; }
  .bl-empty { text-align:center; color:#94a3b8; padding:22px; background:#f8fafc; border:2px dashed #e2e8f0; border-radius:8px; font-size:12px; }
  .bl-pill { font-size:10px; font-weight:700; padding:1px 7px; border-radius:20px; color:#fff; text-transform:uppercase; }
</style>

{{-- â•â• ALLERGIES â•â• --}}
<div class="bl-section" id="bl-allergies">
    <div class="bl-head">
        <i class="fas fa-allergies text-danger"></i>
        <h4>@lang('clinicCore::lang.allergies_structured')</h4>
        <span class="bl-count">({{ $patient->structuredAllergies->count() }})</span>
        @if($clcCanManageBaseline)
        <button type="button" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#blAllergyModal" id="bl_allergy_new">
            <i class="fa fa-plus"></i> @lang('clinicCore::lang.add')
        </button>
        @endif
    </div>
    @if($patient->structuredAllergies->isEmpty())
        <div class="bl-empty"><i class="fa fa-info-circle"></i> @lang('clinicCore::lang.no_allergies')</div>
    @else
        @foreach($patient->structuredAllergies as $al)
        <div class="bl-card sev-{{ $al->severity }}">
            <div class="bl-body">
                <div class="bl-title">
                    {{ $al->allergen }}
                    <span class="bl-pill" style="background:{{ in_array($al->severity,['severe','life_threatening'])?'#e74c3c':($al->severity==='moderate'?'#f39c12':'#3498db') }};">
                        {{ __('clinicCore::lang.severity_' . $al->severity) }}
                    </span>
                    @if($al->status !== 'active')<span class="text-muted" style="font-size:10px;">({{ __('clinicCore::lang.inactive') }})</span>@endif
                </div>
                <div class="bl-meta">
                    <span><i class="fa fa-tag"></i> {{ __('clinicCore::lang.allergy_type_' . $al->type) }}</span>
                    @if($al->reaction)<span><i class="fa fa-bolt"></i> {{ $al->reaction }}</span>@endif
                    <span><i class="fa fa-user-check"></i> {{ __('clinicCore::lang.allergy_source_' . $al->source) }}</span>
                    @if($al->verified_at)<span class="text-success"><i class="fa fa-check-circle"></i> @lang('clinicCore::lang.verified')</span>@endif
                </div>
            </div>
            @if($clcCanManageBaseline)
            <div class="bl-actions">
                <button class="btn btn-xs btn-default bl-allergy-edit"
                    data-id="{{ $al->id }}" data-allergen="{{ e($al->allergen) }}" data-type="{{ $al->type }}"
                    data-severity="{{ $al->severity }}" data-reaction="{{ e($al->reaction) }}"
                    data-source="{{ $al->source }}" data-status="{{ $al->status }}" data-notes="{{ e($al->notes) }}"><i class="fa fa-edit"></i></button>
                <button class="btn btn-xs btn-danger bl-allergy-del" data-id="{{ $al->id }}"><i class="fa fa-trash"></i></button>
            </div>
            @endif
        </div>
        @endforeach
    @endif
</div>

{{-- â•â• CURRENT MEDICATIONS â•â• --}}
<div class="bl-section" id="bl-medications">
    <div class="bl-head">
        <i class="fas fa-pills text-primary"></i>
        <h4>@lang('clinicCore::lang.current_medications')</h4>
        <span class="bl-count">({{ $patient->medications->count() }})</span>
        @if($clcCanManageBaseline)
        <button type="button" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#blMedModal" id="bl_med_new">
            <i class="fa fa-plus"></i> @lang('clinicCore::lang.add')
        </button>
        @endif
    </div>
    <p class="text-muted" style="font-size:11px;margin:-4px 0 8px;"><i class="fa fa-info-circle"></i> @lang('clinicCore::lang.home_meds_hint')</p>
    @if($patient->medications->isEmpty())
        <div class="bl-empty"><i class="fa fa-info-circle"></i> @lang('clinicCore::lang.no_medications')</div>
    @else
        @foreach($patient->medications as $md)
        <div class="bl-card {{ $md->ongoing ? '' : 'sev-mild' }}">
            <div class="bl-body">
                <div class="bl-title">{{ $md->medication_name }}
                    @if($md->ongoing)<span class="bl-pill" style="background:#22c55e;">@lang('clinicCore::lang.ongoing')</span>@endif
                </div>
                <div class="bl-meta">
                    @if($md->dosage)<span><i class="fa fa-prescription-bottle"></i> {{ $md->dosage }}</span>@endif
                    @if($md->frequency)<span><i class="fa fa-clock"></i> {{ $md->frequency }}</span>@endif
                    @if($md->route)<span><i class="fa fa-route"></i> {{ $md->route }}</span>@endif
                    @if($md->prescriber)<span><i class="fa fa-user-md"></i> {{ $md->prescriber }}</span>@endif
                    @if($md->start_date)<span><i class="fa fa-calendar"></i> {{ $md->start_date->format('d-m-Y') }}@if($md->end_date) â†’ {{ $md->end_date->format('d-m-Y') }}@endif</span>@endif
                </div>
            </div>
            @if($clcCanManageBaseline)
            <div class="bl-actions">
                <button class="btn btn-xs btn-default bl-med-edit"
                    data-id="{{ $md->id }}" data-name="{{ e($md->medication_name) }}" data-dosage="{{ e($md->dosage) }}"
                    data-frequency="{{ e($md->frequency) }}" data-route="{{ e($md->route) }}" data-prescriber="{{ e($md->prescriber) }}"
                    data-start="{{ optional($md->start_date)->format('Y-m-d') }}" data-end="{{ optional($md->end_date)->format('Y-m-d') }}"
                    data-ongoing="{{ $md->ongoing ? 1 : 0 }}" data-notes="{{ e($md->notes) }}"><i class="fa fa-edit"></i></button>
                <button class="btn btn-xs btn-danger bl-med-del" data-id="{{ $md->id }}"><i class="fa fa-trash"></i></button>
            </div>
            @endif
        </div>
        @endforeach
    @endif
</div>

{{-- â•â• INSURANCE â•â• --}}
<div class="bl-section" id="bl-insurances">
    <div class="bl-head">
        <i class="fas fa-id-card text-success"></i>
        <h4>@lang('clinicCore::lang.insurance_cards')</h4>
        <span class="bl-count">({{ $patient->insurances->count() }})</span>
        @if($clcCanManageBaseline)
        <button type="button" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#blInsModal" id="bl_ins_new">
            <i class="fa fa-plus"></i> @lang('clinicCore::lang.add')
        </button>
        @endif
    </div>
    @if($patient->insurances->isEmpty())
        <div class="bl-empty"><i class="fa fa-info-circle"></i> @lang('clinicCore::lang.no_insurance')</div>
    @else
        @foreach($patient->insurances as $ins)
        <div class="bl-card {{ $ins->status !== 'active' ? 'sev-mild' : '' }}">
            <div class="bl-body">
                <div class="bl-title">{{ $ins->provider_name }}
                    <span class="bl-pill" style="background:{{ $ins->status==='active'?'#22c55e':'#94a3b8' }};">{{ __('clinicCore::lang.ins_status_' . $ins->status) }}</span>
                    @if($ins->pre_auth_required)<span class="bl-pill" style="background:#f39c12;">@lang('clinicCore::lang.pre_auth')</span>@endif
                </div>
                <div class="bl-meta">
                    @if($ins->policy_number)<span><i class="fa fa-hashtag"></i> {{ $ins->policy_number }}</span>@endif
                    @if($ins->group_number)<span><i class="fa fa-users"></i> {{ $ins->group_number }}</span>@endif
                    @if($ins->plan_type)<span><i class="fa fa-tag"></i> {{ $ins->plan_type }}</span>@endif
                    @if($ins->copay_amount !== null)<span><i class="fa fa-coins"></i> @lang('clinicCore::lang.copay'): {{ $ins->copay_amount }}</span>@endif
                    @if($ins->effective_date)<span><i class="fa fa-calendar"></i> {{ $ins->effective_date->format('d-m-Y') }}@if($ins->expiry_date) â†’ {{ $ins->expiry_date->format('d-m-Y') }}@endif</span>@endif
                </div>
            </div>
            @if($clcCanManageBaseline)
            <div class="bl-actions">
                <button class="btn btn-xs btn-default bl-ins-edit"
                    data-id="{{ $ins->id }}" data-provider="{{ e($ins->provider_name) }}" data-policy="{{ e($ins->policy_number) }}"
                    data-group="{{ e($ins->group_number) }}" data-plan="{{ e($ins->plan_type) }}"
                    data-effective="{{ optional($ins->effective_date)->format('Y-m-d') }}" data-expiry="{{ optional($ins->expiry_date)->format('Y-m-d') }}"
                    data-copay="{{ $ins->copay_amount }}" data-preauth="{{ $ins->pre_auth_required ? 1 : 0 }}"
                    data-status="{{ $ins->status }}" data-notes="{{ e($ins->notes) }}"><i class="fa fa-edit"></i></button>
                <button class="btn btn-xs btn-danger bl-ins-del" data-id="{{ $ins->id }}"><i class="fa fa-trash"></i></button>
            </div>
            @endif
        </div>
        @endforeach
    @endif
</div>

@if($clcCanManageBaseline)
{{-- â”€â”€ Allergy modal â”€â”€ --}}
<div class="modal fade" id="blAllergyModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
    <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><i class="fas fa-allergies"></i> <span id="bl_allergy_title">@lang('clinicCore::lang.add_allergy')</span></h4></div>
    <div class="modal-body">
        <input type="hidden" id="bl_allergy_id">
        <div class="form-group"><label>@lang('clinicCore::lang.allergen') <span class="text-danger">*</span></label>
            <input type="text" id="bl_allergy_allergen" class="form-control input-sm"></div>
        <div class="row">
            <div class="col-md-6"><div class="form-group"><label>@lang('clinicCore::lang.type')</label>
                <select id="bl_allergy_type" class="form-control input-sm">
                    @foreach(\Modules\ClinicCore\Entities\ClcPatientAllergy::TYPES as $t)
                        <option value="{{ $t }}">@lang('clinicCore::lang.allergy_type_' . $t)</option>
                    @endforeach
                </select></div></div>
            <div class="col-md-6"><div class="form-group"><label>@lang('clinicCore::lang.severity')</label>
                <select id="bl_allergy_severity" class="form-control input-sm">
                    @foreach(\Modules\ClinicCore\Entities\ClcPatientAllergy::SEVERITIES as $s)
                        <option value="{{ $s }}">@lang('clinicCore::lang.severity_' . $s)</option>
                    @endforeach
                </select></div></div>
        </div>
        <div class="form-group"><label>@lang('clinicCore::lang.reaction')</label>
            <input type="text" id="bl_allergy_reaction" class="form-control input-sm"></div>
        <div class="row">
            <div class="col-md-6"><div class="form-group"><label>@lang('clinicCore::lang.source')</label>
                <select id="bl_allergy_source" class="form-control input-sm">
                    @foreach(\Modules\ClinicCore\Entities\ClcPatientAllergy::SOURCES as $s)
                        <option value="{{ $s }}">@lang('clinicCore::lang.allergy_source_' . $s)</option>
                    @endforeach
                </select></div></div>
            <div class="col-md-6"><div class="form-group"><label>@lang('clinicCore::lang.status')</label>
                <select id="bl_allergy_status" class="form-control input-sm">
                    <option value="active">@lang('clinicCore::lang.active')</option>
                    <option value="inactive">@lang('clinicCore::lang.inactive')</option>
                </select></div></div>
        </div>
        <div class="form-group"><label>@lang('clinicCore::lang.notes')</label>
            <textarea id="bl_allergy_notes" class="form-control input-sm" rows="2"></textarea></div>
    </div>
    <div class="modal-footer"><button class="btn btn-default btn-sm" data-dismiss="modal">@lang('clinicCore::lang.cancel')</button>
        <button class="btn btn-primary btn-sm" id="bl_allergy_save"><i class="fa fa-save"></i> @lang('clinicCore::lang.save')</button></div>
</div></div></div>

{{-- â”€â”€ Medication modal â”€â”€ --}}
<div class="modal fade" id="blMedModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
    <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><i class="fas fa-pills"></i> <span id="bl_med_title">@lang('clinicCore::lang.add_medication')</span></h4></div>
    <div class="modal-body">
        <input type="hidden" id="bl_med_id">
        <div class="form-group"><label>@lang('clinicCore::lang.medication_name') <span class="text-danger">*</span></label>
            <input type="text" id="bl_med_name" class="form-control input-sm"></div>
        <div class="row">
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.dosage')</label>
                <input type="text" id="bl_med_dosage" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.frequency')</label>
                <input type="text" id="bl_med_frequency" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.route')</label>
                <input type="text" id="bl_med_route" class="form-control input-sm"></div></div>
        </div>
        <div class="row">
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.prescriber')</label>
                <input type="text" id="bl_med_prescriber" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.start_date')</label>
                <input type="date" id="bl_med_start" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.end_date')</label>
                <input type="date" id="bl_med_end" class="form-control input-sm"></div></div>
        </div>
        <div class="checkbox"><label><input type="checkbox" id="bl_med_ongoing" checked> @lang('clinicCore::lang.ongoing')</label></div>
        <div class="form-group"><label>@lang('clinicCore::lang.notes')</label>
            <textarea id="bl_med_notes" class="form-control input-sm" rows="2"></textarea></div>
    </div>
    <div class="modal-footer"><button class="btn btn-default btn-sm" data-dismiss="modal">@lang('clinicCore::lang.cancel')</button>
        <button class="btn btn-primary btn-sm" id="bl_med_save"><i class="fa fa-save"></i> @lang('clinicCore::lang.save')</button></div>
</div></div></div>

{{-- â”€â”€ Insurance modal â”€â”€ --}}
<div class="modal fade" id="blInsModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content">
    <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><i class="fas fa-id-card"></i> <span id="bl_ins_title">@lang('clinicCore::lang.add_insurance')</span></h4></div>
    <div class="modal-body">
        <input type="hidden" id="bl_ins_id">
        <div class="row">
            <div class="col-md-6"><div class="form-group"><label>@lang('clinicCore::lang.provider_name') <span class="text-danger">*</span></label>
                <input type="text" id="bl_ins_provider" class="form-control input-sm"></div></div>
            <div class="col-md-6"><div class="form-group"><label>@lang('clinicCore::lang.policy_number')</label>
                <input type="text" id="bl_ins_policy" class="form-control input-sm"></div></div>
        </div>
        <div class="row">
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.group_number')</label>
                <input type="text" id="bl_ins_group" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.plan_type')</label>
                <input type="text" id="bl_ins_plan" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.copay')</label>
                <input type="number" step="0.01" id="bl_ins_copay" class="form-control input-sm"></div></div>
        </div>
        <div class="row">
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.effective_date')</label>
                <input type="date" id="bl_ins_effective" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.expiry_date')</label>
                <input type="date" id="bl_ins_expiry" class="form-control input-sm"></div></div>
            <div class="col-md-4"><div class="form-group"><label>@lang('clinicCore::lang.status')</label>
                <select id="bl_ins_status" class="form-control input-sm">
                    @foreach(\Modules\ClinicCore\Entities\ClcPatientInsurance::STATUSES as $s)
                        <option value="{{ $s }}">@lang('clinicCore::lang.ins_status_' . $s)</option>
                    @endforeach
                </select></div></div>
        </div>
        <div class="checkbox"><label><input type="checkbox" id="bl_ins_preauth"> @lang('clinicCore::lang.pre_auth_required')</label></div>
        <div class="form-group"><label>@lang('clinicCore::lang.notes')</label>
            <textarea id="bl_ins_notes" class="form-control input-sm" rows="2"></textarea></div>
        <p class="text-muted" style="font-size:11px;"><i class="fa fa-info-circle"></i> @lang('clinicCore::lang.insurance_phase4_hint')</p>
    </div>
    <div class="modal-footer"><button class="btn btn-default btn-sm" data-dismiss="modal">@lang('clinicCore::lang.cancel')</button>
        <button class="btn btn-primary btn-sm" id="bl_ins_save"><i class="fa fa-save"></i> @lang('clinicCore::lang.save')</button></div>
</div></div></div>

<script>
(function initClcBaseline(){
    if (typeof window.jQuery === 'undefined') { return setTimeout(initClcBaseline, 50); }
    jQuery(function ($) {
        var token = $('meta[name="csrf-token"]').attr('content') || '{{ csrf_token() }}';
        var patientId = {{ (int) $patient->id }};

        function ok(res){ if (res && res.success){ if(window.toastr) toastr.success(res.msg); location.reload(); } }
        function err(){ if(window.toastr) toastr.error('Could not save. Check required fields.'); }

        // â”€â”€ Allergies â”€â”€
        $('#bl_allergy_new').on('click', function(){
            $('#bl_allergy_id').val(''); $('#bl_allergy_allergen,#bl_allergy_reaction,#bl_allergy_notes').val('');
            $('#bl_allergy_type,#bl_allergy_severity,#bl_allergy_source').prop('selectedIndex',0);
            $('#bl_allergy_status').val('active');
            $('#bl_allergy_title').text('{{ __('clinicCore::lang.add_allergy') }}');
        });
        $(document).on('click', '.bl-allergy-edit', function(){
            var d = $(this).data();
            $('#bl_allergy_id').val(d.id); $('#bl_allergy_allergen').val(d.allergen);
            $('#bl_allergy_type').val(d.type); $('#bl_allergy_severity').val(d.severity);
            $('#bl_allergy_reaction').val(d.reaction); $('#bl_allergy_source').val(d.source);
            $('#bl_allergy_status').val(d.status); $('#bl_allergy_notes').val(d.notes);
            $('#bl_allergy_title').text('{{ __('clinicCore::lang.edit') }}');
            $('#blAllergyModal').modal('show');
        });
        $('#bl_allergy_save').on('click', function(){
            var id = $('#bl_allergy_id').val();
            var url = id ? '{{ url('clinic/patients/allergies') }}/' + id + '/update'
                         : '{{ url('clinic/patients') }}/' + patientId + '/allergies';
            $.post(url, {
                _token: token,
                allergen: $('#bl_allergy_allergen').val(), type: $('#bl_allergy_type').val(),
                severity: $('#bl_allergy_severity').val(), reaction: $('#bl_allergy_reaction').val(),
                source: $('#bl_allergy_source').val(), status: $('#bl_allergy_status').val(),
                notes: $('#bl_allergy_notes').val()
            }).done(ok).fail(err);
        });
        $(document).on('click', '.bl-allergy-del', function(){
            if(!confirm('{{ __('clinicCore::lang.confirm_delete') }}')) return;
            $.ajax({ url: '{{ url('clinic/patients/allergies') }}/' + $(this).data('id'), method:'POST',
                data:{ _token: token, _method:'DELETE' } }).done(ok).fail(err);
        });

        // â”€â”€ Medications â”€â”€
        $('#bl_med_new').on('click', function(){
            $('#bl_med_id').val(''); $('#bl_med_name,#bl_med_dosage,#bl_med_frequency,#bl_med_route,#bl_med_prescriber,#bl_med_start,#bl_med_end,#bl_med_notes').val('');
            $('#bl_med_ongoing').prop('checked', true);
            $('#bl_med_title').text('{{ __('clinicCore::lang.add_medication') }}');
        });
        $(document).on('click', '.bl-med-edit', function(){
            var d = $(this).data();
            $('#bl_med_id').val(d.id); $('#bl_med_name').val(d.name); $('#bl_med_dosage').val(d.dosage);
            $('#bl_med_frequency').val(d.frequency); $('#bl_med_route').val(d.route); $('#bl_med_prescriber').val(d.prescriber);
            $('#bl_med_start').val(d.start); $('#bl_med_end').val(d.end);
            $('#bl_med_ongoing').prop('checked', String(d.ongoing) === '1');
            $('#bl_med_notes').val(d.notes);
            $('#bl_med_title').text('{{ __('clinicCore::lang.edit') }}');
            $('#blMedModal').modal('show');
        });
        $('#bl_med_save').on('click', function(){
            var id = $('#bl_med_id').val();
            var url = id ? '{{ url('clinic/patients/medications') }}/' + id + '/update'
                         : '{{ url('clinic/patients') }}/' + patientId + '/medications';
            $.post(url, {
                _token: token,
                medication_name: $('#bl_med_name').val(), dosage: $('#bl_med_dosage').val(),
                frequency: $('#bl_med_frequency').val(), route: $('#bl_med_route').val(),
                prescriber: $('#bl_med_prescriber').val(), start_date: $('#bl_med_start').val(),
                end_date: $('#bl_med_end').val(), ongoing: $('#bl_med_ongoing').is(':checked') ? 1 : 0,
                notes: $('#bl_med_notes').val()
            }).done(ok).fail(err);
        });
        $(document).on('click', '.bl-med-del', function(){
            if(!confirm('{{ __('clinicCore::lang.confirm_delete') }}')) return;
            $.ajax({ url: '{{ url('clinic/patients/medications') }}/' + $(this).data('id'), method:'POST',
                data:{ _token: token, _method:'DELETE' } }).done(ok).fail(err);
        });

        // â”€â”€ Insurances â”€â”€
        $('#bl_ins_new').on('click', function(){
            $('#bl_ins_id').val(''); $('#bl_ins_provider,#bl_ins_policy,#bl_ins_group,#bl_ins_plan,#bl_ins_copay,#bl_ins_effective,#bl_ins_expiry,#bl_ins_notes').val('');
            $('#bl_ins_status').val('active'); $('#bl_ins_preauth').prop('checked', false);
            $('#bl_ins_title').text('{{ __('clinicCore::lang.add_insurance') }}');
        });
        $(document).on('click', '.bl-ins-edit', function(){
            var d = $(this).data();
            $('#bl_ins_id').val(d.id); $('#bl_ins_provider').val(d.provider); $('#bl_ins_policy').val(d.policy);
            $('#bl_ins_group').val(d.group); $('#bl_ins_plan').val(d.plan); $('#bl_ins_copay').val(d.copay);
            $('#bl_ins_effective').val(d.effective); $('#bl_ins_expiry').val(d.expiry);
            $('#bl_ins_status').val(d.status); $('#bl_ins_preauth').prop('checked', String(d.preauth) === '1');
            $('#bl_ins_notes').val(d.notes);
            $('#bl_ins_title').text('{{ __('clinicCore::lang.edit') }}');
            $('#blInsModal').modal('show');
        });
        $('#bl_ins_save').on('click', function(){
            var id = $('#bl_ins_id').val();
            var url = id ? '{{ url('clinic/patients/insurances') }}/' + id + '/update'
                         : '{{ url('clinic/patients') }}/' + patientId + '/insurances';
            $.post(url, {
                _token: token,
                provider_name: $('#bl_ins_provider').val(), policy_number: $('#bl_ins_policy').val(),
                group_number: $('#bl_ins_group').val(), plan_type: $('#bl_ins_plan').val(),
                copay_amount: $('#bl_ins_copay').val(), effective_date: $('#bl_ins_effective').val(),
                expiry_date: $('#bl_ins_expiry').val(), status: $('#bl_ins_status').val(),
                pre_auth_required: $('#bl_ins_preauth').is(':checked') ? 1 : 0,
                notes: $('#bl_ins_notes').val()
            }).done(ok).fail(err);
        });
        $(document).on('click', '.bl-ins-del', function(){
            if(!confirm('{{ __('clinicCore::lang.confirm_delete') }}')) return;
            $.ajax({ url: '{{ url('clinic/patients/insurances') }}/' + $(this).data('id'), method:'POST',
                data:{ _token: token, _method:'DELETE' } }).done(ok).fail(err);
        });
    });
})();
</script>
@endif
