<?php
$currentValue = '';
$previousValue = '';
$previousChildReview = null;
if (@$point) {
    if ($point->lastChildReview && $point->last_child_review_point_scoped_note_id === $note->id) {
        $parsedReview = json_decode($point->lastChildReview->data);
        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;
            }
        }
    }
}
?>
<div visit-moe wide class="d-block">
    <?php if($point): ?>
        <form show url="/api/visitPoint/upsertChildReview" class="mcp-theme-1 w-100 frm-upsert-review-plan-nutrition" left>
            <input type="hidden" name="uid" value="<?= $point->uid ?>">
            <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
            <input type="hidden" name="data">



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

            <p class="mb-2"><b>Current Intake (ounces per day)</b></p>

            <div class="mb-2">
                <div note-rte slim-rte
                     class="form-group mb-0 border-left border-right rte-holder"
                     data-field-name="value"><?= $currentValue ?></div>
            </div>
        </form>
    <?php else: ?>

    <?php endif; ?>
</div>
<?php if(!empty($currentValue)): ?>
    <div visit-moe relative class="d-block">
        <a start show href="#" class="px-1 text-left" title="Undo Changes">
            <i class="fa fa-undo"></i> Undo Changes
        </a>
        <form url="/api/visitPoint/destroyCurrentChildReview" class="mcp-theme-1">
            <input type="hidden" name="uid" value="<?= $point->uid ?>">

            <p class="mb-2">Undo Changes?</p>

            <div>
                <button submit class="btn btn-sm btn-danger mr-2">Delete</button>
                <button cancel class="btn btn-sm btn-default border">Cancel</button>
            </div>
        </form>
    </div>
<?php endif; ?>

