<div class="note-rhs-sidebar screen-only pb-5" id="note-rhs-sidebar">

    <?php
    $cmStartDate = date('Y-m-01', strtotime($note->effective_dateest));
    $careMonth = \App\Models\CareMonth::where('client_id', $patient->id)->where('start_date', $cmStartDate)->first();
    ?>
    <div id="recent-measurements"
         class="p-2 border-bottom c-pointer on-hover-aliceblue {{$note->is_signed_by_hcp ? 'events-none' : ''}}"
         open-in-stag-popup
         mc-initer="care-month-dashboard-{{$patient->uid}}"
         @if($careMonth)
         title="Care Month: {{friendly_month($careMonth->start_date)}}"
         @endif
         popup-style="overflow-visible"
         @if($careMonth)
         href="/patients/view/{{ $note->client->uid }}/care-months/view/{{$careMonth->uid}}?fromNoteUid={{$note->uid}}"
         @endif
         >
        <div class="font-weight-bold">
            Recent Measurements
        </div>
        <?php
        $date = date('Y-m-d');
        $end = strtotime($date);
        $start = date_sub(date_create($date), date_interval_create_from_date_string("12 days"))->getTimestamp();
        $start *= 1000;
        $end *= 1000;
        $recentMeasurements = \App\Models\Measurement
            ::where('client_id', $patient->id)
            ->whereRaw("(is_removed IS NULL OR is_removed = FALSE)")
            ->whereNotNull('ts')
            ->whereRaw("(is_cellular_zero IS NULL OR is_cellular_zero = FALSE)")
            ->whereRaw("(label = 'BP' OR label = 'Wt. (lbs.)')")
            /*->where('ts', '>=', $start)
            ->where('ts', '<=', $end)*/
            ->orderBy('ts', 'desc')
            ->skip(0)
            ->take(10)
            ->get();
        ?>
        @if($recentMeasurements && count($recentMeasurements))
            <div class="pt-2">
                @foreach($recentMeasurements as $m)
                    @if($m->label === 'BP')
                        <div class="text-sm text-nowrap">BP: {{round($m->sbp_mm_hg)}}/{{round($m->dbp_mm_hg)}} ({{date('m/d/Y', $m->ts/1000)}})</div>
                    @elseif($m->label === 'Wt. (lbs.)')
                        <div class="text-sm text-nowrap">Wt: {{round($m->value, 1)}} ({{date('m/d/Y', $m->ts/1000)}})</div>
                    @endif
                @endforeach
            </div>
        @endif
    </div>

    <!-- MRV DT & DDI -->
    @include('app.patient.segment-templates.medrisk_vigilence.dt_ddi_short', ['note' => $note, 'patient' => $patient, 'segment' => $note->coreSegment])

    <?php
    $allergies = \App\Models\Point::getPointsOfCategory($patient, "ALLERGY");
    $medications = \App\Models\Point::getPointsOfCategory($patient, "MEDICATION");
    $supplements = \App\Models\Point::getPointsOfCategory($patient, "SUPPLEMENT");
    $problems = \App\Models\Point::getPointsOfCategory($patient, "PROBLEM");
    $goals = \App\Models\Point::getPointsOfCategory($patient, "GOAL");
    $prescriptions = $patient->prescriptionsCreatedInNote($note);
    $tracker = \App\Models\Point::getGlobalSingletonOfCategory($patient, 'NO_KNOWN_TRACKER');
    ?>
    <div id="active-allergies"
         class="p-2 border-bottom c-pointer on-hover-aliceblue"
         open-in-stag-popup
         mc-initer="allergies-center-{{$note->id}}"
         title="Allergies Center"
         popup-style="overflow-visible"
         href="/allergies-center/{{$patient->uid}}/{{$note->uid}}">
        <div class="font-weight-bold mb-2">
            Allergies
            <i class="fa fa-bolt text-primary ml-1"></i>
        </div>
        @if($tracker && @($tracker->data->no_known_drug_allergies))
            <div class="text-secondary mb-2"><span>No Known Drug Allergies</span></div>
        @endif
        @if($tracker && @($tracker->data->no_known_allergies_of_any_type))
            <div class="text-secondary mb-2"><span>No Known Allergies of Any Type</span></div>
        @endif
        @if(!$tracker || !@($tracker->data->no_known_allergies_of_any_type))
            @if($allergies && count($allergies))
                @foreach($allergies as $allergy)
                    <?php $rel = $allergy->relevanceToNote($note); ?>
                    <div class="d-inline-flex align-items-baseline px-1 {{$rel ? 'bg-warning-mellow' : ''}}">
                        <span class="mr-1">•</span>
                        <span>{{$allergy->data->name}}</span>
                    </div>
                @endforeach
            @else
                <span class="px-1 text-secondary">-</span>
            @endif
        @endif
    </div>
    <div id="active-medications"
         class="p-2 border-bottom c-pointer on-hover-aliceblue {{$note->is_signed_by_hcp ? 'events-none' : ''}}"
         open-in-stag-popup
         mc-initer="medications-center-{{$note->id}}"
         title="Medications Center"
         popup-style="overflow-visible"
         href="/medications-center/{{$patient->uid}}/{{$note->uid}}">
        <div class="font-weight-bold mb-2">
            Medications
            <i class="fa fa-bolt text-primary ml-1"></i>
        </div>
        @if($tracker && @($tracker->data->no_known_medications))
            <div class="text-secondary mb-2"><span>-</span></div>
        @endif
        @if(!$tracker || !@($tracker->data->no_known_medications))
            @if($medications && count($medications))
                @foreach($medications as $medication)
                    <?php $rel = $medication->relevanceToNote($note); ?>
                    <div class="d-inline-flex align-items-baseline px-1 {{$rel ? 'bg-warning-mellow' : ''}}">
                        <span class="mr-1">•</span>
                        <span>{{$medication->data->name}}
                        <?php
                        if($medication->lastChildReview) {
                            $review = json_decode($medication->lastChildReview->data);
                            if(isset($review->value) && isset($review->experience)) {
                                ?>
                                <span class="text-dark text-sm ml-1">({{$review->value}})</span>
                                <?php
                            }
                        }
                        ?>
                        </span>
                    </div>
                @endforeach
            @else
                <span class="px-1 text-secondary">-</span>
            @endif
        @endif
    </div>
    <div id="active-problems"
         class="p-2 border-bottom c-pointer on-hover-aliceblue {{$note->is_signed_by_hcp ? 'events-none' : ''}}"
         open-in-stag-popup
         mc-initer="problems-center-{{$note->id}}"
         title="Problems Center"
         popup-style="overflow-visible"
         href="/problems-center/{{$patient->uid}}/{{$note->uid}}">
        <div class="font-weight-bold mb-2">
            Problems
            <i class="fa fa-bolt text-primary ml-1"></i>
        </div>
        @if($problems && count($problems))
            @foreach($problems as $problem)
                <?php $rel = $problem->relevanceToNote($note); ?>
                <div class="d-inline-flex align-items-baseline px-1 {{$rel ? 'bg-warning-mellow' : ''}}">
                    <span class="mr-1">•</span>
                    <span>{{$problem->data->name}}
                    	{{$problem->data->icd}}
			</span>
                </div>
            @endforeach
        @else
            <span class="px-1 text-secondary">-</span>
        @endif
    </div>
    <div id="active-goals"
         class="p-2 border-bottom c-pointer on-hover-aliceblue {{$note->is_signed_by_hcp ? 'events-none' : ''}}"
         open-in-stag-popup
         mc-initer="goals-center-{{$note->id}}"
         title="Goals Center"
         popup-style="overflow-visible"
         href="/goals-center/{{$patient->uid}}/{{$note->uid}}">
        <div class="font-weight-bold mb-2">
            Goals
            <i class="fa fa-bolt text-primary ml-1"></i>
        </div>
        @if($goals && count($goals))
            @foreach($goals as $goal)
                <?php $rel = $goal->relevanceToNote($note); ?>
                <div class="d-inline-flex align-items-baseline px-1 {{$rel ? 'bg-warning-mellow' : ''}}">
                    <span class="mr-1">•</span>
                    <span>{{$goal->data->goal}}</span>
                </div>
            @endforeach
        @else
            <span class="px-1 text-secondary">-</span>
        @endif
    </div>
    <div id="active-careteam"
         class="p-2 border-bottom c-pointer on-hover-aliceblue {{$note->is_signed_by_hcp ? 'events-none' : ''}}"
         open-in-stag-popup
         mc-initer="careteam-center-{{$note->id}}"
         title="Care Team Center"
         popup-style="overflow-visible"
         href="/careteam-center/{{$patient->uid}}/{{$note->uid}}">
        <div class="font-weight-bold">
            Care Team
            <i class="fa fa-external-link-alt text-primary ml-1"></i>
        </div>
    </div>
    <div id="note-prescriptions" class="p-2 border-bottom c-pointer on-hover-aliceblue {{$note->is_signed_by_hcp ? 'events-none' : ''}}" data-non-segment-target="Prescriptions">
        <div class="font-weight-bold">
            ERx &amp; Orders
        </div>
        @if($prescriptions && count($prescriptions))
            @foreach($prescriptions as $prescription)
                <div class="d-flex align-items-baseline px-1 pt-2">
                    <span class="mr-2 text-secondary text-sm font-weight-bold">{{$prescription->erx_category}}</span>
                    <?php $parsed = json_decode($prescription->clinical_detail_json) ?>
                    <span class="flex-grow-1">
                        <?php
                        switch($prescription->erx_category) {
                            case 'DRUG':
                                if(@$parsed && @$parsed->items) {
                                    foreach ($parsed->items as $item) {
                                        echo '<div>' . $item->medication . '</div>';
                                    }
                                }
                                break;
                            case 'LAB':
                            case 'IMAGING':
                                if(@$parsed && @$parsed->items) {
                                    foreach ($parsed->items as $item) {
                                        if(@$item->tests) {
                                            foreach ($item->tests as $test) {
                                                echo '<div>' . $test->desc . '</div>';
                                            }
                                        }
                                    }
                                }
                                break;
                        }
                        ?>
                    </span>
                </div>
            @endforeach
        @endif
    </div>

</div>
