<?php
$templateIndex = json_decode(file_get_contents(storage_path('templates/index.json')));
$hasTemplates = true;
$templateName = '';
$defaultTemplateName = '';
$templateSets = [];
foreach($templateIndex as $s => $templateSection) {
    if(strpos($s, "exam-") === 0 && substr($s, -7) === "-detail") {
        foreach($templateIndex->{$s} as $k => $templateSet) {
            if($templateName === '') $templateName = $k;
            if($templateSet->default) $defaultTemplateName = $k;
            if(isset($templateSets[$k])) {
                echo "$k already exists!";
            }
            $namePre = ucwords(str_replace("-", " ", str_replace("exam-", "", $s)));
            // $templateSet->name = $namePre . ' / ' . $templateSet->name;
            // $templateSets["$s/$k"] = $templateSet;
            $templateSets[] = [
                "exam" => preg_replace("/-detail$/", "", preg_replace("/^exam-/", "", $s)),
                // "exam" => $s,
                "key" => $k,
                "templateSet" => $templateSet
            ];
        }
    }
}
if($defaultTemplateName !== '') $templateName = $defaultTemplateName;
?>
    <span class="position-relative d-none if-edit ml-3 pl-3 border-left">
        <a href="#" class="note-templates-trigger">Templates</a>
        <div class="note-template-container">
            <div class="position-relative w-100">
                <div class="note-template-output">
                    <div class="font-weight-bold text-secondary">Result:</div>
                    <div class="note-template-output-text"></div>
                </div>
                <div class="note-template-buttons d-flex align-items-center">
                    <button class="btn btn-sm btn-default bg-white border text-primary border-primary note-template-apply-trigger">Apply</button>
                    <button class="btn btn-sm btn-default bg-light border text-secondary border-secondary note-template-close-trigger">Close</button>
                </div>
            </div>
            <div class="p-1">
                <select class="form-control form-control-sm note-exam-template-set-chooser" data-section="{{$sectionInternalName}}">
                    <option exam="gen" value="default" selected>Default</option>
                    @foreach($templateSets as $templateSet)
                        <option exam="{{$templateSet["exam"]}}" value="{{$templateSet["key"]}}">{{$templateSet["templateSet"]->name}}</option>
                    @endforeach
                </select>
            </div>
            <div class="px-1 pb-1 border-bottom">
                <select class="form-control form-control-sm note-exam-exam-chooser">
                    <option value="gen" selected>General Exam</option>
                    <option value="heent">HEENT System</option>
                    <option value="neck">Neck</option>
                    <option value="breast">Breast</option>
                    <option value="resp">Resp System</option>
                    <option value="cv">CV System</option>
                    <option value="gi">GI System</option>
                    <option value="gu-rectal">GU/Rectal</option>
                    <option value="derm">Derm System</option>
                    <option value="ext">Extremities</option>
                    <option value="muscskel">Musc/Skel System</option>
                    <option value="neuro">Neuro System</option>
                    <option value="psych">Psych System</option>
                </select>
            </div>
            {{--@include('app/patient/note/_template')--}}
        </div>
    </span>


