<?php
$vitalLabels = [
    "heightInInches" => "Ht. (in.)",
    "weightPounds" => "Wt. (lbs.)",
    "temperatureF" => "Temp. (F)",
    "systolicBP" => "SBP",
    "diastolicBP" => "DBP",
    "pulseRatePerMinute" => "Pulse",
    "respirationRatePerMinute" => "Resp.",
    "pulseOx" => "Pulse Ox.",
    "smokingStatus" => "Smoking Status",
    "bmi" => "BMI (kg/m²)",
];
$contentData = [
    "heightInInches" => [
        "label" => "Ht. (in.)",
        "value" => "",
        "date" => "",
    ],
    "weightPounds" => [
        "label" => "Wt. (lbs.)",
        "value" => "",
        "date" => "",
    ],
    "temperatureF" => [
        "label" => "Temp. (F)",
        "value" => "",
        "date" => "",
    ],
    "systolicBP" => [
        "label" => "SBP",
        "value" => "",
        "date" => "",
    ],
    "diastolicBP" => [
        "label" => "DBP",
        "value" => "",
        "date" => "",
    ],
    "pulseRatePerMinute" => [
        "label" => "Pulse",
        "value" => "",
        "date" => "",
    ],
    "respirationRatePerMinute" => [
        "label" => "Resp.",
        "value" => "",
        "date" => "",
    ],
    "pulseOx" => [
        "label" => "Pulse Ox.",
        "value" => "",
        "date" => "",
    ],
    "smokingStatus" => [
        "label" => "Smoking Status",
        "value" => "",
        "date" => "",
    ],
    "bmi" => [
        "label" => "BMI (kg/m²)",
        "value" => "",
        "date" => "",
    ],
];

if($patient->canvas_data) {
    $canvasData = json_decode($patient->canvas_data, true);
    if(isset($canvasData["vitals"])) {
        $contentData = $canvasData["vitals"];
        // ensure $contentData has all the expected vitals and correct labels!
        foreach ($vitalLabels as $k => $v) {
            if(!isset($contentData[$k])) {
                $contentData[$k] = [
                    "label" => $v,
                    "value" => "",
                    "date" => "",
                ];
            }
        }

    }
}

foreach ($vitalLabels as $k => $v) {
?>
    <div class="d-flex vital-item align-items-center">
        <span class="content-html text-nowrap">
            <span><?= $v ?>:</span>
            <?php
            $vital = [];
            if(isset($contentData[$k])) {
                $vital = $contentData[$k];
            }
            ?>
            <b><?= isset($vital["value"]) && !empty($vital["value"]) ? $vital["value"] : '-' ?></b>
            <?php if($k === 'bmi' && isset($vital["value"]) && !empty($vital["value"])):
                $bmi = floatval($vital["value"]);
                ?>
                <span class="ml-2 py-1 m-0 font-weight-bold">
                    <?php if($bmi < 18.5): ?>
                        <span class="text-sm text-warning-mellow">(Underweight)</span>
                    <?php endif; ?>
                    <?php if($bmi >= 18.5 && $bmi < 25): ?>
                        <span class="text-sm text-success">(Healthy Weight)</span>
                    <?php endif; ?>
                    <?php if($bmi >= 25 && $bmi < 30): ?>
                        <span class="text-sm text-warning-mellow">(Overweight)</span>
                    <?php endif; ?>
                    <?php if($bmi >= 30): ?>
                        <span class="text-sm text-warning-mellow">(Obese)</span>
                    <?php endif; ?>
                </span>
            <?php endif; ?>
            <?php if(!!$vital["date"]): ?>
                <span class="font-weight-normal text-secondary ml-2 text-sm">(as on <?= friendly_date_time($vital["date"], false) ?>)</span>
            <?php endif; ?>
        </span>
    </div>
<?php
}
?>
