<div class="p-3">

    <!-- basic info regarding problem for context -->
    <?php
    if ($point->data) {
        $point->data = json_decode($point->data);
    }
    $point = \App\Models\Point::fillPointStateAndBadge($point, $note);
    ?>
    <div class="border bg-aliceblue d-flex align-items-baseline p-2 mb-3">
        <b>{{$point->state}}</b>
        @if($point->badge)
            <span class="mx-2 text-secondary">|</span>
            <span class="">{{$point->badge}}</span>
        @endif
        @if(@($point->data->start_date))
            <span class="mx-2 text-secondary">|</span>
            <span class="">Start: {{friendly_date($point->data->start_date)}}</span>
        @endif
        @if(@($point->removal_effective_date))
            <span class="mx-2 text-secondary">|</span>
            <span class="">End: {{friendly_date($point->removal_effective_date)}}</span>
        @endif
    </div>

    <?php
    $currentReview = '';
    $currentValue = '';
    $previousValue = '';
    $previousChildReview = null;
    if ($point->lastChildReview && $point->last_child_review_point_scoped_note_id === $note->id) {
        $parsedReview = json_decode($point->lastChildReview->data);
        $currentReview = $parsedReview;
        if(@$parsedReview->value) {
            $currentValue = $parsedReview->value;
            $previousChildReview = \App\Models\Point::where('id', '<', $point->lastChildReview->id)
                ->where('category', 'REVIEW')
                ->where('parent_point_id', $point->id)
                ->orderBy('id', 'DESC')
                ->first();
            if($previousChildReview && $previousChildReview->data) {
                $parsedReview = json_decode($previousChildReview->data);
                $previousValue = $parsedReview->value;
            }
        }
    }
    else {
        $previousChildReview = \App\Models\Point::where('parent_point_id', $point->id)
            ->where('category', 'REVIEW')
            ->orderBy('id', 'DESC')
            ->first();
        if($previousChildReview && $previousChildReview->data) {
            $parsedReview = json_decode($previousChildReview->data);
            if(@$parsedReview->value) {
                $previousValue = $parsedReview->value;
            }
        }
    }
    ?>

    <?php if($previousValue): ?>
    <div class="mb-2">
        <div class="d-flex align-items-baseline mb-1">
            <span class="text-secondary">Previous HPI - <?= friendlier_date($previousChildReview->created_at) ?></span>
        </div>
        <div class="p-2 bg-light border inline-html-container"><?= $previousValue ?></div>
    </div>
    <?php endif; ?>

    <?php
    if(!@$template) {
        $template = 'hpi/core.json';
    }
    $patient = $note->client;
    $data = $currentReview ? $currentReview : null;
    $segment = null;
    if($note->id !== $patient->core_note_id) {
        $segment = $note->getSegmentByInternalName('intake_problems');
    }
    ?>
    <div id="edit-hpi-{{$point->id}}">

        <div moe relative class="d-block">
            <form show url="/api/visitPoint/upsertChildReview" class="mcp-theme-1" right>
                <input type="hidden" name="uid" value="<?= $point->uid ?>">
                <input type="hidden" name="noteUid" value="<?= $note->uid ?>">
                @if(!!$segment)
                    <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
                @endif

                <input type="hidden" name="data">

                @include('app.dq-engine.edit', compact('template', 'note', 'patient', 'point', 'data'))

                <div>
                    <button submit class="btn btn-sm btn-primary mr-2">Save</button>
                    <button cancel class="btn btn-sm btn-default border" onclick="closeStagPopup()">Close</button>
                </div>
            </form>
        </div>

    </div>

    <script src="/js/dq.js?v={{config('app.asset_version')}}"></script>
    <script>
        (function() {
            function init() {
                initDQ();
            }
            addMCInitializer('edit-hpi-{{$point->id}}', init, '#edit-hpi-{{$point->id}}');
        }).call(window);
    </script>

</div>