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


//$circle = $_POST['circle'];
$condX = $_POST['condX'];
$conX = $_POST['conX'];
$Q23 = array();
$Q23V = array();
$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,7,8";
$Outstanding="9,10";

	/*Base script start*/
	
	$sql_base=$base;
	$data['base'] = $sql_base;
	/*Base script End*/
	
	if($sql_base==0) 
	{
		$data['Q20'] = 0;
		$data['Q21'] = 0;
		$data['Q22p'] = 0;
		$data['Q22O'] = 0;
		$data['Q23'] = 0;
	}
	else
	{
        $columnS=array("record","Q9g","Q20",$tablecondX);
        $mq=query_marge($time,$columnS);
        /*Q20 script start*/
        $q20=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q20 = '1' ");
        //echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q20 = '1' ";
        $Q20=mysqli_fetch_array($q20); 
        $data['Q20'] = round(($Q20[0] / $sql_base ) * 100,1);
        $data['Q20V'] = $Q20[0];
        /*Q20 script end*/

        /*Q21 script start*/
        $columnS=array("record","Q9g","Q21",$tablecondX);
        $mq=query_marge($time,$columnS);
        $q21=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q21 = '1' ");
        $Q21=mysqli_fetch_array($q21); 
        $data['Q21'] = round(($Q21[0] / $sql_base ) * 100,1);
        $data['Q21V'] = $Q21[0];
        /*Q21 script end*/

        /*Q22 script start*/
        $columnS=array("record","Q9g","Q22",$tablecondX);
        $mq=query_marge($time,$columnS);
        $q22p=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q22 in ($poor,$average) ");
        $Q22p=mysqli_fetch_array($q22p); 
        $data['Q22pAN'] = $Q22p[0];
        $data['Q22p'] = round(($Q22p[0] / $sql_base ) * 100,1); 
        
        //  $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,1);
        
        $q22O=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q22 in ($Outstanding) ");
        $Q22O=mysqli_fetch_array($q22O); 
        $data['Q22OAN'] = $Q22O[0];
        $data['Q22O'] = round(($Q22O[0] / $sql_base ) * 100,1); 
        /*Q22 script end*/






        /*Q23 script start*/
        $columnS=array("record","Q9g","Q23",$tablecondX);
        $mq=query_marge($time,$columnS);
        $q23a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q23 = '1' ");
        $Q23a=mysqli_fetch_array($q23a); 
        $Q23A = round(($Q23a[0] / $sql_base ) * 100,1);
        
        $q23b=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q23 = '2' ");
        $Q23b=mysqli_fetch_array($q23b); 
        $Q23B = round(($Q23b[0] / $sql_base ) * 100,1);

        $q23c=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q23 = '3' ");
        $Q23c=mysqli_fetch_array($q23c); 
        $Q23C = round(($Q23c[0] / $sql_base ) * 100,1);

        array_push($Q23,$Q23A,$Q23B,$Q23C);
        array_push($Q23V,$Q23a[0],$Q23b[0],$Q23c[0]);

        $data['Q23'] = $Q23;
        $data['Q23V'] = $Q23V;
        
        /*Q23 script end*/

    }
	
	
	
	
		



	
	
	echo json_encode($data);
?>