@extends ('layouts/template')

@section('content')

    <div class="p-3 mcp-theme-1">

        <div class="card">

            <div class="card-header px-2 py-1 d-flex align-items-center">
                <strong class="mr-4 flex-grow-1">
                    <i class="fas fa-wifi"></i>
                    Cellular Device Manager
                </strong>
                <div class="width-200px ml-auto">
                    <select class="form-control form-control-sm"
                            provider-search data-pro-uid="{{$proUid}}"
                            onchange="fastLoad('/practice-management/cellular-device-manager/' + this.value, true, false, false)">
                        <option value="" {{ $proUid === '' ? 'selected' : '' }}>All Pros</option>
                    </select>
                </div>
            </div>
            <style>
                .cellular-bp {
                    background-color: #d7d9f2;
                }
                .cellular-weight {
                    background-color: #ffe8d4;
                }
            </style>
            <div class="card-body p-0">
                <table class="table table-sm table-striped p-0 m-0">
                    <colgroup>
                        <col span="8">
                        <col span="4" class="cellular-bp">
                        <col span="4" class="cellular-weight">
                    </colgroup>
                    <thead class="bg-light">
                    <tr>
                        <th class="px-3 border-0">Patient</th>
                        <th class="border-0">DOB</th>
                        <th class="border-0">MCP</th>
                        <th class="border-0">RME</th>
                        <th class="border-0">RMM</th>
                        <th class="border-0">Coverage</th>
                        <th class="border-0">SMS Notifs</th>

                        <th class="border-0" colspan="4">Cellular BP</th>
                        <th class="border-0" colspan="4">Cellular Weight</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($clients as $client)
                        <tr>

                            <td class="pl-3">
                                <a href="{{route('patients.view.dashboard', ['patient' => $client])}}"><b>{{ $client->displayName() }}</b></a>
                                <div class="mt-1 text-secondary text-sm">{{friendlier_date_time($client->created_at)}}</div>
                            </td>
                            <td>{{ friendlier_date_time($client->dob, false) }}</td>
                            <td>{{ $client->mcp ? $client->mcp->displayName() : '' }}</td>
                            <td>{{ $client->rme ? $client->rme->displayName() : '' }}</td>
                            <td>{{ $client->rmm ? $client->rmm->displayName() : '' }}</td>

                            <td>
                                <?php $coverageStatus = $client->getPrimaryCoverageStatus(); ?>
                                @if($coverageStatus === 'YES')
                                    <div class="text-nowrap">Covered <i class="fa fa-check-circle text-success"></i>
                                    </div>
                                @elseif($coverageStatus === 'NO')
                                    <div class="text-nowrap">Not Covered <i class="fa fa-times text-danger"></i></div>
                                @else
                                    <div class="text-nowrap">Unknown <i
                                                class="fa fa-exclamation-triangle text-warning-mellow"></i></div>
                                @endif
                                <?php $coverage = $client->getPrimaryCoverage(); ?>
                                @if($coverage)
                                    {{$coverage->insuranceDisplayName()}}
                                @endif
                            </td>

                            <td>{{$client->send_sms_on_bdt_measurement ? 'Yes' : ''}}</td>

                            <td colspan="4">
                                <?php
                                $bpDevice = $client->firstCellularBPDevice();
                                $bpMeasurements = [];
                                if ($bpDevice) {
                                    $records = \App\Models\Measurement
                                        ::where('imei', $bpDevice->device->imei)
                                        ->where('client_id', $client->id)
                                        ->where('is_cellular_zero', false)
                                        ->orderBy('ts')
                                        ->get();
                                    if(count($records)) {
                                        $bpMeasurements["first"] = $records[0];
                                        $bpMeasurements["last"] = $records[count($records) - 1];
                                        $bpMeasurements["count"] = count($records);
                                    }
                                    // get num measurement days
                                    $dayCount = [];
                                    for($i = 0; $i<count($records); $i++) {
                                        $date = friendly_date_short_with_tz_from_timestamp_divide1000($records[$i]->ts);
                                        if(!in_array($date, $dayCount)) {
                                            $dayCount[] = $date;
                                        }
                                    }
                                }
                                ?>
                                @if($bpDevice)
                                    <?php $bpSO = $client->supplyOrderForCellularBPDevice(); ?>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Ordered:</span>
                                        <span class="ml-3">{{$bpSO ? 'Yes' : 'No'}}</span>
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Sent:</span>
                                        <span class="ml-3">{{$bpSO && $bpSO->shipment && $bpSO->shipment->status === 'DISPATCHED' ? 'Yes' : 'No'}}</span>
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">IMEI:</span>
                                        <span class="ml-3">{{$bpDevice->device->imei}}</span>
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">First Use:</span>
                                        @if(@$bpMeasurements["first"])
                                            <span class="ml-3">{{ $bpMeasurements["first"]->sbp_mm_hg . '/' . $bpMeasurements["first"]->dbp_mm_hg . ' mmHg' }}</span>
                                            <span class="ml-3 text-secondary">{{date("Y-m-d", $bpMeasurements["first"]->ts/1000)}}</span>
                                            <span class="ml-3">{{date_diff(date_create(date("Y-m-d", $bpMeasurements["first"]->ts/1000)), date_create('now'))->days}} days ago</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Last Use:</span>
                                        @if(@$bpMeasurements["last"])
                                            <span class="ml-3">{{ $bpMeasurements["last"]->sbp_mm_hg . '/' . $bpMeasurements["last"]->dbp_mm_hg . ' mmHg' }}</span>
                                            <span class="ml-3 text-secondary">{{date("Y-m-d", $bpMeasurements["last"]->ts/1000)}}</span>
                                            <span class="ml-3">{{date_diff(date_create(date("Y-m-d", $bpMeasurements["last"]->ts/1000)), date_create('now'))->days}} days ago</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Measurements:</span>
                                        @if(@$bpMeasurements["count"])
                                            <span class="ml-3">{{ $bpMeasurements["count"] }}</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Measurement Days:</span>
                                        @if(@$dayCount)
                                            <span class="ml-3">{{ count($dayCount) }}</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                @endif
                            </td>

                            <td colspan="4">
                                <?php
                                $weightScale = $client->firstCellularWeightDevice();
                                $weightMeasurements = [];
                                if ($weightScale) {
                                    $records = \App\Models\Measurement
                                        ::where('imei', $weightScale->device->imei)
                                        ->where('client_id', $client->id)
                                        ->where('is_cellular_zero', false)
                                        ->orderBy('ts')
                                        ->get();
                                    if(count($records)) {
                                        $weightMeasurements["first"] = $records[0];
                                        $weightMeasurements["last"] = $records[count($records) - 1];
                                        $weightMeasurements["count"] = count($records);
                                    }
                                    // get num measurement days
                                    $dayCount = [];
                                    for($i = 0; $i<count($records); $i++) {
                                        $date = friendly_date_short_with_tz_from_timestamp_divide1000($records[$i]->ts);
                                        if(!in_array($date, $dayCount)) {
                                            $dayCount[] = $date;
                                        }
                                    }
                                }
                                ?>
                                @if($weightScale)
                                    <?php $wsSO = $client->supplyOrderForCellularWeightScale(); ?>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Ordered:</span>
                                        <span class="ml-3">{{$wsSO ? 'Yes' : ''}}</span>
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Sent:</span>
                                        <span class="ml-3">{{$wsSO && $wsSO->shipment && $wsSO->shipment->status === 'DISPATCHED' ? 'Yes' : 'No'}}</span>
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">IMEI:</span>
                                        <span class="ml-3">{{$weightScale->device->imei}}</span>
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">First Use:</span>
                                        @if(@$weightMeasurements["first"])
                                            <span class="ml-3">{{ round($weightMeasurements["first"]->value, 2) . ' lbs' }}</span>
                                            <span class="ml-3 text-secondary">{{date("Y-m-d", $weightMeasurements["first"]->ts/1000)}}</span>
                                            <span class="ml-3">{{date_diff(date_create(date("Y-m-d", $weightMeasurements["first"]->ts/1000)), date_create('now'))->days}} days ago</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Last Use:</span>
                                        @if(@$weightMeasurements["last"])
                                            <span class="ml-3">{{ round($weightMeasurements["last"]->value, 2) . ' lbs' }}</span>
                                            <span class="ml-3 text-secondary">{{date("Y-m-d", $weightMeasurements["last"]->ts/1000)}}</span>
                                            <span class="ml-3">{{date_diff(date_create(date("Y-m-d", $weightMeasurements["last"]->ts/1000)), date_create('now'))->days}} days ago</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Measurements:</span>
                                        @if(@$weightMeasurements["count"])
                                            <span class="ml-3">{{ $weightMeasurements["count"] }}</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                    <div class="d-flex align-items-baseline mb-1">
                                        <span class="width-150px text-sm">Measurement Days:</span>
                                        @if(@$dayCount)
                                            <span class="ml-3">{{ count($dayCount) }}</span>
                                        @else
                                            -
                                        @endif
                                    </div>
                                @endif
                            </td>

                            {{--
                            <td>{{ $client->firstCellularBPDevice() ? 'Yes' : '' }}</td>
                            <td>{{ $client->getFirstCellularBPMeasurementAt()  }}</td>
                            <td>{{ $client->getLatestCellularBPMeasurementAt() }}</td>
                            <td>{{ $client->getTotalCellularBPMeasurements() }}</td>

                            <td>{{ $client->firstCellularWeightDevice() ? 'Yes' : '' }}</td>
                            <td>{{ $client->getFirstCellularWeightMeasurementAt()  }}</td>
                            <td>{{ $client->getLatestCellularWeightMeasurementAt() }}</td>
                            <td>{{ $client->getTotalCellularWeightMeasurements() }}</td>
                            --}}

                        </tr>
                    @endforeach
                    </tbody>
                </table>
                <div class="border-top px-3 pt-3">
                    {{$clients->links()}}
                </div>
            </div>
        </div>
    </div>

@endsection
