<?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 ($_problem) {
    return @$_problem->data &&
        $_problem->data->name &&
        $_problem->data->dxid;
});

// get pivot drug
$pivotRx = json_decode(json_encode([
    "data" => [
        "name" => request()->input('name'),
        "medId" => request()->input('medId'),
        "routedMedId" => request()->input('routedMedId'),
        "routedDosageFormMedId" => request()->input('routedDosageFormMedId'),
        "gcnSeqno" => request()->input('gcnSeqno'),
    ]
]));

// other drugs
$otherDrugs = $drugs->filter(function ($_drug) {
    return $_drug->data->medId !== request()->input('medId') ||
        $_drug->data->gcnSeqno !== request()->input('gcnSeqno');
});

// side effects
$sideeffects = side_effects_info([$pivotRx]);

// contraindications
// TODO: check against patient's dx
$contraindications = contraindications_info([$pivotRx], $problems);

// dpt
// TODO: check against patient's other drugs
$dptInfo = duplicate_therapy_info_with_pivot($pivotRx, $otherDrugs);

// ddi
// TODO: check against patient's other drugs
$ddi = drug_drug_interaction_info_with_pivot($pivotRx, $otherDrugs);

// dam
$dam = drug_allergy_info([$pivotRx], $allergies);

?>

@if(!!$dptInfo)
    <div class="p-3 mb-3 border bg-white mr-0">
        <p class="font-weight-bold m-0 font-size-14 text-info">Duplicate Therapy Notes</p>
        <div class="mt-3">
            {!! $dptInfo !!}
        </div>
    </div>
@endif

@if(!!$dam)
    <div class="p-3 mb-3 border bg-white mr-0">
        <p class="font-weight-bold m-0 font-size-14 text-info">Drug Allergy Notes</p>
        <div class="mt-3">
            {!! $dam !!}
        </div>
    </div>
@endif

@if(!!$ddi)
    </div>
    <div class="p-3 mb-3 border bg-white mr-0">
        <p class="font-weight-bold m-0 font-size-14 text-info">Drug-Drug Interaction Notes</p>
        <div class="mt-3">
            {!! $ddi !!}
        </div>
    </div>
@endif

@if(count($sideeffects))
    <div class="p-3 mb-3 border bg-white mr-0">
        <p class="font-weight-bold m-0 font-size-14 text-info">Side Effects</p>
        @foreach($sideeffects as $drug => $sideeffect)
            <div class="mt-3">
                <p class="font-weight-bold mb-2">{{$drug}}</p>
                <div class="flex-grow-1 d-inline-flex flex-wrap">
                    @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))
    <div class="p-3 mb-3 border bg-white mr-0">
        <p class="font-weight-bold m-0 font-size-14 text-info">Contraindications</p>
        @foreach($contraindications as $drug => $contraindication)
            <div class="mt-3">
                <p class="font-weight-bold mb-2">{{$drug}}</p>
                <div class="flex-grow-1 d-inline-flex flex-wrap">
                    @foreach($contraindication as $ci)
                        <span class="mr-2 px-2 py-0 border bg-light mb-1 {{@$ci['flag'] ? 'text-danger border-danger' : ''}}">{{$ci['dxid_desc56']}}</span>
                    @endforeach
                </div>
            </div>
        @endforeach
    </div>
@endif

