<?php
include '../config.php';
$data = array();
$sql_base;
//$circle = $_POST['circle'];
$condX = $_POST['condX'];
$HQ2  = $condX;
$conX = $_POST['conX'];
$time = $_POST['timeX'];
$tablecondX = $_POST['tablecondX'];
$timeZ = "('".implode("','",$time)."')";
$columnS=array("record","Q9g","Main_dealer_name");
$mq=query_marge($time,$columnS);

    $q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND t.`Main_dealer_name` = '$HQ2' ");
   
   $baseX=mysqli_fetch_array($q);
    $base = $baseX[0];
	/*Base script start*/
	
	$sql_base=$base;
	$data['base'] = $sql_base;
	/*Base script End*/
	if($sql_base==0)
	{
	    $data['ssi'] = 0;
	    $data['Q5_1'] = 0;
	    $data['Q5_2'] = 0;
	    $data['Q5_3'] = 0;
	    $data['Q5_NPS'] = 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","Main_dealer_name");
    $mq=query_marge($time,$columnS);
	$ssi=mysqli_query($conn,"SELECT avg(`ssi_v`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND t.`Main_dealer_name` = '$HQ2' ");
	$SSI=mysqli_fetch_array($ssi); 
	$data['ssi'] = round($SSI[0]);
	/*SSI script end*/

    /*Q5 script start*/
    $columnS=array("record","Q9g","Q5","Main_dealer_name");
    $mq=query_marge($time,$columnS);
	$q5_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND t.`Main_dealer_name` = '$HQ2' AND t.`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 t.`Main_dealer_name` = '$HQ2' AND t.`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 t.`Main_dealer_name` = '$HQ2' AND t.`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'] = $Q5_NPS;
	/*Q5 script end*/

    /*Q9 script start*/
    $columnS=array("record","Q9g","Q9","Main_dealer_name");
    $mq=query_marge($time,$columnS);
	$q9_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND t.`Main_dealer_name` = '$HQ2' AND t.`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 t.`Main_dealer_name` = '$HQ2' AND t.`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 t.`Main_dealer_name` = '$HQ2' AND t.`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);
?>