<?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 visit-moe large class="d-inline">
    <a start show href="#" class="px-1 text-center d-block" title="<?= !empty($currentValue) ? 'Edit Plan' : 'Add Plan'?>">
        <i class="fa <?= !empty($currentValue) ? 'fa-pencil-alt' : 'fa-plus-square'?>"></i>
    </a>
    <form 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">

        <p class="mb-2"><b>Plan</b></p>

        <?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" auto-paste="[data-field-name=value]"><?= $previousValue ?></div>
        </div>
        <?php endif; ?>

        <div class="mb-2">
            <div note-rte
                 class="form-group mb-2 border-left border-right rte-holder"
                 data-field-name="value"><?= $currentValue ?></div>
        </div>
        <div>
            <button submit class="btn btn-sm btn-primary mr-2"><i class="fa fa-save"></i></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="px-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; ?>
