<?php
    $segmentMap = [];
    foreach($note->segments as $segment):
        $segmentMap[$segment->segmentTemplate->internal_name] = $segment;
    endforeach;

    function renderVisitTemplateNode($note, $node, $pos, $segMap) {
        if(is_array($node)){
            renderVisitTemplateGroup($note, $node, $pos, $segMap);
        }else{
            renderNoteTemplateLeaf($note, $node, $pos, $segMap);
        }
    }

    function renderVisitTemplateGroup($note, $node, $pos, $segMap){
        ?>
        <div class="vt-group vt-pos-<?= $pos ?>">
            <div class="vt-heading">{{$node['heading']}}</div>
            <div class="vt-children">
                <?php foreach($node['children'] as $child): ?>
                <div class="vt-child">
                    <?php renderVisitTemplateNode($note, $child, $pos + 1, $segMap); ?>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php
    }

    function renderNoteTemplateLeaf($note, $leaf, $pos, $segMap){
        $segment = $segMap[$leaf];
        $leafPhpPath = resource_path('views') . '/app/patient/note/segment/leaf.php';
        include($leafPhpPath);
    }
    $len = count($visitTemplateUiConfig);
    $firsthalf = array_slice($visitTemplateUiConfig, 0, $len / 2 + 1);
    $secondhalf = array_slice($visitTemplateUiConfig, $len / 2 + 1);

?>
<style>
    .vt-group {
    }
    .vt-heading {
      font-family: Verdana, sans-serif;
      font-size: 12px;
      font-weight: bold;
      margin-bottom: 5px;
    }
    .vt-children {
        margin-left: 20px;
    }
    .vt-child {

    }
    .vt-leaf {
      margin-bottom: 12px;
    }
    .vt-leaf .vt-leaf-name {
      font-family: Verdana, sans-serif;
      font-size: 12px;
      font-weight: bold;
    }
    .vt-leaf .vt-leaf-summary {
        margin-left: 10px;
    }
    .vt-group.vt-pos-0 > .vt-heading {
        text-decoration: underline;
    }
    .vt-leaf.vt-pos-0 > .vt-leaf-name {
        text-decoration: underline;
    }
    .vt-group.vt-pos-1 > .vt-heading {
        /* font-size: 1.4em; */
    }
    .vt-leaf.vt-pos-1 > .vt-leaf-name {
        /* font-size: 1.4em; */
    }

</style>
<div>
    <div class="row p-3">
        <div class="col-md-12">
            <div class="row segments-list" id="note-segments-list">
                <div class="col-md-6 ">
                    <?php
                    foreach($firsthalf as $node) renderVisitTemplateNode($note, $node, 0, $segmentMap);
                    ?>
                </div>
                <div class="col-md-6">
                    <?php
                    foreach($secondhalf as $node) renderVisitTemplateNode($note, $node, 0, $segmentMap);
                    ?>
                    <div class="p-2 border-bottom screen-only" data-non-segment-section="Prescriptions">
                      @include('app.patient.prescriptions.list', compact('patient', 'note'))
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@include('app.patient.note.segment_script')
