<?php

require_once 'phplot.php';
include_once("config.php");

if (isset($_SESSION["user_id"]) && isset($_SESSION["pin"])) { //LOG IN!
	$id = urlencode(stripslashes($_SESSION["user_id"]));
	$pin = urlencode(stripslashes($_SESSION["pin"]));

	$params = "id=".$id;
	$params .= "&pin=" . $pin;

	$plot = new PHPlot(480, 360);

	if ($_GET['mode'] == 'cholesterol') {
		$results = new xml("amjayviewchol", $params, true);
		if (isset($results->error)) {
			$_SESSION['Err'] = $results->error[0]->row[0]->row[0]->error;
		} elseif (is_array($results->cholesterol[0]->patients[0]->row)) {			
			foreach($results->cholesterol[0]->patientcholesterol[0]->row as $patientcholesterol) { 
				foreach($patientcholesterol as $var => $val) {
					if ($var == 'id') $data_id = $val;
					if ($var == 'ldl') $data_ldl = $val;
					if ($var == 'hdl') $data_hdl = $val;
					if ($var == 'triglycerides') $data_triglycerides = $val;					
					if ($var == 'readingdate') $data_readingdate = $val;
				}		
				//$data[] = array('', $data_id, $data_ldl, $data_hdl, $data_triglycerides,);				
				list($m,$d,$y) = explode("/", $data_readingdate);
				$idx = mktime(0, 0, 0, $m, $d, $y);
				//$idx = floor(strtotime($data_readingdate)/3600/24);
				//$data[$idx] = array(substr($data_readingdate, 0, -5), $idx, $data_ldl, $data_hdl, $data_triglycerides);				
				$data[$idx] = array($idx, $data_ldl, $data_hdl, $data_triglycerides);				
			}			
			$plot->SetLegend(array('ldl', 'hdl', 'Triglyceride'));
			$plot->SetTitle('LDL HDL and Triglyceride trend');	
		} else {
			$template = new template();
			$template->go404();
		}
	} elseif ($_GET['mode'] == 'bloodpressure') {
		$results = new xml("amjayviewbp", $params, true);
		if (isset($results->error)) {
			$_SESSION['Err'] = $results->error[0]->row[0]->row[0]->error;
		} elseif (is_array($results->bloodpressure[0]->patients[0]->row)) {		
			foreach($results->bloodpressure[0]->patientbloodpressure[0]->row as $patientbloodpressure) { 
				foreach($patientbloodpressure as $var => $val) {
					if ($var == 'id') $data_id = $val;
					if ($var == 'systolic') $data_systolic = $val;
					if ($var == 'diastolic') $data_diastolic = $val;
					if ($var == 'pulse') $data_pulse = $val;
					if ($var == 'readingdate') $data_readingdate = $val;					
				}
				//$data[] = array('', $data_id, $data_systolic, $data_diastolic, $data_pulse);
				list($m,$d,$y) = explode("/", $data_readingdate);
				$idx = mktime(0, 0, 0, $m, $d, $y) . "$data_id";
				//$idx = floor(strtotime($data_readingdate)/3600/24);
				//$data[$idx] = array(substr($data_readingdate, 0, -5), $idx, $data_systolic, $data_diastolic, $data_pulse);
				$data[$idx] = array($idx, $data_systolic, $data_diastolic, $data_pulse);
			}
			$plot->SetLegend(array('Systolic', 'Diastolic', 'Pulse'));
			$plot->SetTitle('Systolic, Diastolic and Pulse trend');	
		} else {
			$template = new template();
			$template->go404();		
		}
	} elseif ($_GET['mode'] == 'bloodsugar') {
		$results = new xml("amjayviewbs", $params, true);
		if (isset($results->error)) {
			$_SESSION['Err'] = $results->error[0]->row[0]->row[0]->error;
		} elseif (is_array($results->bloodsugar[0]->patientbloodsugar[0]->row)) {		
			foreach($results->bloodsugar[0]->patientbloodsugar[0]->row as $patientbloodsugar) { 
				foreach($patientbloodsugar as $var => $val) {
					if ($var == 'id') $data_id = $val;
					if ($var == 'sugarlevel') $data_sugarlevel = $val;
					if ($var == 'unitofmeasure') $data_unitofmeasure = $val;
					if ($var == 'readingdate') $data_readingdate = $val;
				}
				if ($data_unitofmeasure == 'mg/dL') $data_sugarlevel = $data_sugarlevel * 0.055;
				//$data[] = array('', $data_id, $data_sugarlevel);
				list($m,$d,$y) = explode("/", $data_readingdate);
				$idx = mktime(0, 0, 0, $m, $d, $y);
				//$idx = floor(strtotime($data_readingdate)/3600/24);
				//$data[$idx] = array(substr($data_readingdate, 0, -5), $idx, $data_sugarlevel);
				$data[$idx] = array($idx, $data_sugarlevel);
			}				
			$plot->SetLegend(array('Sugar Level'));
			$plot->SetTitle('Blood Sugar trend');	
			$plot->SetYTitle('mmol/L');
		} else {
			$template = new template();
			$template->go404();		
		}
	} else {
			$template = new template();
			$template->go404();		
	}
	
	sort($data);
	foreach ($data as $val) {		
		//$graph[] = array(date("n/d", $val[0]), $val[0], $val[1], $val[2], $val[3]);
		$graph[] = array('', $val[0], $val[1], $val[2], $val[3]);
	}	
	$graph[0] = array(date("n/d", $graph[0][1]), $graph[0][1], $graph[0][2], $graph[0][3], $graph[0][4]);
	$cnt = count($graph)-1;
	$graph[$cnt] = array(date("n/d", $graph[$cnt][1]), $graph[$cnt][1], $graph[$cnt][2], $graph[$cnt][3], $graph[$cnt][4]);
	$cnt = floor($cnt/2);
	$graph[$cnt] = array(date("n/d", $graph[$cnt][1]), $graph[$cnt][1], $graph[$cnt][2], $graph[$cnt][3], $graph[$cnt][4]);

	$plot->SetImageBorderType('plain');

	$plot->SetDataColors(array('blue', 'red', 'black'));
	$plot->SetLineStyles(array('solid', 'solid', 'solid'));
	$plot->SetLegendStyle('left', 'left');	

	$plot->SetXTickLabelPos('none');

	$plot->SetPlotType('linepoints');
	$plot->SetDataType('data-data');
	$plot->SetDataValues($graph);

	$plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);

	//print_r($graph);
	if (count($graph)>1) $plot->DrawGraph();	
} else {
	$template = new template();
	$template->go404();		
}