<?php

// info from patient chart
$drugs = \App\Models\Point::getPointsOfCategory($patient, "MEDICATION");
$allergies = \App\Models\Point::getPointsOfCategory($patient, "ALLERGY");
$problems = \App\Models\Point::getPointsOfCategory($patient, "PROBLEM");

// filter out drugs without fdb info on them
$drugs = $drugs->filter(function($_drug) {
    return @$_drug->data &&
        $_drug->data->name &&
        $_drug->data->medId &&
        $_drug->data->routedMedId &&
        $_drug->data->routedDosageFormMedId &&
        $_drug->data->gcnSeqno;
});

// filter out allergies without fdb info on them
$allergies = $allergies->filter(function($_allergy) {
    return @$_allergy->data &&
        $_allergy->data->name &&
        $_allergy->data->damConceptId &&
        $_allergy->data->damConceptIdType;
});

// filter out problems without fdb info on them
$problems = $problems->filter(function($_allergy) {
    return @$_allergy->data &&
        $_allergy->data->name &&
        $_allergy->data->dxid;
});

// side effects
$sideeffects = side_effects_info($drugs);

// contraindications
$contraindications = contraindications_info($drugs, $problems);

// dpt
$dptInfo = duplicate_therapy_info($drugs);

// coadministration notes
$coadministration = coadministration_info($drugs);

// ddi
$ddi = drug_drug_interaction_info($drugs);

// dam
$dam = drug_allergy_info($drugs, $allergies);

$hasMRVData = false;

?>

<div class="px-3 pt-3 popup-content-container">

@if(!!$dptInfo)
    <?php $hasMRVData = true; ?>
    <div class="mrv-badge ml-1 align-self-center on-trigger-only" title="Duplicate Therapy Notes"><span>DT</span></div>
    <div class="p-3 mb-3 border bg-white mr-0 on-content-only">
        <p class="font-weight-bold m-0 font-size-14 text-info">Duplicate Therapy Notes</p>
        <div class="mt-3 ml-3">
            {!! $dptInfo !!}
        </div>
    </div>
@endif

@if(!!$coadministration)
    <?php $hasMRVData = true; ?>
    <div class="mrv-badge ml-1 align-self-center on-trigger-only" title="Coadministration Notes"><span>CA</span></div>
    <div class="p-3 mb-3 border bg-white mr-0 on-content-only">
        <p class="font-weight-bold m-0 font-size-14 text-info">Coadministration Notes</p>
        <div class="mt-3 ml-3">
            {!! $coadministration !!}
        </div>
    </div>
@endif

@if(!!$dam)
    <?php $hasMRVData = true; ?>
    <div class="mrv-badge ml-1 align-self-center on-trigger-only" title="Drug Allergy Notes"><span>DA</span></div>
    <div class="p-3 mb-3 border bg-white mr-0 on-content-only">
        <p class="font-weight-bold m-0 font-size-14 text-info">Drug Allergy Notes</p>
        <div class="mt-3 ml-3">
            {!! $dam !!}
        </div>
    </div>
@endif

@if(!!$ddi)
    <?php $hasMRVData = true; ?>
    <div class="mrv-badge ml-1 align-self-center on-trigger-only" title="Drug-Drug Interaction Notes"><span>DI</span></div>
    <div class="p-3 mb-3 border bg-white mr-0 on-content-only">
        <p class="font-weight-bold m-0 font-size-14 text-info">Drug-Drug Interaction Notes</p>
        <div class="mt-3 ml-3">
            {!! $ddi !!}
        </div>
    </div>
@endif

@if(count($sideeffects))
    <?php $hasMRVData = true; ?>
    <div class="mrv-badge ml-1 align-self-center on-trigger-only" title="Side Effects"><span>SE</span></div>
    <div class="p-3 mb-3 border bg-white mr-0 on-content-only">
        <p class="font-weight-bold m-0 font-size-14 text-info">Side Effects</p>
        @foreach($sideeffects as $drug => $sideeffect)
            <div class="mt-3 ml-3">
                <p class="font-weight-bold mb-2">{{$drug}}</p>
                <div class="flex-grow-1 d-inline-flex flex-wrap ml-3">
                    @foreach($sideeffect as $se)
                        <span class="mr-2 px-2 py-0 border bg-light mb-1">{{$se['dxid_desc56']}}</span>
                    @endforeach
                </div>
            </div>
        @endforeach
    </div>
@endif

@if(count($contraindications))
    <?php $hasMRVData = true; ?>
    <div class="mrv-badge ml-1 align-self-center on-trigger-only" title="Contraindications"><span>CI</span></div>
    <div class="p-3 mb-3 border bg-white mr-0 on-content-only">
        <p class="font-weight-bold m-0 font-size-14 text-info">Contraindications</p>
        @foreach($contraindications as $drug => $contraindication)
            <div class="mt-3 ml-3">
                <p class="font-weight-bold mb-2">{{$drug}}</p>
                <div class="flex-grow-1 d-inline-flex flex-wrap ml-3">
                    @foreach($contraindication as $ci)
                        <span class="mr-2 px-2 py-0 border bg-light mb-1">{{$ci['dxid_desc56']}}</span>
                    @endforeach
                </div>
            </div>
        @endforeach
    </div>
@endif

</div>