@extends ('layouts.patient')

@section('inner-content')

    <h4 class="font-weight-bold m-0 font-size-16" id="vitals-settings">Vitals Settings</h4>
    <hr class="m-neg-4">

    <div class="row" id="vitals-settings-{{$patient->uid}}">
        <div class="col-6 border-right">
            <div class="d-flex align-items-baseline">
                <b>BMI/Weight Management</b>
                <span class="mx-2 text-secondary">|</span>
                @include('app.patient.vitals-settings.bmi-management-form')
            </div>
            <div class="pt-3">
                @include('app.patient.vitals-settings.bmi-management-summary')
            </div>
        </div>
        <div class="col-6">
            <div class="d-flex align-items-baseline">
                <b>BP Management</b>
                <span class="mx-2 text-secondary">|</span>
                @include('app.patient.vitals-settings.bp-management-form')
            </div>
            <div class="pt-3">
                @include('app.patient.vitals-settings.bp-management-summary')
            </div>
        </div>
    </div>

    <script>
        (function () {
            function calculateUsualBmi() {
                let h = $('#bmi-management-settings [name="currentHeightInInches"]').val(),
                    wMin = $('#bmi-management-settings [name="usualWeightInPoundsMin"]').val(),
                    wMax = $('#bmi-management-settings [name="usualWeightInPoundsMax"]').val();
                let bmiMinElem = $('#bmi-management-settings [name="usualBmiMin"]').val(''),
                    bmiMinCategoryElem = $('#bmi-management-settings [name="usualBmiMinCategory"]').val(''),
                    bmiMaxElem = $('#bmi-management-settings [name="usualBmiMax"]').val(''),
                    bmiMaxCategoryElem = $('#bmi-management-settings [name="usualBmiMaxCategory"]').val('');
                calculateBmi(h, wMin, bmiMinElem, bmiMinCategoryElem);
                calculateBmi(h, wMax, bmiMaxElem, bmiMaxCategoryElem);
                return false;
            }

            function calculateIdealBmi() {
                let h = $('#bmi-management-settings [name="currentHeightInInches"]').val(),
                    w = $('#bmi-management-settings [name="idealWeightInPounds"]').val();
                let bmiElem = $('#bmi-management-settings [name="idealBmi"]').val(''),
                    bmiCategoryElem = $('#bmi-management-settings [name="idealBmiCategory"]').val('');
                calculateBmi(h, w, bmiElem, bmiCategoryElem);
                return false;
            }

            function calculateBmi(h, w, bmiElem, bmiCategoryElem) {
                let bmi = 0,
                    bmiCategory = '';
                try {
                    h = +h;
                    w = +w;
                    bmi = (w / (h * h)) * 703;
                    bmiElem.val(bmi.toFixed(1));
                    if (bmi < 18.5) {
                        bmiCategory = '(Underweight)';
                    }
                    if (bmi >= 18.5 && bmi < 25) {
                        bmiCategory = '';
                    }
                    if (bmi >= 25 && bmi < 30) {
                        bmiCategory = '(Overweight)';
                    }
                    if (bmi >= 30) {
                        bmiCategory = '(Obese)';
                    }
                    bmiCategoryElem.show();
                    if (bmiCategory == '') {
                      bmiCategoryElem.hide();
                    }
                    bmiCategoryElem.val(bmiCategory);
                } catch (e) {
                    return false;
                }
            }

            function init() {
                $(document)
                    .off('change input paste keyup',
                        '#bmi-management-settings [name="currentHeightInInches"], #bmi-management-settings [name="usualWeightInPoundsMin"],  #bmi-management-settings [name="usualWeightInPoundsMax"]')
                    .on('change input paste keyup',
                        '#bmi-management-settings [name="currentHeightInInches"], #bmi-management-settings [name="usualWeightInPoundsMin"],  #bmi-management-settings [name="usualWeightInPoundsMax"]', function () {
                            return calculateUsualBmi();
                        });
                $(document)
                    .off('change input paste keyup',
                        '#bmi-management-settings [name="currentHeightInInches"], #bmi-management-settings [name="idealWeightInPounds"]')
                    .on('change input paste keyup',
                        '#bmi-management-settings [name="currentHeightInInches"], #bmi-management-settings [name="idealWeightInPounds"]', function () {
                            return calculateIdealBmi();
                        });

                $('#bmi-management-settings [name="currentHeightInInches"]').trigger('change');
            }

            addMCInitializer('vitals-settings-{{$patient->uid}}', init, '#vitals-settings-{{$patient->uid}}')
        }).call(window);
    </script>

@endsection
