<?php

use App\Models\Point;

/** @var \App\Models\Client $patient */

$interventions = Point::getPlanPointsOfCategory($patient, 'INTERVENTION', $note);
$intakeOrVisit = 'PLAN';

$grouped = [];
foreach($interventions as $intervention) {
    if($intervention->is_removed && $intervention->is_removed_due_to_entry_error) continue;
    if(!isset($grouped[$intervention->data->category])) {
        $grouped[$intervention->data->category] = [];
    }
    $grouped[$intervention->data->category][] = $intervention;
}

$numRelevant = 0;
?>

<?php if (!count($interventions)): ?>
    <div class="text-secondary">-</div>
<?php else: ?>
    <?php foreach($grouped as $k => $group): ?>
        <b><?= $k ?></b>
        <?php foreach($group as $intervention): ?>
            <div class="mb-2 pl-3">
                <div class="d-flex align-items-baseline">
                    @if($rel)
                        <span class="text-info mr-1">
                            <i class="fa fa-star"></i>
                        </span>
                        <?php $numRelevant++; ?>
                    @endif
                    <div class="<?= $intervention->is_removed ? 'strike-through' : '' ?>">
                        <?= !!@($intervention->data->intervention) ? @($intervention->data->intervention) : '' ?>
                        <br>
                        <span class="text-secondary"><?= !!@($intervention->data->description) ? @($intervention->data->description) : '' ?></span>
                    </div>
                    <?php if ($intervention->is_removed): ?>
                        @if($intervention->removal_reason_category === 'DURING_VISIT')
                            <span class="ml-2 text-sm text-secondary">Removed during visit</span>
                        @elseif($intervention->removal_reason_category === 'ON_INTAKE')
                            <span class="ml-2 text-sm text-secondary">Historic</span>
                        @endif
                    <?php elseif ($intervention->added_in_note_id === $note->id): ?>
                        <?php if ($intervention->addition_reason_category === 'DURING_VISIT'): ?>
                        <span class="ml-2 text-sm text-success">* Added during visit</span>
                        <?php else: ?>
                            <span class="ml-2 text-sm text-info">* Added on intake</span>
                        <?php endif;?>
                    <?php endif; ?>
                </div>
                <?php $review = $intervention->childPlanAddedInNote($note); ?>
                <?php if(!!$review): ?>
                <div class="pl-3 mt-1">
                    <div class="text-secondary font-weight-bold">Plan</div>
                    <div>{!! $review->data->value !!}</div>
                </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    <?php endforeach; ?>
    @if($numRelevant)
        <div class="d-flex align-items-baseline text-info mr-1 mt-3">
            <span class="text-sm mr-1"><i class="fa fa-star text-sm"></i></span>
            <span class="text-sm">Interventions relevant to this note</span>
        </div>
    @endif
<?php endif; ?>

