@extends ('layouts.patient')

@section('inner-content')

    <h4 class="font-weight-bold mb-3 font-size-14" id="rm-setup">Remote Monitoring</h4>

    @include('app.patient.note.rm-setup')
    @if($patient->is_enrolled_in_rm == 'YES' && !($patient->is_weight_and_bmi_settings_updated || $patient->is_bp_and_pulse_updated))
    <hr>
      <p class="alert alert-warning border-warning text-dark mb-2">
        <i class="fa fa-exclamation-triangle mr-1"></i>  You need to complete the patient's <a href="{{ route('patients.view.vitals-settings', ['patient' => $patient]) }}" class="font-weight-bold">Vitals settings</a> before ordering cellular weight scale and/or a cellular BP device.
      </p>
    @endif
    @if($patient->is_weight_and_bmi_settings_updated || $patient->is_bp_and_pulse_updated)
    <hr>
    <link href="/select2/select2.min.css" rel="stylesheet" />
    <script src="/select2/select2.min.js"></script>
    <div id="patient-devices">
        <div class="d-flex align-items-center pb-2">
            <h4 class="font-weight-bold mb-0">Devices</h4>
            @if(count($devices) && $pro->pro_type === 'ADMIN')
                <span class="mx-2 text-secondary">|</span>
                <div moe>
                    <a start show>Add</a>
                    <form url="/api/clientBdtDevice/create" class="mcp-theme-1">
                        <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Device</label>
                            <select name="deviceUid" class="form-control form-control-sm">
                                <option value=""> --select-- </option>
                                @foreach($devices as $device)
                                    <option value="{{$device->uid}}">
                                        {{$device->imei}} ({{$device->category}})
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Instructions</label>
                            <input type="text" name="instructions" class="form-control form-control-sm">
                        </div>
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Internal Memo</label>
                            <input type="text" name="internalMemo" class="form-control form-control-sm">
                        </div>
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Status</label>
                            <input type="text" name="status" class="form-control form-control-sm">
                        </div>
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Status Memo</label>
                            <input type="text" name="statusMemo" class="form-control form-control-sm">
                        </div>
                        <div>
                            <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                            <button cancel class="btn btn-sm btn-default border">Cancel</button>
                        </div>
                    </form>
                </div>
            @endif
        </div>
        <table class="table table-striped table-sm table-bordered mb-0">
            @if($patient->devices && count($patient->devices))
                <thead class="bg-light">
                <tr>
                    <th class="border-0 text-secondary">Created</th>
                    <th class="border-0 text-secondary">Category</th>
                    <th class="border-0 text-secondary">IMEI</th>
                    <th class="border-0 text-secondary">Last Measurement</th>
                    <th class="border-0 text-secondary"></th>
                    @if($pro->pro_type === 'ADMIN')
                    <th class="border-0 text-secondary"></th>
                    @endif
                </tr>
                </thead>
                <tbody>
                @foreach($patient->devices as $device)
                    <tr>
                        <td class="text-nowrap">{{ friendly_date_time($device->device->created_at) }}</td>
                        <td>{{ $device->device->category }}</td>
                        <td><pre class="m-0">{{ $device->device->imei }}</pre></td>
                        <td>
                            <?php $lastMeasurement = $device->lastDeviceMeasurement(); ?>
                            @if($lastMeasurement)
                                @if($lastMeasurement->is_cellular_zero)
                                    <i class="font-size-11 fa fa-rss"></i>
                                @elseif($lastMeasurement->label === 'BP')
                                    {{ $lastMeasurement->sbp_mm_hg }} / {{ $lastMeasurement->dbp_mm_hg }}
                                @elseif($lastMeasurement->label === 'Wt. (lbs.)')
                                    {{ round($lastMeasurement->numeric_value, 2) }} lbs
                                @else
                                    {{ $lastMeasurement->value }}
                                @endif
                                <div>
                                    <i class="far fa-calendar-check"></i> <span class="text-secondary">{{ friendly_date_time($lastMeasurement->created_at) }}</span>
                                </div>
                            @else
                                <small class="text-muted">-</small>
                            @endif
                        </td>
                        <td>
                            <div moe relative>
                                <a start show class="on-hover-opaque">Deactivate</a>
                                <form url="/api/clientBdtDevice/deactivate" right>
                                    <input type="hidden" name="uid" value="{{ $device->uid }}">
                                    <p class="small">Are you sure you want to deactivate this device?</p>
                                    <div class="d-flex align-items-center">
                                        <button class="btn btn-sm btn-danger mr-2" submit>Deactivate</button>
                                        <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                    </div>
                                </form>
                            </div>
                        </td>
                        @if($pro->pro_type === 'ADMIN')
                        <td class="text-nowrap">
                            @include('app.generic-bills.modal', ['entityType' => 'ClientBDTDevice', 'entityUid' => $device->uid])
                        </td>
                        @endif
                    </tr>
                @endforeach
                </tbody>
            @else
                <tbody>
                <tr>
                    <td class="text-secondary p-3">No active devices for this patient</td>
                </tr>
                </tbody>
            @endif
        </table>
        <hr>
        <h2>Deactivated Devices</h2>
        <table class="table table-striped table-sm table-bordered mb-0">
            @if($patient->deactivatedDevices && count($patient->deactivatedDevices))
                <thead class="bg-light">
                <tr>
                    <th class="border-0 text-secondary">Created</th>
                    <th class="border-0 text-secondary w-25">Category</th>
                    <th class="border-0 text-secondary w-50">IMEI</th>
                    <th class="border-0 text-secondary"></th>
                </tr>
                </thead>
                <tbody>
                @foreach($patient->deactivatedDevices as $deactivatedDevice)
                    <tr>
                        <td>{{ friendly_date_time($deactivatedDevice->device->created_at) }}</td>
                        <td>{{ $deactivatedDevice->device->category }}</td>
                        <td><pre class="m-0">{{ $deactivatedDevice->device->imei }}</pre></td>
                        <td>
                            <div moe relative>
                                <a start show>Reactivate</a>
                                <form url="/api/clientBdtDevice/reactivate" right>
                                    <input type="hidden" name="uid" value="{{ $deactivatedDevice->uid }}">
                                    <p class="small">Are you sure you want to reactivate this device?</p>
                                    <div class="d-flex align-items-center">
                                        <button class="btn btn-sm btn-success mr-2" submit>Reactivate</button>
                                        <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                    </div>
                                </form>
                            </div>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            @else
                <tbody>
                <tr>
                    <td class="text-secondary p-3">No deactivated devices for this patient</td>
                </tr>
                </tbody>
            @endif
        </table>
    </div>
    <script>
        (function() {
            function init() {
                $('select[name="deviceUid"]').select2({
                    width: '100%'
                });
            }
            addMCInitializer('patient-devices', init, '#patient-devices');
        }).call(window);
    </script>
    <hr>
    <div>
       @include('app.patient.partials.client_bp_weight_phone_number_status')
    </div>
    @endif
@endsection
