<?php

	$opd=array();

	$xaxis = array();
	$opd2=array();

	$xaxis2 = array();
	$opd3=array();

	$xaxis3 = array();

	for($i=0;$i<15;$i++){
		$month=date('m')-$i;

		if($month>0){$query="SELECT COUNT(appointment_id) , date(appointment_date) FROM employee_appointment WHERE month(appointment_date) = '".$month."'  and year(appointment_date) = year(now())   AND appointment_type='O' ";
		$query2="SELECT COUNT(appointment_id) , date(appointment_date) FROM employee_appointment WHERE month(appointment_date) = '".$month."' and year(appointment_date) = year(now())  AND appointment_type='O' and ailment_systems_new='1'";
        $query3="SELECT COUNT(e.appointment_id) , date(e.appointment_date) FROM employee_appointment e left join patient_master p on e.emp_id=p.id  WHERE month(e.appointment_date) = '".$month."' and year(e.appointment_date) = year(now()) AND e.appointment_type='O' and p.designation_id='38'";}

   else {

		 $month= 12+$month;

	 	$query="SELECT COUNT(appointment_id) , date(now()- INTERVAL $i month) FROM employee_appointment WHERE month(appointment_date) = '".$month."' and year(appointment_date) = year(now())-1  AND appointment_type='O' ";
		$query2="SELECT COUNT(appointment_id) , date(now()- INTERVAL $i month) FROM employee_appointment WHERE month(appointment_date) = '".$month."' and year(appointment_date) = year(now())-1  AND appointment_type='O' and ailment_systems_new='1' ";
        $query3="SELECT COUNT(e.appointment_id) , date(now()- INTERVAL $i month) FROM employee_appointment e left join patient_master p on e.emp_id=p.id WHERE month(e.appointment_date) = '".$month."' and year(e.appointment_date) = year(now())-1  AND e.appointment_type='O' and p.designation_id='38' ";
	}


		 error_log('mmxx'.$query) ;
		if (!$result = @mysqli_query($conn,$query)) {
			die(mysqli_error($conn));
		}
		if(mysqli_num_rows($result) > 0) {
			while ($row = @mysqli_fetch_array($result)) {

			     $newDate = date("M-Y", strtotime($row[1]));
				array_push($xaxis,$newDate);
				array_push($opd,$row[0]);
			}
		} 

		if (!$result2 = @mysqli_query($conn,$query2)) {
			die(mysqli_error($conn));
		}
		if(mysqli_num_rows($result2) > 0) {
			while ($row2 = @mysqli_fetch_array($result2)) {
				$newDate = date("M-Y", strtotime($row2[1]));
error_log('mm'.$newDate) ;
			 array_push($xaxis2,$newDate);

				array_push($opd2,$row2[0]);
			}
		} 


        if (!$result3 = @mysqli_query($conn,$query3)) {
			die(mysqli_error($conn));
		}
		if(mysqli_num_rows($result3) > 0) {
			while ($row3 = @mysqli_fetch_array($result3)) {
				$newDate = date("M-Y", strtotime($row3[1]));
error_log('mm'.$newDate) ;
			 array_push($xaxis3,$newDate);

				array_push($opd3,$row3[0]);
			}
		} 
    }


?>


<script>
var ctx = document.getElementById('patient_month').getContext('2d');
var opd = [<?php  echo join(',',$opd); ?>];
var xaxis = [<?php  echo json_encode($xaxis); ?>];


for (let i = 0; i < xaxis.length; i++) {
    xaxis[0][i] = (xaxis[0][i]);
}

var myChart = new Chart(ctx, {
    type: 'line',
    options: {
        title: {
            display: true,
            text: "Month Wise No. of OPD Patients"
        },
        legend: {
            position: "bottom"
        },
        scales: {
            yAxes: [{
                ticks: {
                    precision: 0
                }
            }]
        }
    },
    data: {
        labels: xaxis[0],
        datasets: [{
            data: opd,
            label: "OPD",
            borderColor: "#d45087",
            backgroundColor: "#d45087",
            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';
            }

        }]
    },
});

var ctx2 = document.getElementById('patient_month2').getContext('2d');
var opd2 = [<?php  echo join(',',$opd2); ?>];

var xaxis2 = [<?php  echo json_encode($xaxis2); ?>];


for (let i = 0; i < xaxis2.length; i++) {
    xaxis2[0][i] = (xaxis2[0][i]);
}
var myChart = new Chart(ctx2, {
    type: 'line',
    options: {
        title: {
            display: true,
            text: "Month Wise No. of Patients"
        },
        legend: {
            position: "bottom"
        },
        scales: {
            yAxes: [{
                ticks: {
                    precision: 0
                }
            }]
        }
    },
    data: {
        labels: xaxis2[0],
        datasets: [{
            data: opd2,
            label: "OPD",
            borderColor: "#d45087",
            backgroundColor: "#d45087",
            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';
            }

        }]
    },
});

///desig chart\\\

var ctx3 = document.getElementById('patient_month3').getContext('2d');
var opd3 = [<?php  echo join(',',$opd3); ?>];

var xaxis3 = [<?php  echo json_encode($xaxis2); ?>];


for (let i = 0; i < xaxis3.length; i++) {
    xaxis3[0][i] = (xaxis3[0][i]);
}
var myChart3 = new Chart(ctx3, {
    type: 'line',
    options: {
        title: {
            display: true,
            text: "Month Wise No. of Patients"
        },
        legend: {
            position: "bottom"
        },
        scales: {
            yAxes: [{
                ticks: {
                    precision: 0
                }
            }]
        }
    },
    data: {
        labels: xaxis3[0],
        datasets: [{
            data: opd3,
            label: "OPD",
            borderColor: "#d45087",
            backgroundColor: "#d45087",
            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';
            }

        }]
    },
});
</script>