<?php
include '../config.php';
$data = array();
$sql_base;


//$circle = $_POST['circle'];
$condX = $_POST['condX'];
$conX = $_POST['conX'];
$time = $_POST['timeX'];
$tablecondX = $_POST['tablecondX'];
$timeZ = "('".implode("','",$time)."')";
$columnS=array("record","Q9g",$tablecondX);
$mq=query_marge($time,$columnS);

if($conX==1){    
	
   $condX="";   
	$q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ");
	//echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ";
	$baseX=mysqli_fetch_array($q);   
	$base = $baseX[0];
}

if($conX==2){
   $q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX 1=1");
   $baseX=mysqli_fetch_array($q);   
   $base = $baseX[0];
}

$poor="1,2,3,4";
$average="5,6";
$Outstanding="7,8,9,10";

	/*Base script start*/
	
	$sql_base=$base;
	$data['base'] = $sql_base;
	/*Base script End*/

	if($sql_base==0) 
	{
		$data['Q17'] = 0;
		$data['Q18a'] = 0;
		$data['Q18c'] = 0;
		$data['Q18d'] = 0;
		
	}
	else
	{
	    $columnS=array("record","Q9g","Q17","Q18a","Q18c","Q18d",$tablecondX);
        $mq=query_marge($time,$columnS);
		/*Q17 script start*/
		$q17=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q17 IN (1,2,3,4,5,6,7,8)");
		//echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q11 = '2' ";
		$Q17=mysqli_fetch_array($q17); 
		$data['Q17'] = round(($Q17[0] / $sql_base ) * 100,1);
		$data['Q17V'] = $Q17[0];
		/*Q17 script end*/

		/*Q18a script start*/
		$Baseq18a = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q18a != '' ");
		$baseq18a=mysqli_fetch_array($Baseq18a);

		$q18a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q18a = '2' ");
		//echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q11 = '2' ";
		$Q18a=mysqli_fetch_array($q18a); 
		$data['Q18a'] = round(($Q18a[0] / $sql_base ) * 100,1);
		$data['Q18aV'] = $Q18a[0];
		/*Q18a script end*/


		/*Q18c script start*/
		$Baseq18c = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q18c != '' ");
		$baseq18c=mysqli_fetch_array($Baseq18c);
		$q18c=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q18c = '2' ");
		$Q18c=mysqli_fetch_array($q18c); 
		
		$data['Q18c'] = round(($Q18c[0] / $sql_base ) * 100,1);
		$data['Q18cV'] = $Q18c[0];
		/*Q18c script end*/

		/*Q18d script start*/
		$Baseq18d = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q18d != '' ");
		$baseq18d=mysqli_fetch_array($Baseq18d);
		$q18d=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q18d = '2' ");
		$Q18d=mysqli_fetch_array($q18d); 
		$data['Q18d'] = round(($Q18d[0] / $sql_base ) * 100,1);
		$data['Q18dV'] = $Q18d[0];
		/*Q18d script end*/
	}
	
	
	
	
	echo json_encode($data);
?>