<div class="table-responsive">
	<table class="table p-0 m-0 table-sm border-top border-bottom text-nowrap">
		<thead class="bg-light">
			<tr>
				<th class="border-0 width-90px">#</th>
				<th class="border-0">Name</th>
				<th class="border-0">DOB</th>
				<th class="border-0">Age</th>
				<th class="border-0">Sex</th>
				<th class="border-0">BMI</th>
				<th class="border-0">Insurance</th>
				<th class="border-0">Last Visit</th>
				<th class="border-0">Next Appt.</th>
				<th class="border-0">Status</th>

				<th class="border-0">BP</th>
				<th class="border-0"> <i class="fa fa-heartbeat"></i> </th>
				<th class="border-0">Pulse</th>
				<th class="border-0">BP/Pulse Timestamp</th>
				<th class="border-0">Weight</th>
				<th class="border-0">Weight Timestamp</th>

				{{-- <th>Scale <i class="fa fa-battery"></i></th>--}}
				{{-- <th class="border-0">RPM</th>--}}
				{{-- <th class="border-0 d-none">Last Weight-In</th>--}}
				{{-- <th class="border-0 d-none">Last BP</th>--}}
				<th class="border-0">Assigned On</th>

				@if($pro->pro_type == 'ADMIN')
				<th class="border-0">MCP</th>
				<th class="border-0">Initiative</th>
				@endif
			</tr>
		</thead>
		<tbody>
			@foreach($patients as $patient)
			<tr>
				<td>
					<a native target="_blank" href="{{route('patients.view.dashboard', $patient)}}">
						{{$patient->chart_number}}
					</a>
					<span class="on-hover-show left d-inline-block on-hover-opaque">
						<i class="fa fa-info-circle ml-1"></i>
						<div class="on-hover-content py-2 pl-3">
							Created: <b class="m-0">{{friendly_date_time($patient->created_at)}}</b>
						</div>
					</span>
				</td>
				<td>
					<div class="d-flex align-items-center flex-nowrap">
						<span>{{$patient->displayName()}}</span>
						@if(count($patient->activeNotes))
							<a href="#" class="btn-toggle-notes-row ml-3 on-hover-opaque text-sm" onclick="$(this).closest('tr').next('.notes-row').toggleClass('d-none'); return false">
								<i class="fa fa-file-alt"></i> <i class="fa fa-sort"></i>
								{{count($patient->activeNotes)}}
							</a>
						@endif
					</div>
				</td>
				<td>{{ 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-none d-dflex 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>
					{{ friendly_date($patient->most_recent_completed_mcp_note_date) }}
					{{-- {{$patient->lastMcpAppointment ? friendly_date_time($patient->lastMcpAppointment->raw_date.' '.$patient->lastMcpAppointment->raw_start_time) : '-'}}--}}
				</td>
				<td>{{$patient->nextMcpAppointment ? friendly_date_time($patient->nextMcpAppointment->raw_date.' '.$patient->nextMcpAppointment->raw_start_time) : '-'}}</td>
				<td>{{$patient->nextMcpAppointment ? $patient->nextMcpAppointment->status : '-'}}</td>

				<td>
					@if($patient->most_recent_cellular_bp_measurement_at)
					{{ $patient->most_recent_cellular_bp_sbp_mm_hg }} / {{ $patient->most_recent_cellular_bp_dbp_mm_hg }}
					@endif
				</td>

				<td><?= $patient->most_recent_cellular_bp_value_irregular ? '<i class="fa fa-heartbeat"></i>' : '' ?></td>

				<td>{{ $patient->most_recent_cellular_bp_value_pulse }}</td>

				<td>{{ friendlier_date_time($patient->most_recent_cellular_bp_measurement_at, false) }}</td>

				<td>{{ $patient->most_recent_cellular_weight_value ? round($patient->most_recent_cellular_weight_value, 2) : '--' }}</td>

				<td>{{ friendlier_date_time($patient->most_recent_cellular_weight_measurement_at, false) }}</td>


				<td class="d-none text-nowrap">
					<?php $m = $patient->lastMeasurementOfType('Wt. (lbs.)'); ?>
					{{$m && $m->value ? round($m->value, 2) : '-'}}
				</td>
				<td class="d-none text-nowrap">
					<?php $m = $patient->lastMeasurementOfType('BP'); ?>
					{{$m && $m->value ? $m->value : '-'}}
				</td>
				<td>{{$patient->getMcpAssignedOn()}}</td>
				@if($pro->pro_type == 'ADMIN')
				<td>{{@$patient->mcp ? $patient->mcp->displayName() : '--'}}</td>
				<td>{{$patient->initiative}}</td>
				@endif
			</tr>
				@if(count($patient->activeNotes))
					<tr class="notes-row bg-light">
						<td></td>
						<td colspan="20">
							<div class="my-2">
								@if(count($patient->activeNotes))
									Notes for <b>{{$patient->displayName()}}</b>
									<table class="table table-sm table-striped table-bordered mb-0 mt-2">
										<thead class="bg-light">
										<tr>
											<th class="border-0 text-secondary">Effective Date</th>
											<th class="border-0 text-secondary">New / Follow-up</th>
											<th class="border-0 text-secondary">Method</th>
											<th class="border-0 text-secondary">HCP</th>
											<th class="border-0 text-secondary">NA</th>
											<th class="border-0 text-secondary">HCP Signed</th>
											<th class="border-0 text-secondary">ICDs</th>
											<th class="border-0 text-secondary">Bills</th>
										</tr>
										</thead>
										<tbody>
										@foreach ($patient->activeNotes as $note)
											<tr class="{{ $note->is_cancelled ? 'cancelled-item always-clickable' : '' }}">
												<td>
													<a native target="_blank" href="/patients/view/{{ $patient->uid }}/notes/view/{{ $note->uid }}">
														{{ friendly_date_time($note->effective_dateest, false) }}
													</a>
													@if(!!$note->visitTemplate)
														<span class="text-info font-weight-bold">*</span>
													@endif
													<span class="ml-1">{{ $note->is_cancelled ? '[cancelled]' : '' }}</span>
												</td>
												<td>
													{{ $note->new_or_fu_or_na === 'NEW' ? 'New' : 'Follow-up' }}
												</td>
												@if($pro->pro_type == 'ADMIN')
													<td>
														{{ noteMethodDisplay($note->method) }}
													</td>
												@endif
												<td>
													{{$note->hcpPro ? $note->hcpPro->name_display: '-'}}
												</td>
												@if($pro->pro_type == 'ADMIN')
													<td>
														{{$note->allyPro ? $note->allyPro->name_display: '-'}}
													</td>
												@endif
												<td>
													{!! $note->hcpPro && $note->is_signed_by_hcp ? '<b class="text-success">Yes</b>' : 'No' !!}
												</td>
												<td class="max-width-200px">
													@if(count($note->reasons()))
														@foreach ($note->reasons() as $reason)
															<div class="mb-1">
																<span>{{ $reason->code }}</span>
																<span class="text-secondary text-sm">{{ $reason->description}}</span>
															</div>
														@endforeach
													@else
														-
													@endif
												</td>
												<td>
													@if(!count($note->bills))
														<div class="alert alert-warning">No bills.</div>	
													@endif
													@foreach($note->bills as $bill)
													<div>{{ $bill->code}} | {{ $bill->number_of_units }} | {{ $bill->hcp_expected_payment_amount }}</div>
													@endforeach
												</td>
											</tr>
										@endforeach
										</tbody>
									</table>
								@else
									<span class="text-secondary">No active notes for <b>{{$patient->displayName()}}</b></span>
								@endif
							</div>
						</td>
					</tr>
				@endif
			@endforeach

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

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