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


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

	$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];


$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['ssi'] = 0;
		$data['Q4'] = 0;
		$data['Q4_1'] = 0;
		$data['Q4_2'] = 0;
		$data['Q5_1'] = 0;
		$data['Q5_2'] = 0;
		$data['Q5_3'] = 0;
		$data['Q5_NPS'] = 0;
		$data['Q8'] = 0;
		$data['Q8_1'] = 0;
		$data['Q8_2'] = 0;
		$data['Q9_1'] = 0;
		$data['Q9_2'] = 0;
		$data['Q9_3'] = 0;
		$data['Q9_NPS'] = 0;
	}
	else
	{
		/*SSI script start*/
		$columnS=array("record","Q9g","ssi_v");
        $mq=query_marge($time,$columnS);
		$ssi=mysqli_query($conn,"SELECT avg(`ssi_v`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ");
		//echo "SELECT avg(`ssi_v`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND ";
		$SSI=mysqli_fetch_array($ssi); 
		$data['ssi'] = round($SSI[0]);
		/*SSI script end*/


		/*Q4 script start*/
		$columnS=array("record","Q9g","Q4");
        $mq=query_marge($time,$columnS);
		$q4=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q4 in (9,10) ");
		$Q4=mysqli_fetch_array($q4); 
		$data['Q4'] = round(($Q4[0] / $sql_base ) * 100,1);
        $data['Q4V'] = $Q4[0];

		$q4_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q4 in (9,10) ");
		$Q4_1=mysqli_fetch_array($q4_1); 
		$data['Q4_1'] = round(($Q4_1[0] / $sql_base ) * 100,1); 

		$q4_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q4 in (1,2,3,4,5,6,7,8) ");
		$Q4_2=mysqli_fetch_array($q4_2); 
		$data['Q4_2'] = round(($Q4_2[0] / $sql_base ) * 100,1);
		/*Q4 script end*/


		/*Q5 script start*/
		$columnS=array("record","Q9g","Q5");
        $mq=query_marge($time,$columnS);
		$q5_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q5 in (0,1,2,3,4,5,6,7) ");
		$Q5_1=mysqli_fetch_array($q5_1); 
		$data['Q5_1'] = round(($Q5_1[0] / $sql_base ) * 100,1);


		$q5_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q5 in (8,9) ");
		$Q5_2=mysqli_fetch_array($q5_2); 
		$data['Q5_2'] = round(($Q5_2[0] / $sql_base ) * 100,1);


		$q5_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q5 in (10,11) ");
		$Q5_3=mysqli_fetch_array($q5_3); 
		$data['Q5_3'] = round(($Q5_3[0] / $sql_base ) * 100,1);

		$Q5_NPS = $data['Q5_3'] - $data['Q5_1'];
		$data['Q5_NPS'] = round($Q5_NPS,1);
		/*Q5 script end*/


		/*Q8 script start*/
		$columnS=array("record","Q9g","Q8");
        $mq=query_marge($time,$columnS);
		$q8=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q8 in (9,10) ");
		$Q8=mysqli_fetch_array($q8); 
		$data['Q8'] = round(($Q8[0] / $sql_base ) * 100,1);
		$data['Q8V'] = $Q8[0];


		$q8_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q8 in (9,10) ");
		$Q8_1=mysqli_fetch_array($q8_1); 
		$data['Q8_1'] = round(($Q8_1[0] / $sql_base ) * 100,1);


		$q8_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q8 in (1,2,3,4,5,6,7,8) ");
		$Q8_2=mysqli_fetch_array($q8_2); 
		$data['Q8_2'] = round(($Q8_2[0] / $sql_base ) * 100,1);

		/*Q8 script end*/

			/*Q9 script start*/
			$columnS=array("record","Q9g","Q9");
        $mq=query_marge($time,$columnS);
		$q9_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q9 in (1,2,3,4,5,6,7) ");
		$Q9_1=mysqli_fetch_array($q9_1); 
		$data['Q9_1'] = round(($Q9_1[0] / $sql_base ) * 100,1);


		$q9_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q9 in (8,9) ");
		$Q9_2=mysqli_fetch_array($q9_2); 
		$data['Q9_2'] = round(($Q9_2[0] / $sql_base ) * 100,1);


		$q9_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND  Q9 in (10,11) ");
		$Q9_3=mysqli_fetch_array($q9_3); 
		$data['Q9_3'] = round(($Q9_3[0] / $sql_base ) * 100,1);

		$Q9_NPS = $data['Q9_3'] - $data['Q9_1'];
		$data['Q9_NPS'] = round($Q9_NPS,1);
		/*Q9 script end*/
	}
	



	
	
	echo json_encode($data);
?>