<?php
$currentValue = '';
$previousValue = '';
$previousChildPlan = null;
if ($point->last_child_plan_point_id && $point->last_child_plan_data && $point->last_child_plan_point_scoped_note_id === $note->id) {
    $parsedPlan = json_decode($point->last_child_plan_data);
    $currentValue = $parsedPlan->value;
    $previousChildPlan = \App\Models\Point::where('id', '<', $point->last_child_plan_point_id)
        ->where('category', 'PLAN')
        ->where('parent_point_id', $point->id)
        ->orderBy('id', 'DESC')
        ->first();
    if($previousChildPlan && $previousChildPlan->data) {
        $parsedPlan = json_decode($previousChildPlan->data);
        $previousValue = $parsedPlan->value;
    }
}
else {
    $previousChildPlan = \App\Models\Point::where('parent_point_id', $point->id)
        ->where('category', 'PLAN')
        ->orderBy('id', 'DESC')
        ->first();
    if($previousChildPlan && $previousChildPlan->data) {
        $parsedPlan = json_decode($previousChildPlan->data);
        $previousValue = $parsedPlan->value;
    }
}
?>
<div class="d-flex align-items-start">
    <div visit-moe relative class="d-block flex-grow-1">
        <form show url="/api/visitPoint/upsertChildPlan" class="mcp-theme-1 w-100" 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 Plan / <?= friendlier_date($previousChildPlan->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="Delete Plan">
                <i class="fa fa-trash-alt text-danger on-hover-opaque"></i>
            </a>
            <form url="/api/visitPoint/destroyCurrentChildPlan" class="mcp-theme-1" right>
                <input type="hidden" name="uid" value="<?= $point->uid ?>">

                <p class="mb-2">Delete this plan?</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>
