<?php 

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."/mystery/init.php");
include_once(PATH_TO_ROOT."/mystery/_class/class.mysteryCalculator.php");

/*
echo "<pre>";
	var_dump($_SESSION['mysteryReportGraph']);
echo "</pre>";
*/



$project = $_GET['projectName'];
$idProject = $_GET['idProject'];

$calculator = new mysteryCalculator($db,$project);


if ($_SESSION[$_GET['uniqeID']]['GraphStatus']) {
	foreach ($_SESSION[$_GET['uniqeID']]['GraphStatus'] as $key=>$prop) {
			$calculator->hideGroups[$key] = $key;	
	}
}


$calculator->LoadQuestionnaireTree();
$calculator->LoadQuestionScores();


	
if ($_GET['id_record']) {
	$calculator->LoadRecords($_GET['id_record']);	
	
} elseif($_GET['records']) {
	
	$calculator->LoadRecords($_GET['records']);

} else {
	$sql = "SELECT id FROM ".DB_WARPIT_WEBCATI_BASE.".".$project."_structure WHERE flag_bad = 101";
	//echo $sql;
	$res = $db->SQLexecute($sql);
	while ($zad = $db->fetchAssoc($res)) {
		$id = $zad['id'];
		$calculator->LoadRecords($id);	
	}
}


foreach ($calculator->FolderStructure['main'] as $idMainFolder => $FolderLab) {

	if (!$calculator->calculatedScoresFolders[$idMainFolder]['total']) continue;
	$labels[] = "'".$FolderLab."'";
	$data1y[] = round(($calculator->calculatedScoresFolders[$idMainFolder]['pos'] / $calculator->calculatedScoresFolders[$idMainFolder]['total'] * 100),0);
	$data2y[] = round(($calculator->calculatedScoresFolders[$idMainFolder]['neg'] / $calculator->calculatedScoresFolders[$idMainFolder]['total'] * 100),0);
	
}

	$labels[] = "'Total'";
	$data1y[] = round($calculator->calculatedScoresTotal['pos'] / $calculator->calculatedScoresTotal['total'],2)*100;
	$data2y[] = round($calculator->calculatedScoresTotal['neg'] / $calculator->calculatedScoresTotal['total'],2)*100;
	


?>

<head>
  <title>Graphs</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-Equiv="Cache-Control" Content="no-cache">
  <meta http-Equiv="Pragma" Content="no-cache">
  <meta http-Equiv="Expires" Content="0">
   <style type="text/css">
  table
	{ 
	margin-left: auto;
	margin-right: auto;
	}
  </style>
   <script type="text/javascript" src="<?php echo PATH_TO_ROOT;?>_support/jquery.js"></script>
   <script type="text/javascript" src="<?php echo PATH_TO_ROOT;?>_plugins/charts/js/highcharts.js"></script>
   <script type="text/javascript" src="<?php echo PATH_TO_ROOT;?>_plugins/charts/js/modules/exporting.js"></script>
</head>
<body>
<?php 

 echo  "<script type=\"text/javascript\">
			$(function () {
				    var chart;
						   
				    $(document).ready(function() {
					     chart = new Highcharts.Chart({
						            chart: {
						                renderTo: 'container',
						                type: 'bar'
						            },
						            title: {
						                text: ''
						            },
						            exporting: {
						            
						            width: 2000,
						            filename: 'totalGraph-".$_GET['projectName']."'
						            },
						            xAxis: {
						                categories: [ ".implode(",",$labels)."],
						                labels: {
						                	style: {
						                		fontSize: '16px'
						                	}
						                }
						                			 
						            },
						            yAxis: {
						                min: 0,
						                max:100,
						                title: {
						                    text: '%'
						                },
						                stackLabels: {
						                    enabled: false,
						                    style: {
						                        fontWeight: 'bold',
						                        fontSize: '20px',
						                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
						                    }
						                }
						            },
						            tooltip: {
						                formatter: function() {
						                        return '<b>'+ this.series.name +'</b><br/>'+
						                        this.x +': '+ this.y +'%';
						                }
						            },
						            credits: {
						                enabled: false
						            },

						            legend: {
						          

						                reverse: true
						            },
						         
						            plotOptions: {
						                series: {
						                    stacking: 'normal',
						                     dataLabels: {
						                        enabled: true,
						                        align: 'center',
						                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
						                        formatter: function() {
						                        	return  this.y +'%';
						                		}
						                    }

						                }
						            },
						            
						            series: [{
							                name: 'Postignuće %',
							                color: '#465623',
							                data: [".implode(",",$data1y)."]
							            }, {
							                name: 'Nedostatak %',
							                color: '#7D2725',
							                data: [".implode(",",$data2y)."]
							            }]
						            ";
   									
   				echo "					
						        });
		              });						       
			});
</script>
<div id=\"container\" style=\"min-width: 400px; max-width:95%; height: 600px; margin: 0 auto\"></div>";

?>
</body>
</html>