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


//$circle = $_POST['circle'];
$condX = $_POST['condX'];
$conX = $_POST['conX'];
$time = $_POST['timeX'];

$timeZ = "('".implode("','",$time)."')";
$tablecondX = $_POST['tablecondX'];
$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,7,8";
$Outstanding="9,10";

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

	if($sql_base==0) 
	{
		$data['Q11'] = 0;
		$data['Q15'] = 0;
		$data['Q15a'] = 0;
		$data['Q15c'] = 0;
		$data['Q12p'] = 0;
		$data['Q12O'] = 0;
		$data['Q13p'] = 0;
		$data['Q13O'] = 0;
		$data['Q14p'] = 0;
		$data['Q14O'] = 0;
		$data['Q15bp'] = 0;
		$data['Q15bO'] = 0;
		$data['Q15dp'] = 0;
		$data['Q15dO'] = 0;
	}
	else
	{
	    $columnS=array("record","Q9g","Q11","Q15","Q15a","Q15c","Q12","Q13","Q14","Q15b","Q15d",$tablecondX);
        $mq=query_marge($time,$columnS);
		/*Q11 script start*/
		$q11=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q11 = '1' ");
		//echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q11 = '2' ";
		$Q11=mysqli_fetch_array($q11); 
		$data['Q11'] = round(($Q11[0] / $sql_base ) * 100,1);
		$data['Q11V'] = $Q11[0];
		/*Q11 script end*/


		/*Q15 script start*/
		$q15=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15 = '1' ");
		$Q15=mysqli_fetch_array($q15); 
		$data['Q15'] = round(($Q15[0] / $sql_base ) * 100,1);
		$data['Q15V'] = $Q15[0];
		/*Q15 script end*/

		/*Q15a script start*/
		$q15apbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15a != '' ");
		$Q15apbaseX=mysqli_fetch_array($q15apbase);
		$Q15apbase = $Q15apbaseX[0];
		
		$q15a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15a = '1' ");
		$Q15a=mysqli_fetch_array($q15a); 

		if($Q15apbase==0)
		{
			$data['Q15a'] = 0 ;
		}
		else
		{
			$data['Q15a'] = round(($Q15a[0] / $Q15apbase ) * 100,1);
		}
		
		$data['Q15aV'] = $Q15a[0];
		/*Q15a script end*/

		/*Q15c script start*/
		$q15cpbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15c != '' ");
		$Q15cpbaseX=mysqli_fetch_array($q15cpbase);
		$Q15cpbase = $Q15cpbaseX[0];
		
		$q15c=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15c = '1' ");
		$Q15c=mysqli_fetch_array($q15c); 

		if($Q15cpbase==0)
		{
			$data['Q15c'] = 0 ;
		}
		else
		{
			$data['Q15c'] = round(($Q15c[0] / $Q15cpbase ) * 100,1);
		}

		$data['Q15cV'] = $Q15c[0];
		/*Q15c script end*/	

		/*Q12 script start*/
		$q12pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q12 != '' ");
		$Q12pbaseX=mysqli_fetch_array($q12pbase);
		$Q12pbase = $Q12pbaseX[0];
		$q12p=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q12 in ($poor,$average) ");
		$Q12p=mysqli_fetch_array($q12p); 

		//  $q12a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q12 in ($average) ");
		//  $Q12a=mysqli_fetch_array($q12a); 
		//  $data['Q12a'] = round(($Q12a[0] / $sql_base ) * 100);
		
		$q12O=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q12 in ($Outstanding) ");
		$Q12O=mysqli_fetch_array($q12O); 
		
		if($Q12pbase==0)
		{
			$data['Q12p'] = 0;
			$data['Q12O'] = 0;
		}
		else
		{
			$data['Q12p'] = round(($Q12p[0] / $Q12pbase ) * 100,1);
			$data['Q12O'] = round(($Q12O[0] / $Q12pbase ) * 100,1);
		}

		$data['Q12pAN'] =$Q12p[0];
		$data['Q12OAN'] =$Q12O[0];
		
		
		
		
		/*Q12 script end*/	


		/*Q13 script start*/
		$q13pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q13 != '' ");
		$Q13pbaseX=mysqli_fetch_array($q13pbase);
		$Q13pbase = $Q13pbaseX[0];
		$q13p=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q13 in ($poor,$average) ");
		$Q13p=mysqli_fetch_array($q13p); 
		

		$q13O=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q13 in ($Outstanding) ");
		$Q13O=mysqli_fetch_array($q13O); 
		

		if($Q13pbase==0)
		{
			$data['Q13p'] = 0;
			$data['Q13O'] = 0;
		}
		else
		{
			$data['Q13p'] = round(($Q13p[0] / $Q13pbase ) * 100,1);
			$data['Q13O'] = round(($Q13O[0] / $Q13pbase ) * 100,1); 
		}

		$data['Q13pAN'] =$Q13p[0];
		$data['Q13OAN'] =$Q13O[0];
		 


		/*Q13 script end*/
			

		/*Q14 script start*/
		$q14pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q14 != '' ");
		$Q14pbaseX=mysqli_fetch_array($q14pbase);
		$Q14pbase = $Q14pbaseX[0];
		$q14p=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q14 in ($poor,$average) ");
		$Q14p=mysqli_fetch_array($q14p); 
		 

		$q14O=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q14 in ($Outstanding) ");
		$Q14O=mysqli_fetch_array($q14O); 
		
		
		if($Q14pbase==0)
		{
			$data['Q14p'] = 0;
			$data['Q14O'] = 0;
		}
		else
		{
			$data['Q14p'] = round(($Q14p[0] / $Q14pbase ) * 100,1);
			$data['Q14O'] = round(($Q14O[0] / $Q14pbase ) * 100,1);
		}
		
		$data['Q14pAN'] =$Q14p[0];
		$data['Q14OAN'] =$Q14O[0];
		 
		
		/*Q14 script end*/

		/*Q15b script start*/
		
		$q15pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15b != '' ");
		$Q15pbaseX=mysqli_fetch_array($q15pbase);
        $Q15pbase = $Q15pbaseX[0];
		$q15bp=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15b in ($poor,$average) ");
		$Q15bp=mysqli_fetch_array($q15bp);
		 

		//  $q15ba=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15b in ($average) ");
		//  $Q15ba=mysqli_fetch_array($q15ba); 
		//  $data['Q15ba'] = round(($Q15ba[0] / $sql_base ) * 100);

		$q15bO=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15b in ($Outstanding) ");
		$Q15bO=mysqli_fetch_array($q15bO); 
		
		
		if($Q15pbase==0)
		{
			$data['Q15bp'] = 0;
			$data['Q15bO'] = 0;
		}
		else
		{
			$data['Q15bp'] = round(($Q15bp[0] / $Q15pbase ) * 100,1);
			$data['Q15bO'] = round(($Q15bO[0] / $Q15pbase ) * 100,1); 
		}
		
		$data['Q15bpAN'] =$Q15bp[0]; 
		$data['Q15bOAN'] =$Q15bO[0];
		
		
		/*Q15b script end*/

		/*Q15d script start*/
		$q15dpbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15d != '' ");
		$Q15dpbaseX=mysqli_fetch_array($q15dpbase);
		$Q15dpbase = $Q15dpbaseX[0];
		$q15dp=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15d in ($poor,$average) ");
		$Q15dp=mysqli_fetch_array($q15dp); 
		 

		//  $q15da=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15d in ($average) ");
		//  $Q15da=mysqli_fetch_array($q15da); 
		//  $data['Q15da'] = round(($Q15da[0] / $sql_base ) * 100);

		$q15dO=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q15d in ($Outstanding) ");
		$Q15dO=mysqli_fetch_array($q15dO); 
		
		
		if($Q15dpbase==0)
		{
			$data['Q15dp'] = 0;
			$data['Q15dO'] = 0;
		}
		else
		{
			$data['Q15dp'] = round(($Q15dp[0] / $Q15dpbase ) * 100,1);
			$data['Q15dO'] = round(($Q15dO[0] / $Q15dpbase ) * 100,1); 
		}

		$data['Q15dpAN'] =$Q15dp[0];
		$data['Q15dOAN'] =$Q15dO[0];
		
		
		
		
		/*Q15d script end*/
	}
	
	
	echo json_encode($data);
?>