@extends ('layouts/template')

@section('content')
<div class="p-3 mcp-theme-1" id="patients-list">
    <div class="card">

        <div class="card-header px-2 py-1 d-flex align-items-center">
            <strong class="mr-4">
                <i class="fas fa-weight"></i>
                Clients BDT Devices
            </strong>
        </div>

        <div class="card-body p-0">
            <div class="p-3">
                @include('app.practice-management.clients_bdt_devices_filters')
            </div>
            <table class="table table-sm table-striped border-top p-0 m-0">
                <thead class="bg-light">
                    <tr>
                        <th class="px-2 border-0">IMEI</th>
                        <th class="px-2 border-0">Client</th>
                        <th class="px-2 border-0">MCP</th>
                        <th class="px-2 border-0">Created</th>
                        <th class="px-2 border-0">Category</th>
                        <th class="px-2 border-0"># of Non-zero Measurements</th>
                        <!-- <th class="px-2 border-0">Most Recent Non-zero Measurement At</th> -->
                        <th class="px-2 border-0">Most Recent Measurement At</th>
                        <th class="px-2 border-0">Most Recent Measurement</th>
                        <th class="px-2 border-0">Status</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($devices as $device)
                    <?php 
                    $mcpName = $device->client->mcp ? implode(', ', array_filter([$device->client->mcp->name_last, $device->client->mcp->name_first])) : null;
                    ?>
                    <tr>
                        <td class="px-2">
                            <pre class="m-0">{{ $device->device->imei }}</pre>
                        </td>
                        <td>
                            <a target="_blank" native href="{{route('patients.view.dashboard', $device->client)}}">
                                {{$device->client->displayName()}}
                            </a>
                        </td>
                        <td>{{ $mcpName }}</td>
                        <td class="px-2 text-nowrap">{{ friendly_date_time($device->device->created_at) }}</td>
                        <td class="px-2">{{ $device->device->category }}</td>
                        <td class="px-2">{{ $device->num_of_measurements}}</td>
                        <!-- <td class="px-2">{{ friendly_date_time($device->most_recent_none_zero_measurement_at)}}</td> -->
                        <td class="px-2">{{ friendly_date_time($device->most_recent_measurement_at)}}</td>
                        <td class="px-2 d-flex align-items-center">
                            <?php $lastMeasurement = $device->mostRecentMeasurement; ?>
                            @if($lastMeasurement && $lastMeasurement->measurement)
                                @if($lastMeasurement->measurement->is_cellular_zero)
                                    <i class="font-size-11 fa fa-rss"></i>
                                @elseif($device->device->category === 'BP')
                                    {{ $lastMeasurement->measurement->sbp_mm_hg }} / {{ $lastMeasurement->measurement->dbp_mm_hg }}
                                @elseif($device->device->category === 'WEIGHT')
                                    {{ round($lastMeasurement->measurement->numeric_value, 2) }} lbs
                                @else
                                    {{ $lastMeasurement->measurement->value }}
                                @endif
                                <div class="ml-2">
                                    <i class="far fa-calendar-check"></i> <span class="text-secondary">{{ friendly_date_time($lastMeasurement->ts_date_time) }}</span>
                                </div>
                            @else
                                <h1>No measurement</h1>
                                <small class="text-muted">-</small>
                            @endif
                        </td>
                        <td>
                            @if($device->is_active)
                                <span class="text-success">Active</span>
                            @else
                                <span class="text-danger">Deactivated</span>
                            @endif
                        </td>
                    </tr>
                    @endforeach

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

            </table>
            <div class="ml-2 mt-2">
                {{ $devices->appends(request()->input())->links() }}
            </div>
        </div>
    </div>
</div>
@endsection