<?php
$currentValue = '';
$previousValue = '';
$previousChildReview = null;
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 class="d-flex align-items-start">
    <div visit-moe relative class="d-block flex-grow-1">
        <form show url="/api/visitPoint/upsertChildReview" class="mcp-theme-1" right>
            <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; ?>

            <div class="mb-0">
                <div note-rte slim-rte
                     class="form-group mb-0 border-left border-right rte-holder"
                     data-field-name="value"><?= $currentValue ?></div>
            </div>
            <div class="d-none">
                <button submit class="btn btn-sm btn-primary mr-2">Save</button>
                <button cancel class="btn btn-sm btn-default border">Cancel</button>
            </div>
        </form>
    </div>
    <?php if(!empty($currentValue)): ?>
        <div visit-moe relative class="d-inline">
            <a start show href="#" class="pl-2 pr-1 text-center d-block" title="Undo Changes">
                <i class="fa fa-trash-alt text-danger on-hover-opaque"></i>
            </a>
            <form url="/api/visitPoint/destroyCurrentChildReview" class="mcp-theme-1" right>
                <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; ?>
</div>

