<?php

use App\Models\Point;

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

$interventions = Point::getIntakePointsOfCategory($patient, 'INTERVENTION', $note);

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

<?php if (!count($grouped)): ?>
    <div class="text-secondary">-</div>
<?php else: ?>
    <?php foreach($grouped as $k => $group): ?>
        <div class="">
        <b><?= $k ?></b>
        <table class="table table-bordered table-xs table-cage mb-0">
            <?php foreach($group as $intervention): ?>
                <tr>
                    <td class="px-2" style="max-width: 300px;">
                        <div class="font-weight-bold <?= $intervention->is_removed ? 'strike-through' : '' ?>">
                            <?= !!@($intervention->data->intervention) ? @($intervention->data->intervention) : '-' ?>
                            <br>
                            <span class="text-secondary"><?= !!@($intervention->data->description) ? @($intervention->data->description) : '' ?></span>
                        </div>
                    </td>
                    <td class="px-2" style="max-width: 300px;">
                        <div>
                            <?php $review = $intervention->childReviewAddedInNote($note); ?>
                            <?php if(!!$review): ?>
                            <div class="pl-3 mt-1">
                                <div class="text-secondary font-weight-bold">Subjective</div>
                                <div>{!! $review->data->value !!}</div>
                            </div>
                            <?php endif; ?>
                        </div>
                        <div class="d-flex align-items-baseline">                        
                            <?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): ?>
                            <span class="ml-2 text-sm text-success">* Added on intake</span>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
        </table>
        </div>
    <?php endforeach; ?>
<?php endif; ?>