<?php

$vitalMap = [
    'heightInInches' => 'Ht. (in.)',
    'weightPounds' => 'Wt. (lbs.)',
    'temperatureF' => 'Temp. (F)',
    'pulseRatePerMinute' => 'Pulse',
    'respirationRatePerMinute' => 'Resp.',
    'pulseOx' => 'Pulse Ox.',
    'systolicBP' => 'SBP',
    'diastolicBP' => 'DBP',
    'smokingStatus' => 'Smoking Status',
    'bmi' => 'BMI'
];

$vitalLabels = ['Ht. (in.)','Wt. (lbs.)','Temp. (F)','Pulse','Resp.','Pulse Ox.','SBP','DBP','Smoking Status', 'BMI'];

$vitalValues = json_decode($patient->latest_measurements, true);

foreach($vitalLabels as $vitalLabel){
    if(!isset($vitalValues[$vitalLabel])){
        $vitalValues[$vitalLabel] = [
            'value' => '',
            'effectiveDate'=>''
        ];
    }
}

?>

<div moe-parent
     url="/embed/{{ $patient->uid }}/vitals/{{ @$selectable ? 1 : 0 }}"
     class="<?= !@$selectable ? 'mt-2' : 'border-bottom' ?> pb-1">
    <div class="d-flex align-items-center mb-2 py-2 <?= @$selectable ? 'px-2' : 'border-top' ?> border-bottom">

        @if(!@$selectable)
            <h6 class="my-0 font-weight-bold text-secondary">Vitals</h6>
            <span class="mx-2 text-secondary">|</span>
        @endif

        <div moe>
            <a start show class="py-0 font-weight-normal">Update</a>
            <form url="/api/measurement/updateVitals">
                <input type="hidden" name="clientUid" value="{{ $patient->uid }}">
                <label class="text-secondary text-sm mb-1">Ht. (in.)</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="heightInInches" value="{{$vitalValues['Ht. (in.)']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">Wt. (lbs.)</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="weightPounds" value="{{$vitalValues['Wt. (lbs.)']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">Temp. (F)</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="temperatureF" value="{{$vitalValues['Temp. (F)']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">Pulse</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="pulseRatePerMinute" value="{{$vitalValues['Pulse']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">Resp.</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="respirationRatePerMinute" value="{{$vitalValues['Resp.']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">Pulse Ox.</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="pulseOx" value="{{$vitalValues['Pulse Ox.']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">SBP</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="systolicBP" value="{{$vitalValues['SBP']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">DBP</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="diastolicBP" value="{{$vitalValues['DBP']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">Smoking Status</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="smokingStatus" value="{{$vitalValues['Smoking Status']['value']}}" placeholder="">
                </div>
                <label class="text-secondary text-sm mb-1">BMI</label>
                <div class="mb-2">
                    <input type="text" class="form-control form-control-sm" name="bmi" value="{{$vitalValues['BMI']['value']}}" placeholder="">
                </div>
                <div class="d-flex align-items-center">
                    <button class="btn btn-sm btn-primary mr-2" submit>Save</button>
                    <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                </div>
            </form>
        </div>

        {{--@if(!@$selectable)
        <span class="mx-2 text-secondary">|</span>
        <a start show class="py-0 font-weight-normal"
           href="/patients/view/{{ $patient->uid }}/measurements">
            View All
        </a>
        @endif--}}
    </div>
    <div class="px-2">
    <table class="table table-sm border-0 my-0">
        <tbody>
        @foreach($vitalLabels as $label)
            <tr>
                <td class="text-black p-0 border-0">
                    <div class="d-flex vital-item align-items-center">
                        @if(@$selectable)
                            <span class="mr-2 d-inline-flex align-items-center">
                                <input type="checkbox" class="my-0 vital-item-checkbox">
                            </span>
                        @endif
                        <div moe class="mr-2">
                            <a class="on-hover-opaque" start show title="Update">
                                <i class="font-size-11 fa fa-edit text-primary"></i>
                            </a>
                            <form url="/api/measurement/create">
                                <input type="hidden" name="clientUid" value="{{ $patient->uid }}">
                                <input type="hidden" name="label" value="{{ $label }}">
                                <label class="text-secondary text-sm mb-1">{{ $label }}</label>
                                <div class="mb-2">
                                    <input required autofocus type="text" class="form-control form-control-sm" name="value" value="{{ $vitalValues[$label]['value'] }}" placeholder="Value">
                                </div>
                                <label class="text-secondary text-sm mb-1">Effective Date</label>
                                <div class="mb-2">
                                    <input required type="date" class="form-control form-control-sm" name="effectiveDate" max="{{ date('Y-m-d') }}" value="{{ date('Y-m-d') }}">
                                </div>
                                <div class="d-flex align-items-center">
                                    <button class="btn btn-sm btn-primary mr-2" submit>Save</button>
                                    <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                </div>
                            </form>
                        </div>
                        <span class="content-html text-nowrap">
                            <span>{{ $label }}:</span>
                            <b>{{ $vitalValues[$label]['value'] }}</b>
                            <span class="font-weight-normal text-secondary ml-2 text-sm">(as on {{ friendly_date_time($vitalValues[$label]['effectiveDate'], false) }})</span>
                        </span>
                    </div>
                </td>
            </tr>
        @endforeach
        @if(!$patient->measurements || !count($patient->measurements) === 0)
            <tr>
                <td class="text-secondary p-0 border-0">
                    No items to show
                </td>
            </tr>
        @endif
        </tbody>
    </table>
    </div>
</div>
