<?php

$bmi = array();
$height = array();
$weight = array();
$medical_date = array();


$bmi_new = array();
$height_new = array();
$weight_new = array();


$query = "SELECT height, date(appointment_date) as dat FROM employee_appointment 
where emp_id='$id'and height != ''
UNION
SELECT cf.checkup_form_value as height ,date(c.checkup_date) as dat from 
checkup_form c left join checkup_form_key_value cf on 
c.checkup_id=cf.checkup_form_id left join checkup_parameter cp on 
cp.`column_name` = cf.checkup_form_key 
where c.emp_id='$id' and cp.key_health_map_name=(select key_param_id from key_health_reportable_parameter_master where key_param_name='height') order by dat desc limit 25";

error_log("weight query ".$query);
$result = @mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    // $count = 0;
  while ($row = @mysqli_fetch_array($result)) {
    $height['height'][$row[1]] = $row[0];
    // $count++;
    array_push($medical_date, $row[1]);
    // array_push($height, $row[0]);
  }
}
error_log("modified array ".print_r($height, true));


$query = "SELECT `weight`, date(appointment_date) as dat FROM employee_appointment 
where emp_id='$id' and `weight` != ''
UNION
SELECT cf.checkup_form_value as weight ,date(c.checkup_date) as dat from 
checkup_form c left join checkup_form_key_value cf on 
c.checkup_id=cf.checkup_form_id left join checkup_parameter cp on 
cp.`column_name` = cf.checkup_form_key 
where c.emp_id='$id' and cp.key_health_map_name=(select key_param_id from key_health_reportable_parameter_master where key_param_name='weight') order by dat desc limit 25";

error_log("weight query ".$query);
$result = @mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    // $count  = 0;
  while ($row = @mysqli_fetch_array($result)) {
    // $count++;
    $weight['weight'][$row[1]] = $row[0];
    array_push($medical_date, $row[1]);
    // array_push($weight, $row[0]);
  }
}
error_log("modified array ".print_r($weight, true));

$query = "SELECT bmi, date(appointment_date) as dat FROM employee_appointment 
where emp_id='$id' and bmi != ''
UNION
SELECT cf.checkup_form_value as bmi ,date(c.checkup_date) as dat from 
checkup_form c left join checkup_form_key_value cf on 
c.checkup_id=cf.checkup_form_id left join checkup_parameter cp on 
cp.`column_name` = cf.checkup_form_key 
where c.emp_id='$id' and cp.key_health_map_name=(select key_param_id from key_health_reportable_parameter_master where key_param_name='bmi') order by dat desc limit 25";

error_log("weight query ".$query);
$result = @mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    // $count  = 0;
  while ($row = @mysqli_fetch_array($result)) {
    // $count++;
    $bmi['bmi'][$row[1]] = $row[0];
    array_push($medical_date, $row[1]);
    // array_push($bmi, $row[0]);
  }
}
error_log("modified array ".print_r($bmi, true));

// error_log("heigth ".print_r($height, true));
// error_log("weight ".print_r($weight, true));
// error_log("bmi ".print_r($bmi, true));
// error_log("date ".print_r($medical_date, true));

$medical_date = array_values(array_unique($medical_date));

error_log("date unique ".print_r($medical_date, true));

for($i = 0; $i < sizeof($medical_date); $i++){

    error_log("inside loop value".$bmi['bmi'][$medical_date[$i]]);
    error_log("inside loop value".$height['height'][$medical_date[$i]]);
    error_log("inside loop value".$weight['weight'][$medical_date[$i]]);

    if($bmi['bmi'][$medical_date[$i]] == ''){
        array_push($bmi_new,0);
    }else{
        array_push($bmi_new,$bmi['bmi'][$medical_date[$i]]);
    }
    if($height['height'][$medical_date[$i]] == ''){
        array_push($height_new,0);
    }else{
        array_push($height_new,$height['height'][$medical_date[$i]]);
    }
    if($weight['weight'][$medical_date[$i]] == ''){
        array_push($weight_new,0);
    }else{
        array_push($weight_new,$weight['weight'][$medical_date[$i]]);
    }
}

error_log("final height ".print_r($height_new, true));
error_log("final weight ".print_r($weight_new, true));
error_log("final bmi ".print_r($bmi_new, true));

?>


<script>
var ctx = document.getElementById('patient_bmi').getContext('2d');
var bmi = [<?php echo join(',', $bmi_new); ?>];
var height = [<?php echo join(',', $height_new); ?>];
var weight = [<?php echo join(',', $weight_new); ?>];
var xaxis = [<?php echo json_encode($medical_date); ?>];

function formatDateBMI(input) {
    var datePart = input.match(/\d+/g),
        year = datePart[0],
        month = datePart[1],
        day = datePart[2];
    return day + '-' + month + '-' + year;
}
for (let i = 0; i < xaxis[0].length; i++) {
    xaxis[0][i] = formatDateBMI(xaxis[0][i]);
}

var myChart = new Chart(ctx, {
    type: 'line',
    options: {
        title: {
            display: true,
            text: "BMI Graph"
        },
        legend: {
            position: "bottom"
        },
        scales: {
            yAxes: [{
                ticks: {
                    precision: 0
                }
            }]
        }
    },
    data: {
        labels: xaxis[0],
        datasets: [{
            data: bmi,
            label: "BMI",
            borderColor: "#7bb6dd",
            backgroundColor: "#7bb6dd",
            fill: false,
            pointBackgroundColor: function(context) {
                var index = context.dataIndex;
                var value = context.dataset.data[index];
                return value < 85 ? 'red' : // draw negative values in red
                    index % 2 ? 'blue' :
                    // else, alternate values in blue and green
                    'green';
            }
        }, {
            data: height,
            label: "Height",
            borderColor: "#d45087",
            backgroundColor: "#d45087",
            fill: false,
            pointBackgroundColor: function(context) {
                var index = context.dataIndex;
                var value = context.dataset.data[index];
                return value < 124 ? 'red' : // draw negative values in red
                    index % 2 ? 'blue' :
                    // else, alternate values in blue and green
                    'green';
            }
        }, {
            data: weight,
            label: "Weight",
            borderColor: "#ff7c43",
            backgroundColor: "#ffa500",
            fill: false,
            pointBackgroundColor: function(context) {
                var index = context.dataIndex;
                var value = context.dataset.data[index];
                return value < 124 ? 'red' : // draw negative values in red
                    index % 2 ? 'blue' :
                    // else, alternate values in blue and green
                    'green';
            }

        }]
    },
});
</script>