<div class="table-responsive">
<table class="table table-sm table-striped p-0 m-0">
	<thead class="bg-light border-top">
		<tr>
			<th class="border-0">#</th>
			<th class="border-0">@include('app.practice-management._sort_header', ['route' => route("rd.patients"), 'label' => 'Name', 'key' => 'name_first'])</th>
			<th class="border-0">@include('app.practice-management._sort_header', ['route' => route("rd.patients"), 'label' => 'DOB', 'key' => 'dob'])</th>
			<th class="border-0">@include('app.practice-management._sort_header', ['route' => route("rd.patients"), 'label' => 'Age', 'key' => 'age_in_years'])</th>
			<th class="border-0">@include('app.practice-management._sort_header', ['route' => route("rd.patients"), 'label' => 'Sex', 'key' => 'sex'])</th>
			<th class="border-0">@include('app.practice-management._sort_header', ['route' => route("rd.patients"), 'label' => 'BMI', 'key' => 'usual_bmi_min'])</th>
			<th class="border-0">Insurance</th>
			<th class="border-0">MCP</th>
			<th class="border-0">RPM</th>
		</tr>
	</thead>
	<tbody>
		@foreach($patients as $patient)
		<?php
			$patient->most_recent_weight_measurement_id = 68434;
		?>
		<tr>
			<td>
				<a native target="_blank" href="{{route('patients.view.dashboard', $patient)}}">
					{{$patient->chart_number}}
				</a>
			</td>
			<td>
				{{$patient->displayName()}}
			</td>
			<td class="text-nowrap">{{ friendly_date_time($patient->dob, false) }}</td>
			<td>{{ $patient->age_in_years ?  $patient->age_in_years : '-' }}</td>
			<td>{{ $patient->sex }}</td>
			<td>
				<div class="d-flex flex-column">
					@if($patient->usual_bmi_min && $patient->usual_bmi_max)
					<small class="text-muted">BMI (Usual): <b>{{ $patient->usual_bmi_min }}</b> {{ $patient->usual_bmi_min_category }} to <b>{{ $patient->usual_bmi_max }}</b> {{ $patient->usual_bmi_max_category }}</small>
					@endif
					@if($patient->ideal_bmi)
					<small class="text-muted">BMI (Ideal) <b>{{ $patient->ideal_bmi }}</b> {{ $patient->ideal_bmi_category }}</small>
					@endif
				</div>
			</td>
			<td>
				@include('app.patient.coverage_column_renderer', ['patient'=>$patient])
			</td>
			<td>
				<div class="d-flex flex-column">
					<span>{{ $patient->mcp ? $patient->mcp->displayName() :'' }}</span>	
					<span>Status: 
						@if($patient->has_mcp_done_onboarding_visit === 'YES')
						<i class="fas fa-check-circle text-success" data-toggle="tooltip" data-placement="bottom" title="YES"></i>
						@elseif($patient->has_mcp_done_onboarding_visit === 'NO')
						<i class="fas fa-times-circle text-danger" data-toggle="tooltip" data-placement="bottom" title="NO"></i>
						@elseif($patient->has_mcp_done_onboarding_visit === 'UNKNOWN')
							<i class="fas fa-question-circle text-secondary" data-toggle="tooltip" data-placement="bottom" title="UNKNOWN"></i>
						@endif
					</span>				
					@if($patient->mostRecentCompletedMcpNote)
						<span>Last Encounter:  
						<a href="/patients/view/{{ $patient->mostRecentCompletedMcpNote->client->uid }}/notes/view/{{ $patient->mostRecentCompletedMcpNote->uid }}" class="font-weight-bold">
                                {{ friendly_date_time($patient->mostRecentCompletedMcpNote->effective_dateest, false) }}
                            </a>
						</span>
					@endif
					@if($patient->next_mcp_appointment_date)
						<span class="text-muted">Next Appt. <b>{{ friendly_date_time($patient->next_mcp_appointment_date) }}</b></span>
					@endif
					<?php $recentNote = $patient->mostRecentCompletedMcpNote; ?>
					@if($recentNote)
					<div>
						Note: <a href="/patients/view/{{ $patient->uid }}/notes/view/{{ $recentNote->uid }}" class="font-weight-bold">
                        {{ friendly_date_time($recentNote->effective_dateest, false) }}
                    </a>
					</div>
					@endif
				</div>
			</td>
			<td>
			   <div class="">
				   <div class="on-hover-show d-inline-block">
					   <span>Enrolled:
						   @if($patient->is_enrolled_in_rm === 'YES') {{-- correct --}}
							   <i class="fas fa-check-circle text-success" data-toggle="tooltip" data-placement="bottom" title="YES"></i>
						   @elseif($patient->is_enrolled_in_rm === 'NO') {{-- correct --}}
							   <i
									   class="fas fa-times-circle text-danger"
									   tabindex="0"
									   data-toggle="popover"
									   title="No"
									   data-target="#enrollment-{{ $patient->uid }}"
							   ></i>
						   @elseif($patient->is_enrolled_in_rm === 'UNKNOWN') {{-- correct --}}
							   <i
									   tabindex="0"
									   data-toggle="popover"
									   title="Uknown"
									   data-target="#enrollment-{{ $patient->uid }}"
									   class="fas fa-question-circle text-secondary"
									   popover
							   ></i>
						   @endif
						</span>
					   	<div id="enrollment-{{ $patient->uid }}" class="on-hover-content">
						   	<h6 class="mt-2 font-weight-bold">{{ $patient->is_enrolled_in_rm }}</h6> {{-- correct --}}
							<span>Is Eligible for RM: {{ $patient->is_eligible_for_rm }}</span><br>
							<span>Why not eligible: {{ $patient->why_not_eligible_for_rm_category ?? '---' }}</span><br>
							<span>{{ $patient->why_not_eligible_for_rm_memo }}</span>
						</div>
				   </div>
				<?php $m = $patient->lastMeasurementOfType('Wt. (lbs.)');?>
				@if($m)
				<span class="d-block mt-1">Wt. (lbs.) {{$m && $m->value ? round($m->value, 2) : '-'}}</span>
				@endif
				<?php $bp = $patient->lastMeasurementOfType('BP'); ?>
				@if($bp)
				<span class="d-block mt-1">BP. {{$bp && $bp->value ? $bp->value : '-'}}</span>
				@endif
			   </div>
			</td>
		</tr>
		@endforeach

		@if(count($patients) === 0)
		<tr>
			<td colspan="9">No records found!</td>
		</tr>
		@endif
	</tbody>

</table>
</div>
<div class="p-3">
	{{$patients->withQueryString()->links()}}
</div>