<?php
include '../config.php';
$data = array();
$Q28a = array();
$Q28b = array();
$Q28e = array();

$Q28aV = array();
$Q28bV = array();
$Q28eV = array();
$delivery_attribute = array();
$delivery_attributeV = 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,5,7,8,9";
$average="8,9";
$Outstanding="10,11";

	/*Base script start*/
	
	$sql_base=$base;
	
	$data['base'] = $sql_base;
	/*Base script End*/
	
	
// 	$q27_base=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q27 in (1,2,3,4,5,6,7,8) ");
// 	$Q27_base=mysqli_fetch_array($q27_base); 
// 	$data['Q27_base'] = $Q27_base[0];
	
	if($sql_base==0) 
	{
		array_push($Q28a,0,0,0,0);
		array_push($Q28b,0,0,0);
		array_push($Q28e,0,0,0);
		array_push($delivery_attribute,0,0,0,0,0);
		$data['Q27'] = 0;
		$data['Q28a'] = $Q28a;
		$data['Q28b'] = $Q28b;
		$data['Q28d'] = 0;
		$data['Q28e'] = $Q28e;
		$data['delivery_attr'] = $delivery_attribute;
	}
	else
	{
	    $columnS=array("record","Q9g","Q27",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q27=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q27 in (1,2,3,4,5,6,7,8) ");
		$Q27=mysqli_fetch_array($q27); 
		$data['Q27'] = round(($Q27[0]/$sql_base )*100,1);
		$data['Q27V'] = $Q27[0];

		/*Q28a script start*/
		$columnS=array("record","Q9g","Q28a",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q28a_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28a =1 ");
		//echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28a =1 ";
		$Q28a_1=mysqli_fetch_array($q28a_1); 
		//echo $Q28a_1;
		$Q28A_1 = round(($Q28a_1[0] / $sql_base ) * 100,1);

		$q28a_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28a =2 ");
		$Q28a_2=mysqli_fetch_array($q28a_2); 
		$Q28A_2 = round(($Q28a_2[0] / $sql_base ) * 100,1);

		$q28a_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28a =3 ");
		$Q28a_3=mysqli_fetch_array($q28a_3); 
		$Q28A_3 = round(($Q28a_3[0] / $sql_base ) * 100,1);

		$q28a_4=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28a =4 ");
		$Q28a_4=mysqli_fetch_array($q28a_4); 
		$Q28A_4 = round(($Q28a_4[0] / $sql_base ) * 100,1);

		array_push($Q28a,$Q28A_1,$Q28A_2,$Q28A_3,$Q28A_4);
		array_push($Q28aV,$Q28a_1[0],$Q28a_2[0],$Q28a_3[0],$Q28a_4[0]);

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

		$q28b_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28b in (2) ");
		$Q28b_2=mysqli_fetch_array($q28b_2); 
		$Q28B_2 = round(($Q28b_2[0] / $sql_base ) * 100,1);

		$q28b_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28b in (3) ");
		$Q28b_3=mysqli_fetch_array($q28b_3); 
		$Q28B_3 = round(($Q28b_3[0] / $sql_base ) * 100,1);

		array_push($Q28b,$Q28B_1,$Q28B_2,$Q28B_3);
		array_push($Q28bV,$Q28b_1[0],$Q28b_2[0],$Q28b_3[0]);

		$data['Q28b'] = $Q28b;
		$data['Q28bV'] = $Q28bV;
		/*Q28b script end*/
		
		
		/*Q28d script start*/	
		$columnS=array("record","Q9g","Q28d",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q28d=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28d in (2) ");
		$Q28d=mysqli_fetch_array($q28d); 
		$data['Q28d'] = round(($Q28d[0] / $sql_base ) * 100,1);
		$data['Q28dV'] = $Q28d[0];
		/*Q28d script end*/
		
		
		/*Q28e script start*/	
		$columnS=array("record","Q9g","Q28e",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q28e_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28e in (1) ");
		$Q28e_1=mysqli_fetch_array($q28e_1); 
		$Q28E_1 = round(($Q28e_1[0] / $sql_base ) * 100,1);

		$q28e_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28e in (2) ");
		$Q28e_2=mysqli_fetch_array($q28e_2); 
		$Q28E_2 = round(($Q28e_2[0] / $sql_base ) * 100,1);

		$q28e_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q28e in (3) ");
		$Q28e_3=mysqli_fetch_array($q28e_3); 
		$Q28E_3 = round(($Q28e_3[0] / $sql_base ) * 100,1);

		array_push($Q28e,$Q28E_1,$Q28E_2,$Q28E_3);
		array_push($Q28eV,$Q28e_1[0],$Q28e_2[0],$Q28e_3[0]);

		$data['Q28e'] = $Q28e;
		$data['Q28eV'] = $Q28eV;
		/*Q28e script end*/
			
		
		/*Q29 script start*/
		$columnS=array("record","Q9g","Q29",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q29=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q29 in (1) ");
		$Q29=mysqli_fetch_array($q29); 
		$Q29X = round(($Q29[0] / $sql_base ) * 100,1);
		/*Q29 script end*/
			
		
		/*Q30 script start*/	
		$columnS=array("record","Q9g","Q30",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q30=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q30 in (1) ");
		$Q30=mysqli_fetch_array($q30); 
		$Q30X = round(($Q30[0] / $sql_base ) * 100,1);
		/*Q30 script end*/
			
		
		/*Q31 script start*/	
		$columnS=array("record","Q9g","Q31",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q31=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q31 in (1) ");
		$Q31=mysqli_fetch_array($q31); 
		$Q31X = round(($Q31[0] / $sql_base ) * 100,1);
		/*Q31 script end*/
		
		
		/*Q33 script start*/
		$columnS=array("record","Q9g","Q33",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q33=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q33 in (1) ");
		$Q33=mysqli_fetch_array($q33); 
		$Q33X = round(($Q33[0] / $sql_base ) * 100,1);
		/*Q33 script end*/
		
		/*Q34 script start*/	
		$columnS=array("record","Q9g","Q34",$tablecondX);
        $mq=query_marge($time,$columnS);
		$q34=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q34 in (1) ");
		$Q34=mysqli_fetch_array($q34); 
		$Q34X = round(($Q34[0] / $sql_base ) * 100,1);
		/*Q34 script end*/

		array_push($delivery_attribute,$Q29X,$Q30X,$Q31X,$Q33X,$Q34X);
		array_push($delivery_attributeV,$Q29[0],$Q30[0],$Q31[0],$Q33[0],$Q34[0]);

		$data['delivery_attr'] = $delivery_attribute;
		$data['delivery_attrV'] = $delivery_attributeV;


	}
	
	
	
	echo json_encode($data);
?>