<div class="segments-list note-segments-print-list {{ $note->is_signed_by_hcp ? 'note-signed-by-hcp' : '' }}" id="note-segments-list">
    <?php
    $previousHeading = null;
    $previousSubHeading = null;
    $segments = $note->segments->filter(function ($_x) {
        $ok = !!$_x->is_active && $_x->segmentTemplate->internal_name !== 'medrisk_vigilence' && strpos($_x->segmentTemplate->internal_name, 'lifestyle_') !== 0;
        if ($ok) {
            $content = $_x->summary_html;
            if ($_x->accepted_suggestion_summary_html) {
                $content = $_x->accepted_suggestion_summary_html;
            }
            $trimmed = trim(strip_tags($content));
            $ok = ($trimmed !== '' && $trimmed !== '-');
        }
        return $ok;
    });
    $printSegments = [];
    foreach ($segments as $segment) {
        if ($segment->left_or_right !== 'RIGHT') {
            $printSegments[] = $segment;
        }
    }
    foreach ($segments as $segment) {
        if ($segment->left_or_right === 'RIGHT') {
            $printSegments[] = $segment;
        }
    }
    ?>
    <!-- -- -->
    <div style="font-family: Arial, Helvetica, sans-serif;display: flex;width:100%;border-top:1px solid #333333;border-bottom:1px solid #333333;">
        <div style="border-right:1px solid #333333;padding:10px;width:30%;vertical-align: top;height:auto;">
            <div style="">
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Race: </span>{{ $patient->race ?? 'No race saved for patient' }}</div>
                <?php
                $canvasData = json_decode($patient->canvas_data);
                ?>
                @if($canvasData && @$canvasData->INTAKE_DATA)
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Ethnicity: </span>{{ $canvasData->INTAKE_DATA->ethnicity ?? 'No ethnicity for patient saved' }}</div>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Height: </span>{{ $canvasData->INTAKE_DATA->height ?? 'No height for patient saved' }}</div>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Weight: </span>{{ $canvasData->INTAKE_DATA->weight ?? 'No weight for patient saved' }}</div>
                @endif
                @if(false)
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Preferred Language: </span>{{ $patient->preferred_language ?? 'No preferred language saved for patient' }}</div>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Preferred Foreign Language: </span>{{ $patient->preferred_foreign_language ?? 'No preferred foreign language saved for patient' }}</div>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Other Languages: </span>{{ $patient->other_languages ?? 'No other languages saved for patient' }}</div>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Sticky Note: </span>{{ $patient->sticky_note ?? 'No sticky note saved for patient' }}</div>
                @endif
                <!-- Allergies -->
                <?php
                $allergies = \App\Models\Point::getPointsOfCategory($patient, "ALLERGY");
                ?>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Alergies:</div>
                @if(count($allergies))
                <ul>
                    @foreach($allergies as $allergy)
                    <li style="font-size: 16px;">{{ $allergy->data_name }}</li>
                    @endforeach
                </ul>
                @else
                <div style="font-size: 16px;">Patient has no known allergies</div>
                @endif

                <!-- Medications -->
                <?php
                $medications = \App\Models\Point::getPointsOfCategory($patient, "MEDICATION");
                ?>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Medications:</div>
                @if(count($medications))
                <ul>
                    @foreach($medications as $medication)
                    <li style="font-size: 16px;">{{ $medication->data_name }}</li>
                    @endforeach
                </ul>
                @else
                <div style="font-size: 16px;">Patient has no known medications</div>
                @endif

                <!-- Problems -->
                <?php
                $problems = \App\Models\Point::getPointsOfCategory($patient, "PROBLEM");
                ?>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Problems:</div>
                @if(count($problems))
                <ul>
                    @foreach($problems as $problem)
                    <li style="font-size: 16px;">{{ $problem->data_name }}</li>
                    @endforeach
                </ul>
                @else
                <div style="font-size: 16px;">Patient has no known problems</div>
                @endif

                <!-- Goals -->
                <?php
                $goals = \App\Models\Point::getPointsOfCategory($patient, "GOAL");
                ?>
                <div style="font-size:16px;margin-bottom:10px;"><span style="font-weight: bold;font-size:16px;">Goals:</div>
                @if(count($goals))
                <ul>
                    @foreach($goals as $goal)
                    <li style="font-size: 16px;">{{ $goal->data_name }}</li>
                    @endforeach
                </ul>
                @else
                <div style="font-size: 16px;">Patient has no known goals</div>
                @endif

            </div>
        </div>
        <div style="padding:10px;width:70%;vertical-align: top;height:auto;">
            <div>
                @foreach($printSegments as $segment)
                @include('app.patient.note.segment-print')
                @endforeach
            </div>
        </div>
    </div>
    <!-- -- -->
