<?php
$sectionTS = strtotime($section->created_at);
if ($latestSectionTS === 0 || $latestSectionTS < $sectionTS) {
    $latestSectionTS = $sectionTS;
}
?>
<div data-ts="{{$sectionTS}}" class="p-3 border-bottom note-section "
    data-section-uid="{{ $section->uid }}"
    data-section-template-uid="{{ $section->sectionTemplate->uid }}"
    data-section-template-name="{{ $section->sectionTemplate->internal_name }}">
    <div class="d-flex align-items-start">
        @if(!isset($note) || (isset($note) && !$note->is_signed_by_hcp))
            <a class="font-weight-bold mb-2 d-flex align-items-center c-pointer">
                {{$section->sectionTemplate->title}}
                @if(!isset($guestAccessCode))
                    <span class="d-none if-not-edit"><i class="fa fa-edit ml-2"></i></span>
                    <span class="d-none if-edit edit-trigger font-weight-normal ml-2">Collapse</span>
                    <span class="edit-trigger"></span>
                @endif
            </a>
        @else
            <span class="font-weight-bold mb-2 d-flex align-items-center">
                {{$section->sectionTemplate->title}}
            </span>
        @endif
        <?php $sectionInternalName = $section->sectionTemplate->internal_name; ?>

        @if($sectionInternalName === "exam" || $sectionInternalName === "objective")
        @include('app/patient/note/_templates-exam-index')
        @else
        @include('app/patient/note/_templates-index')
        @endif

        <?php
        if (file_exists(storage_path('sections/' . $sectionInternalName . '/actions.php'))) {
            include(storage_path('sections/' . $sectionInternalName . '/actions.php'));
        }
        ?>
        <?php if (file_exists(storage_path('sections/' . $sectionInternalName . '/actions.blade.php'))): ?>
        @include('sections/' . $sectionInternalName . '/actions')
        <?php endif; ?>

        {{-- refresh from client data --}}
        <?php if ($section->sectionTemplate->is_canvas): ?>
            <div moe class="d-none if-edit ml-2">
                <a start show class="font-weight-normal" title="Update with latest patient data">
                    <i class="fa fa-sync"></i>
                </a>
                <form url="/api/section/updateFromCanvas">
                    <p>Update this section with latest patient data?</p>
                    <input type="hidden" name="uid" value="{{ $section->uid }}">
                    <div class="d-flex align-items-center">
                        <button class="btn btn-sm btn-primary mr-2" type="button" submit>Update</button>
                        <button class="btn btn-sm btn-default mr-2 border" type="button" cancel>Cancel</button>
                    </div>
                </form>
            </div>
        <?php endif; ?>

        @if(isset($pro))
            <div class="ml-auto d-inline-flex align-items-center">
                <div class="ml-auto">
                    @if(!isset($note) || (isset($note) && !$note->is_signed_by_hcp))
                        @include('app.patient.note.guest-access')
                    @endif
                </div>
                <a href="#" class="d-none text-danger if-edit ml-2 remove-section-trigger mr-1" data-uid="{{$section->uid}}" title="Remove {{$section->sectionTemplate->title}}">
                    Remove
                </a>
                <a href="#" class="d-none if-edit ml-2 move-up-trigger" data-uid="{{$section->uid}}" title="Move Up">
                    <i class="fa fa-arrow-up"></i>
                </a>
                <a href="#" class="d-none if-edit ml-2 move-down-trigger" data-uid="{{$section->uid}}" title="Move Down">
                    <i class="fa fa-arrow-down"></i>
                </a>
            </div>
        @endif
    </div>
    @if(!isset($guestAccessCode) || $section->guest_access_level == 'READ')
    <div class="{{isset($guestAccessCode)?'':'d-none'}} if-not-edit  inset-comment summary-container">
        {!! !empty($section->summary_html) ? $section->summary_html : '-' !!}
    </div>
    @endif

    @if(!isset($guestAccessCode) || $section->guest_access_level == 'WRITE')
    <div class="{{isset($guestAccessCode)?'':'d-none'}} if-edit">

        <?php
        // if canvas
        if ($section->sectionTemplate->is_canvas) {
            $contentData = false;
            if ($canvasData) {
                $contentData = json_decode($section->content_data, true);
            }
        ?>
            @include("app.patient.canvas-sections.{$sectionInternalName}.form")
        <?php
        }

        // if form driven
        else if (file_exists(storage_path('sections/' . $sectionInternalName . '/form.blade.php'))) {
        ?>
            @include('sections.' . $sectionInternalName . '.form')
        <?php
        }

        // if neither canvas nor form driven
        else {
            $contentData = false;
            if ($section) {
                $contentData = json_decode($section->content_data, true);
            }
            if (!$contentData || !isset($contentData['value'])) {
                $contentData = [
                    'value' => ''
                ];
            }
        ?>
            <input type="hidden" name="data">
            <div note-rte data-content="{{$contentData['value']}}" class="form-group mb-2 border-left border-right rte-holder"></div>
        <?php
        }
        ?>

        <div class="d-flex align-items-center">
            <button class="btn btn-sm btn-primary" btn-save-form><i class="fa fa-save"></i></button>
            <span class="ml-2 text-secondary text-sm text-saving d-none"><i>Saving ...</i></span>
            <span class="ml-2 text-secondary text-sm text-saved font-weight-bold d-none"></span>
        </div>
    </div>
    @endif
</div>
