<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Note</title>
    <style>
      /** Define the margins of your page **/
      @page {
        margin: 50px 20px;
      }

      footer {
        position: fixed;
        bottom: -50px;
        left: 0px;
        right: 0px;
        height: 50px;
      }
      .page-number:before {
        content: "Page: " counter(page);
      }
    </style>
</head>
<body>

<footer>
    <table style="border: 0; width: 100%; font-size: 12px" border="0">
        <tr>
            <td style="vertical-align: top; width: 50%; text-align: left">
                Printed on: {{friendly_date_time(date('Y-m-d H:i:s'))}}
                <br>
                Note created using: Endochart
            </td>
            <td style="vertical-align: top; width: 50%; text-align: right">
                <div class="page-number"></div>
            </td>
        </tr>
    </table>
</footer>

<div style="margin: 0; padding: 1rem; font-family: serif">
    <!-- header/smg info -->
    <div style="text-align: right; margin-bottom: 1.5rem; font-family: sans-serif">
        <div style="margin-bottom: 3px; font-weight:bold; font-size: 14px">Scholar Medical Group</div>
        <div style="margin-bottom: 3px; font-size: 13px;">133 Rollins Ave, Suite 3</div>
        <div style="margin-bottom: 3px; font-size: 13px;">Rockville, MD 20852</div>
        <div style="margin-bottom: 3px; font-size: 13px;">Phone: 202-935-1033</div>
        <div style="margin-bottom: 3px; font-size: 13px;">NPI# 1639796428</div>
    </div>

    <!-- header grey bar -->
    <table style="background-color: #d9d9d9; border: 0; width: 100%; font-family: sans-serif" border="0">
        <tr>
            <td style="width: 50%; text-align: left; font-weight:bold; padding: 0.5rem 1rem;">Tele-Audio Visit</td>
            <td style="width: 50%; text-align: right; font-weight:bold; padding: 0.5rem 1rem;">
                @if($note->new_or_fu_or_na === "NEW")
                    New
                @elseif($note->new_or_fu_or_na === "FU")
                    Follow-up
                @else
                    {{$note->new_or_fu_or_na}}
                @endif
                Televisit Note
            </td>
        </tr>
    </table>

    <!-- patient info -->
    <div style="padding: 1rem; border-bottom: 1px solid #ddd;">
        <div style="font-size: 18px; font-weight:bold; padding-bottom: 0.75rem; font-family: sans-serif">
            {{implode(', ', array_filter([$patient->name_last, $patient->name_first]))}}
        </div>
        <table style="border: 0; width: 100%; border: 0" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td style="width: 60%; text-align: left;">
                    <table style="border: 0; width: 100%;" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td style="padding-bottom: 0.75rem; vertical-align: top" width="100px">Address:</td>
                            <td style="padding-bottom: 0.75rem; vertical-align: top">{{@$patient->mailing_address_line1 ? $patient->mailing_address_line1 : ''}}</td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 0.75rem; vertical-align: top" width="100px"></td>
                            <td style="padding-bottom: 0.75rem; vertical-align: top">
                                {{@$patient->mailing_address_city ? $patient->mailing_address_city : ''}}
                                {{@$patient->mailing_address_state ? $patient->mailing_address_state : ''}}
                                {{@$patient->mailing_address_zip ? $patient->mailing_address_zip : ''}}
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 0.75rem; vertical-align: top" width="100px">Gender:</td>
                            <td style="padding-bottom: 0.75rem; vertical-align: top">{{$patient->sex}}</td>
                        </tr>
                        <tr>
                            <td style="vertical-align: top" width="100px">Visited on:</td>
                            <td style="vertical-align: top">{{friendly_date($note->effective_dateest)}}</td>
                        </tr>
                    </table>
                </td>
                <td style="width: 40%; text-align: left;">
                    <table style="border: 0; width: 100%;" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td style="padding-bottom: 0.75rem; vertical-align: top" width="150px">Birth Date (Age):</td>
                            <td style="padding-bottom: 0.75rem; vertical-align: top">{{friendly_date_time($patient->dob, false,null,true)}} ({{$patient->age_in_years}} Y)</td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 0.75rem; vertical-align: top" width="150px">Home Phone:</td>
                            <td style="padding-bottom: 0.75rem; vertical-align: top">{{$patient->phone_home}}</td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 0.75rem; vertical-align: top" width="150px">Cell Phone:</td>
                            <td style="padding-bottom: 0.75rem; vertical-align: top">{{$patient->cell_number}}</td>
                        </tr>
                        <tr>
                            <td style="vertical-align: top" width="150px">Email:</td>
                            <td style="vertical-align: top">{{$patient->email_address}}</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>

    <?php
    $allergiesSegment = null;
    $vitalsSegment = null;
    $medicationsSegment = null;
    $ccSegment = null;
    $problemsIntakeSegment = null;
    $problemsPlanSegment = null;
    $rosSegment = null;
    $objectiveFreeTextSegment = null;
    $objectiveImagingSegment = null;
    $objectiveLabsSegment = null;
    $objectiveMeasurementsSegment = null;
    $objectiveOtherInvestigationsSegment = null;
    $planFreeTextSegment = null;

    // find and store handle to specific segments
    foreach($note->segments as $segment) {
        switch($segment->segmentTemplate->internal_name) {
            case 'plan_allergies':
                $allergiesSegment = $segment;
                break;
            case 'vitals':
                $vitalsSegment = $segment;
                break;
            case 'plan_medications':
                $medicationsSegment = $segment;
                break;
            case 'chief_complaint':
                $ccSegment = $segment;
                break;
            case 'intake_problems':
                $problemsIntakeSegment = $segment;
                break;
            case 'plan_problems':
                $problemsPlanSegment = $segment;
                break;
            case 'ros':
                $rosSegment = $segment;
                break;
            case 'objective_free_text':
                $objectiveFreeTextSegment = $segment;
                break;
            case 'objective_imaging':
                $objectiveImagingSegment = $segment;
                break;
            case 'objective_labs':
                $objectiveLabsSegment = $segment;
                break;
            case 'objective_measurements':
                $objectiveMeasurementsSegment = $segment;
                break;
            case 'objective_other_investigations':
                $objectiveOtherInvestigationsSegment = $segment;
                break;
            case 'plan_free_text':
                $planFreeTextSegment = $segment;
                break;
        }
    }
    ?>

    <!-- allergies -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Allergies</b></p>
        @if(!$allergiesSegment || empty(trim($allergiesSegment->summary_html)))
            NKDA
        @else
            {!! $allergiesSegment->summary_html !!}
        @endif
    </div>

    <!-- vitals -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Vitals</b></p>
        @if(!$vitalsSegment || empty(trim($vitalsSegment->summary_html)))
            Unavailable
        @else
            {!! $vitalsSegment->summary_html !!}
        @endif
    </div>

    <!-- medications -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Medications</b></p>
        <?php
        $medications = \App\Models\Point::getPlanPointsOfCategory($patient, 'MEDICATION', $note);
        ?>
        <?php if (!count($medications)): ?>
            <div class="text-secondary">No medications</div>
        <?php else: ?>
            <?php foreach ($medications as $medication): ?>
                <div style="margin-bottom: 0.25rem;">
                    <span style="<?= $medication->is_removed ? 'text-decoration: line-through;' : '' ?>">
                        <b><?= !!@($medication->data->name) ? @($medication->data->name) : '-' ?></b>
                        <?= !!@($medication->data->dose) ? '/&nbsp;' . @($medication->data->dose) : '' ?>
                        <?= !!@($medication->data->frequency) ? '/&nbsp;' . @($medication->data->frequency) : '' ?>
                    </span>
                    <span style="padding-left: 1rem;">&nbsp;</span>
                    <?php if ($medication->is_removed): ?>
                        <span style="color: #666; font-size: 90%">Removed during visit</span>
                    <?php elseif ($medication->added_in_note_id === $note->id): ?>
                        <?php if ($medication->addition_reason_category === 'DURING_VISIT'): ?>
                            <span style="color: #666; font-size: 90%">* Added during visit</span>
                        <?php else: ?>
                            <span style="color: #666; font-size: 90%">* Added on intake</span>
                        <?php endif;?>
                    <?php endif; ?>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <!-- cc -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Chief Complaint</b></p>
        @if(!$ccSegment || empty(trim($ccSegment->summary_html)))
            -
        @else
            {!! $ccSegment->summary_html !!}
        @endif
    </div>

    <!-- hpi -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">History of Present Illness</b></p>
        @if(!$problemsIntakeSegment || empty(trim($problemsIntakeSegment->summary_html)))
            Unavailable
        @else
            <?php $problems = \App\Models\Point::getIntakePointsOfCategory($patient, 'PROBLEM', $note); ?>
            @if(!$problems || !count($problems)))
                Unavailable
            @endif
            @foreach($problems as $problem)
                <?php $childReviews = $problem->childReviews; ?>
                @foreach($childReviews as $childReview)
                    @if($childReview->added_in_note_id === $note->id)
                        <div style="font-weight:bold;">{{$problem->data->name}}</div>
                        <div style="padding-left: 1rem;">
                            <?php
                            $review = json_decode($childReview->data);
                            ?>
                            <table style="border: 0; width: 100%; border: 0" border="0" cellspacing="0" cellpadding="0">
                                <tr class="mb-1">
                                    <td style="width: 50%">Explain the problem.</td>
                                    <td class="ml-2" data-name="problem_explanation"><?= $review && @$review->problem_explanation ? @$review->problem_explanation : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">What happened between today and our last visit?</td>
                                    <td class="ml-2" data-name="events_between_last_visit_and_this"><?= $review && @$review->events_between_last_visit_and_this ? @$review->events_between_last_visit_and_this : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">How did it go with the plan I gave you?</td>
                                    <td class="ml-2" data-name="current_plan_status"><?= $review && @$review->current_plan_status ? @$review->current_plan_status : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">What barriers have you felt following the plan?</td>
                                    <td class="ml-2" data-name="barriers_following_current_plan"><?= $review && @$review->barriers_following_current_plan ? @$review->barriers_following_current_plan : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">How long have you had this?</td>
                                    <td class="ml-2" data-name="how_long"><?= $review && @$review->how_long ? @$review->how_long : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">When does it get worse?</td>
                                    <td class="ml-2" data-name="when_worse"><?= $review && @$review->when_worse ? @$review->when_worse : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">When does it get better?</td>
                                    <td class="ml-2" data-name="when_better"><?= $review && @$review->when_better ? @$review->when_better : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">What have you done for treatment?</td>
                                    <td class="ml-2" data-name="treatments_done"><?= $review && @$review->treatments_done ? @$review->treatments_done : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">How does it affect your ADLs?</td>
                                    <td class="ml-2" data-name="how_it_affects_ADLs"><?= $review && @$review->how_it_affects_ADLs ? @$review->how_it_affects_ADLs : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">How do you cope?</td>
                                    <td class="ml-2" data-name="how_cope"><?= $review && @$review->how_cope ? @$review->how_cope : '-' ?></td>
                                </tr>
                                <tr class="mb-1">
                                    <td style="width: 50%">How does this affect you emotionally?</td>
                                    <td class="ml-2" data-name="how_affect_emotionally"><?= $review && @$review->how_affect_emotionally ? @$review->how_affect_emotionally : '-' ?></td>
                                </tr>
                                <tr class="">
                                    <td style="width: 50%">Memo</td>
                                    <td class="ml-2" data-name="memo"><?= $review && @$review->memo ? @$review->memo : '-' ?></td>
                                </tr>
                            </table>
                        </div>
                    @endif
                @endforeach
            @endforeach
        @endif
    </div>

    <!-- ros -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Review of Systems</b></p>
        @if(!$rosSegment || empty(trim($rosSegment->summary_html)))
            Unavailable
        @else
            {!! $rosSegment->summary_html !!}
        @endif
    </div>

    <!-- objective -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Objective</b></p>
        @if($objectiveFreeTextSegment && !empty(trim($objectiveFreeTextSegment->summary_html)))
            {!! $objectiveFreeTextSegment->summary_html !!}
        @endif
        @if($objectiveImagingSegment && !empty(trim($objectiveImagingSegment->summary_html)))
            {!! $objectiveImagingSegment->summary_html !!}
        @endif
        @if($objectiveLabsSegment && !empty(trim($objectiveLabsSegment->summary_html)))
            {!! $objectiveLabsSegment->summary_html !!}
        @endif
        @if($objectiveMeasurementsSegment && !empty(trim($objectiveMeasurementsSegment->summary_html)))
            {!! $objectiveMeasurementsSegment->summary_html !!}
        @endif
        @if($objectiveOtherInvestigationsSegment && !empty(trim($objectiveOtherInvestigationsSegment->summary_html)))
            {!! $objectiveOtherInvestigationsSegment->summary_html !!}
        @endif
    </div>

    <!-- icds -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">ICDs</b></p>
        @if(!count($note->reasons()))
            Unavailable
        @else
            @foreach ($note->reasons() as $reason)
                <div style="margin-bottom: 0.5rem;">
                    {{ $reason->description}} ({{ $reason->code }})
                </div>
            @endforeach
        @endif
    </div>

    <!-- plan -->
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Plan</b></p>
        @if(!$problemsPlanSegment || empty(trim($problemsPlanSegment->summary_html)))

        @else
            <?php $problems = \App\Models\Point::getPlanPointsOfCategory($patient, 'PROBLEM', $note); ?>
            @if(!$problems || !count($problems)))

            @endif
            @foreach($problems as $problem)
                <?php $childPlans = $problem->childPlans; ?>
                @foreach($childPlans as $childPlan)
                    @if($childPlan->added_in_note_id === $note->id)
                        <div style="font-weight:bold;">{{$problem->data->name}} {{$problem->data->icd ? ' / ' . $problem->data->icd : ''}}</div>
                        <div style="padding-left: 1rem;">
                            <?php
                            $review = json_decode($childPlan->data);
                            ?>
                            <p>{!! $review->value !!}</p>
                        </div>
                    @endif
                @endforeach
            @endforeach
        @endif
        @if($planFreeTextSegment && !empty(trim($planFreeTextSegment->summary_html)))
            {!! $planFreeTextSegment->summary_html !!}
        @endif
    </div>

    <!-- signature -->
    <?php
    $bills = \App\Models\Bill::where('note_id', $note->id)
        ->where('bill_service_type', 'NOTE')
        ->where('code', 'Treatment Services')
        ->where('is_cancelled', false)
        ->where('hcp_pro_id', $note->hcpPro->id)
        ->get();
    $totalSeconds = 0;
    foreach ($bills as $bill) {
        $totalSeconds += $bill->number_of_units * 3600;
    }
    ?>
    <div style="padding: 1rem; padding-bottom: 0;">
        <p><b style="font-size: 18px">Signature</b></p>
        <table style="border: 0; width: 100%;" border="0" cellspacing="0" cellpadding="0">
            <tr style="background-color: #d9d9d9">
                <td style="font-weight:bold;border-top: 1px solid #222; border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="20%">Date of Service</td>
                <td style="font-weight:bold;border-top: 1px solid #222; border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="30%">Service</td>
                <td style="font-weight:bold;border-top: 1px solid #222; border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="50%">Place of Service</td>
            </tr>
            <tr>
                <td style="padding: 0.25rem 0.5rem; vertical-align: top" width="20%">{{friendly_date($note->effective_dateest)}}</td>
                <td style="padding: 0.25rem 0.5rem; vertical-align: top" width="30%">Treatment Services</td>
                <td style="padding: 0.25rem 0.5rem; vertical-align: top" width="50%">133 Rollins Ave, Suite 3, Rockville, MD 20852</td>
            </tr>
            <tr style="background-color: #d9d9d9">
                <td style="font-weight:bold;border-top: 1px solid #222; border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="20%">Total time spent</td>
                <td style="font-weight:bold;border-top: 1px solid #222; border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="30%">Provider (HCP)</td>
                <td style="font-weight:bold;border-top: 1px solid #222; border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="50%">Signature</td>
            </tr>
            <tr>
                <td style="border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="20%">{{round($totalSeconds/60)}} minutes</td>
                <td style="border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="30%">{{$note->hcpPro->displayName()}}</td>
                <td style="border-bottom: 1px solid #222; padding: 0.25rem 0.5rem; vertical-align: top" width="50%">
                    @if($note->is_signed_by_hcp)
                    <img src="data:image/png;base64,[encoded data, 4596 characters]"
                         style="height: 10px">
                    Signed
                    @else
                    <span style="color: red; font-weight:bold;">No</span>
                    @endif
                </td>
            </tr>
        </table>
        @if($note->is_signed_by_hcp)
        <div style="text-align: right; font-size: 13px; padding-top: 2px">Signed electronically on {{friendlier_date_time($note->signed_by_hcp_at)}}</div>
        @endif
    </div>

    <div style="padding-top: 2rem; text-align: center">********</div>

</div>
</body>
</html>