<?php

$vitalMap = [
    'heightInches' => '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 = [];

foreach($vitalLabels as $l){
    $vitalValues[$l] = [
        'value' => null,
        'effectiveDate' => null
    ];
}

foreach($patient->measurements as $measurement) {
    if(!empty($measurement->label) && in_array($measurement->label, $vitalLabels)){
        $vitalValues[$measurement->label] = [
            'value'=>$measurement->value,
            'effectiveDate' => $measurement->effective_date
        ];
    }
}

?>

<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 }}">
                <div class="mb-2">
                    Ht. (in.)<br>
                    <input type="text" class="form-control form-control-sm" name="heightInches" value="{{$vitalValues['Ht. (in.)']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    Wt. (lbs.)<br>
                    <input type="text" class="form-control form-control-sm" name="weightPounds" value="{{$vitalValues['Wt. (lbs.)']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    <label for="" class="control-label mb-0">Temp. (F)</label>
                    Temp. (F)<br/>
                    <input type="text" class="form-control form-control-sm" name="temperatureF" value="{{$vitalValues['Temp. (F)']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    Pulse<br/>
                    <input type="text" class="form-control form-control-sm" name="pulseRatePerMinute" value="{{$vitalValues['Pulse']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    Resp.<br/>
                    <input type="text" class="form-control form-control-sm" name="respirationRatePerMinute" value="{{$vitalValues['Resp.']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    Pulse Ox.<br/>
                    <input type="text" class="form-control form-control-sm" name="pulseOx" value="{{$vitalValues['Pulse Ox.']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    SBP<br/>
                    <input type="text" class="form-control form-control-sm" name="systolicBP" value="{{$vitalValues['SBP']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    DBP<br/>
                    <input type="text" class="form-control form-control-sm" name="diastolicBP" value="{{$vitalValues['DBP']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    Smoking Status<br/>
                    <input type="text" class="form-control form-control-sm" name="smokingStatus" value="{{$vitalValues['Smoking Status']['value']}}" placeholder="">
                </div>
                <div class="mb-2">
                    BMI<br/>
                    <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>
    <table class="table table-sm border-0 my-0 mx-2">
        <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 }}">
                                <div class="mb-2">
                                    <input type="text" class="form-control form-control-sm" name="label" value="{{ $label }}" placeholder="Type">
                                </div>
                                <div class="mb-2">
                                    <input autofocus type="text" class="form-control form-control-sm" name="value" value="{{ $vitalValues[$label]['value'] }}" placeholder="Value">
                                </div>
                                <div class="mb-2">
                                    <input 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">
                            <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>
