<?php

use App\Models\Point;

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

$points = Point::getPointsOfCategoryExceptEntryError($patient, $note, 'MEDICATION');
$tracker = Point::getGlobalSingletonOfCategory($patient, 'NO_KNOWN_TRACKER');
$numRelevant = 0;
$plural = 'Medications';
?>

<?php if($tracker && @($tracker->data->no_known_medications)): ?>
    <div class="text-secondary">No known medications</div>
<?php endif; ?>
<?php if(!$tracker || !@($tracker->data->no_known_medications)): ?>
<?php if (!count($points)): ?>
    <div class="text-secondary">-</div>
<?php else: ?>
	<table class="table table-bordered table-xs table-cage mb-0">
		<thead>
		<tr>
			<th class="pl-0 pr-1 py-1 border-top-0">#</th>
			<th class="py-1 px-2 border-top-0">Name</th>
			<th class="py-1 px-2 border-top-0">Frequency</th>
			<th class="py-1 px-2 border-top-0">Experience</th>
			<th class="py-1 px-2 border-top-0"></th>
		</tr>
		</thead>
    <?php $j = 0; foreach ($points as $point): $j++; ?>
        <?php $rel = $point->relevanceToNote($note); ?>
		<?php
		$review = null;
		if($point->lastChildReview) {
			$review = json_decode($point->lastChildReview->data);
		}
		?>
	<tr>
		<td class="width-22px pl-0 pr-1">
			@if($rel)
				<?php $numRelevant++; ?>
			@endif
			{{ $j }}.
		</td>
		<td class="px-2">
			<div class=" <?= $point->is_removed ? 'strike-through' : '' ?> grow-till-300px">
				<?= !!@($point->data->name) ? @($point->data->name) : '-' ?>
			</div>
		</td>
		<td class="px-2">
			<div class="grow-till-300px">
				@if(@$review && isset($review->value))
					{{trim(strip_tags($review->value))}}
				@endif
			</div>
		</td>
		<td class="width-300px">
			<div class="d-flex align-items-baseline pl-2">
				@if(@$review && isset($review->experience) && !!trim(strip_tags($review->experience)))
					<span class="pr-2">{{trim(strip_tags($review->experience))}}</span>
				@endif
				<span class="text-nowrap">
				<?php if ($point->is_removed): ?>
				@if($point->removal_reason_category === 'DURING_VISIT')
					@if ($point->removed_in_note_id === $note->id)
						<span class="text-sm text-secondary">Removed during visit</span>
					@else
						<span class="text-sm text-secondary">Removed on a previous visit</span>
					@endif
				@elseif($point->removal_reason_category === 'ON_INTAKE')
					<span class="text-sm text-secondary">Historic</span>
				@endif
				<?php elseif ($point->added_in_note_id === $note->id): ?>
					<?php if ($point->addition_reason_category === 'DURING_VISIT'): ?>
						<span class="text-sm text-success">Added during visit</span>
					<?php else: ?>
						<span class="text-sm text-info">Added on intake</span>
					<?php endif;?>
				<?php endif; ?>
				</span>
			</div>
		</td>
		<td></td>
	</tr>
    <?php endforeach; ?>
	</table>
<?php endif; ?>
<?php endif; ?>
