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

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

$timeZ = "('".implode("','",$time)."')";

$tablecondX = $_POST['tablecondX'];
$timeZ = "('".implode("','",$time)."')";
$poor="1,2,3,4";
$average="5,6";
$Outstanding="7,8,9,10";
		foreach($zoneVal as $key => $value)
		{
			if($value=='Overall')
			{
			    $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];
                }
				 $sql_base=$base;
				$data['base'] = $sql_base;
				/*SSI script start*/
    				$columnS=array("record","Q9g","ssi_v",$tablecondX);
                    $mq=query_marge($time,$columnS);
					$ssi=mysqli_query($conn,"SELECT avg(`ssi_v`) FROM ($mq) AS t WHERE t.Q9g IN $timeZ AND $condX 1=1");
					$SSI=mysqli_fetch_array($ssi); 
					$SSIV = round($SSI[0]);
				/*SSI script end*/
				array_push($zoneValSSI_arr,$SSIV);
				if($sql_base==0)
				{
					$Q9_NPSV = 0;
					array_push($zoneValNPS_arr,$Q9_NPSV);
				}
				else
				{
					/*Q9 script start*/
					$columnS=array("record","Q9g","Q9",$tablecondX);
                    $mq=query_marge($time,$columnS);
					$q9_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.Q9g IN $timeZ AND $condX t.Q9 in (1,2,3,4,5,6,7) ");
					$Q9_1=mysqli_fetch_array ($q9_1); 
					$dataD = 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 $condX t.Q9 in (8,9) ");
					$Q9_2=mysqli_fetch_array ($q9_2); 
					$dataPa = 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 $condX t.Q9 in (10,11) ");
					$Q9_3=mysqli_fetch_array ($q9_3); 
					$dataPr = round(($Q9_3[0] / $sql_base ) * 100,1);

					$Q9_NPS = $dataPr - $dataD;
					$Q9_NPSV = round($Q9_NPS,1);
					array_push($zoneValNPS_arr,$Q9_NPSV);
					/*Q9 script end*/
				}
				
			}
			else
			{
			    $columnS=array("record","Q9g","Zone",$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 AND t.Zone IN ('$value') ");
					$baseX=mysqli_fetch_array ($q); 
					$base  = $baseX[0];
				}
				
				if($conX==2){
				   $condX="";   
				   $q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.Q9g IN $timeZ AND  t.Zone IN ('$value') AND $condX 1=1");
				  //echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.Q9g IN $timeZ AND t.Zone IN ('$value') AND $condX 1=1";
				   $baseX=mysqli_fetch_array ($q);
				   $base  = $baseX[0];
				}
				$sql_base=$base;
				$data['base'] = $sql_base;
				/*SSI script start*/
				    $columnS=array("record","Q9g","ssi_v","Zone",$tablecondX);
                    $mq=query_marge($time,$columnS);
					$ssi=mysqli_query($conn,"SELECT avg(`ssi_v`) FROM ($mq) AS t WHERE t.Q9g IN $timeZ AND t.Zone IN ('$value') AND $condX 1=1");
					$SSI=mysqli_fetch_array($ssi); 
					$SSIV = round($SSI[0]);
				/*SSI script end*/
				array_push($zoneValSSI_arr,$SSIV);
				if($sql_base==0)
				{
					$Q9_NPSV = 0;
					array_push($zoneValNPS_arr,$Q9_NPSV);
				}
				
				else
				{
					/*Q9 script start*/
					$columnS=array("record","Q9g","Q9","Zone",$tablecondX);
                    $mq=query_marge($time,$columnS);
					$q9_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.Q9g IN $timeZ AND t.Zone IN ('$value') AND $condX t.Q9 in (1,2,3,4,5,6,7) ");
					$Q9_1=mysqli_fetch_array ($q9_1); 
					$dataD = 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 t.Zone IN ('$value') AND $condX t.Q9 in (8,9) ");
					$Q9_2=mysqli_fetch_array ($q9_2); 
					$dataPa = 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 t.Zone IN ('$value') AND $condX t.Q9 in (10,11) ");
					$Q9_3=mysqli_fetch_array ($q9_3); 
					$dataPr = round(($Q9_3[0] / $sql_base ) * 100,1);

					$Q9_NPS = $dataPr - $dataD;
					$Q9_NPSV = round($Q9_NPS,1);
					array_push($zoneValNPS_arr,$Q9_NPSV);
					/*Q9 script end*/
				}
				
			}
		}
		$data['ssi'] = $zoneValSSI_arr;
		$data['nps'] = $zoneValNPS_arr;

		echo json_encode($data);
?>