<?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['Q19'] = 0;
		$data['Q19a1'] = 0;
		$data['Q19a2'] = 0;
		$data['Q19a3'] = 0;
		$data['Q19a'] = 0;
		$data['Q19b'] = 0;
		$data['Q19d'] = 0;
		$data['Q19e'] = 0;
		$data['Q19e3'] = 0;
		$data['Q19f'] = 0;
		$data['Q19e2'] = 0;
		$data['Q23a'] = 0;
		
		
	}
	else
	{
        /*Q19 script start*/
        $columnS=array("record","Q9g","Q19",$tablecondX);
        $mq=query_marge($time,$columnS);
        $q19=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q19 IN (1,2,3,4,5,6,7,8) ");
        $Q19=mysqli_fetch_array($q19); 
        $data['Q19'] = round(($Q19[0] / $sql_base ) * 100,1);
        $data['Q19V'] = $Q19[0];
        /*Q19 script end*/

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

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

        /*Q19a3 script start*/
        $columnS=array("record","Q9g","Q19a3",$tablecondX);
        $mq=query_marge($time,$columnS);
        $q19a3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q19a3 = '1' ");

        $Q19a3=mysqli_fetch_array($q19a3); 
        $data['Q19a3'] = round(($Q19a3[0] / $sql_base ) * 100,1);
        $data['Q19a3V'] = $Q19a3[0];
        /*Q19a3 script end*/

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

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

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

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

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

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

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

        /*Q23a script start*/
        $columnS=array("record","Q9g","Q23a",$tablecondX);
        $mq=query_marge($time,$columnS);
        $Baseq23a = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q23a!= '' ");
        $baseq23a=mysqli_fetch_array($Baseq23a);
        $q23a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q23a = '1' ");
        $Q23a=mysqli_fetch_array($q23a); 
        $data['Q23a'] = round(($Q23a[0] / $sql_base ) * 100,1);
        $data['Q23aV'] = $Q23a[0];
        /*Q23a script end*/
		
		
			/*Q19e2 script start*/
			$columnS=array("record","Q9g","Q19e2",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q19e2p=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q19e2 in (1,2,3,4,5,6,7,8) ");
		$Q19e2p=mysqli_fetch_array($q19e2p); 
		$data['Q19e2pAN'] =$Q19e2p[0];
		$data['Q19e2p'] = round(($Q19e2p[0] / $sql_base ) * 100,1); 

		$q19e2O=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q19e2 in (9,10) ");
		$Q19e2O=mysqli_fetch_array($q19e2O); 
		$data['Q19e2OAN'] =$Q19e2O[0];
		$data['Q19e2O'] = round(($Q19e2O[0] / $sql_base ) * 100,1); 
		/*Q19e2 script end*/

    }
	
	
	
	
	echo json_encode($data);
?>