</div>
<script>
    $(document).ready(function() {
        $('.note-segments-print-list table.point-table').each(function() {
            if (!$(this).find('tbody>tr').length) {
                $(this).closest('.visit-segment').remove();
            }
            if ($(this).closest('.note_template_soap_visit').length) {
                if (['nutrition', 'exercise', 'behavior'].indexOf($(this).closest('.visit-segment').attr('data-segment-template-name')) === -1) {
                    convertPointTableToSimpleParas($(this));
                } else {
                    convertNEBTables($(this));
                }
            }
        });
        $('.note-segments-print-list table.table-cage').each(function() {
            if ($(this).closest('.note_template_omega_soap_visit').length) {
                if (['omega_allergies', 'omega_medications', 'omega_goals', 'omega_care_team', 'omega_subjective_system'].indexOf($(this).closest('.visit-segment').attr('data-segment-template-name')) !== -1) {
                    convertCageTableToSimpleParas($(this), 'Subjective');
                } else if (['omega_plan_system'].indexOf($(this).closest('.visit-segment').attr('data-segment-template-name')) !== -1) {
                    convertCageTableToSimpleParas($(this), 'Plan');
                }
            }
        });
        if (isEmpty($('[data-segment-template-name="nutrition"] .summary-container'))) $('[data-segment-template-name="nutrition"]').remove();
        if (isEmpty($('[data-segment-template-name="exercise"] .summary-container'))) $('[data-segment-template-name="exercise"]').remove();
        if (isEmpty($('[data-segment-template-name="behavior"] .summary-container'))) $('[data-segment-template-name="behavior"]').remove();

        function isEmpty(_el) {
            return $.trim(_el.text().replaceAll('-', '')) === '';
        }

        function convertPointTableToSimpleParas(_table) {
            let parent = _table.parent();
            parent.find('[if-edit-mode]').remove();
            $(_table).find('tbody>tr').each(function() {
                if ($(this).find('>td').length > 1) {
                    let newD = $('<div class="mb-2"/>');
                    $('<p class=""/>').html($(this).find('td:eq(1)').html()).appendTo(newD);
                    $('<p class="pl-3 remove-if-empty-parent"/>').append('<b class="text-secondary">Subjective:</b>').append('<div class="d-inline-block pl-2 remove-if-empty">' + $(this).find('td:eq(2)').html() + '</div>').appendTo(newD);
                    $('<p class="pl-3 remove-if-empty-parent"/>').append('<b class="text-secondary">Plan:</b>').append('<div class="d-inline-block pl-2 remove-if-empty">' + $(this).find('td:eq(3)').html() + '</div>').appendTo(newD);
                    newD.appendTo(parent)
                    newD.find('.text-sm.text-secondary, [if-edit-mode], i.fa').remove();
                    newD.find('[if-read-mode]').show().addClass('d-inline-block');
                    newD.find('.bg-warning-mellow.p-2.rounded').removeClass('bg-warning-mellow p-2 rounded');
                    newD.find('.inline-html-container+.text-sm').remove();
                    newD.find('.remove-if-empty').each(function() {
                        if (isEmpty($(this))) {
                            $(this).closest('.remove-if-empty-parent').remove();
                        }
                    });
                }
            });
            $(_table).remove();
        }

        function convertCageTableToSimpleParas(_table, _label = 'Subjective') {
            let parent = _table.parent();
            parent.find('[if-edit-mode]').remove();
            $(_table).find('tbody>tr').each(function() {
                if ($(this).find('>td').length > 1) {
                    let newD = $('<div class="mb-2"/>');
                    $('<p class=""/>').html($(this).find('td:eq(1)').html()).appendTo(newD);
                    $('<p class="pl-3 remove-if-empty-parent"/>').append('<b class="text-secondary">' + _label + ':</b>').append('<div class="d-inline-block pl-2 remove-if-empty">' + $(this).find('td:eq(2)').html() + '</div>').appendTo(newD);
                    newD.appendTo(parent)
                    newD.find('.text-sm.text-secondary, [if-edit-mode], i.fa').remove();
                    newD.find('[if-read-mode]').show().addClass('d-inline-block');
                    newD.find('.bg-warning-mellow.p-2.rounded').removeClass('bg-warning-mellow p-2 rounded');
                    newD.find('.grow-till-300px').removeClass('grow-till-300px');
                    // newD.find('.text-sm.text-info').remove();
                    newD.find('.inline-html-container+.text-sm').remove();
                    newD.find('.remove-if-empty').each(function() {
                        if (isEmpty($(this))) {
                            $(this).closest('.remove-if-empty-parent').remove();
                        }
                    });
                }
            });
            $(_table).remove();
        }

        function convertNEBTables(_table) {
            let parent = _table.parent();
            parent.find('[if-edit-mode]').remove();
            $(_table).find('tbody>tr').each(function() {
                if (!isEmpty($(this).find('td:eq(1)')) || !isEmpty($(this).find('td:eq(2)'))) {
                    let newD = $('<div class="mb-2"/>');
                    $('<p class=""/>').html($(this).find('td:eq(0)').html())
                        .append($('<p class="remove-if-empty-parent"/>').append('<b class="text-secondary">Current:</b>').append('<div class="d-inline-block pl-2 remove-if-empty">' + $(this).find('td:eq(1)').html() + '</div>'))
                        .append($('<p class="remove-if-empty-parent"/>').append('<b class="text-secondary">Plan:</b>').append('<div class="d-inline-block pl-2 remove-if-empty">' + $(this).find('td:eq(2)').html() + '</div>'))
                        .appendTo(newD);
                    newD.appendTo(parent)
                    newD.find('.text-sm.text-secondary, [if-edit-mode], i.fa').remove();
                    newD.find('[if-read-mode]').show().addClass('d-inline-block');
                    newD.find('.bg-warning-mellow.p-2.rounded').removeClass('bg-warning-mellow p-2 rounded');
                    newD.find('.inline-html-container+.text-sm').remove();
                    newD.find('.remove-if-empty').each(function() {
                        if (isEmpty($(this))) {
                            $(this).closest('.remove-if-empty-parent').remove();
                        }
                    });
                }
            });
            $(_table).remove();
        }
    });
</script>