<?php
 include '../config.php';
 $data = array();
 $sql_base;
 $ssi_arr = array();
 $Q4_arr = array();
 $Q5_arr = array();
 $Q8_arr = array();
 $Q9_arr = array();
 $condX = $_POST['condX'];
 $conX = $_POST['conX'];
 $timeZ = $_POST['timeX'];
$tablecondX = $_POST['tablecondX'];
$columnS=array("record","Q9g",$tablecondX);
$mq=query_marge($timeZ,$columnS);
   foreach($timeZ as $key => $value)
   {
      if($conX==1)
      {    
         $condX="";   
         $q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) ");
        // echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value)";
         $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 ($value) AND $condX 1=1");
         $baseX=mysqli_fetch_array($q);
         $base=$baseX[0];
      }

      $sql_base = $base;
      if($sql_base==0)
      {
         array_push($Q4_arr,0);
         array_push($Q5_arr,0);
         array_push($Q8_arr,0);
         array_push($Q9_arr,0);
      }
      else
      {
        /*Q4 script start*/
            $columnS=array("record","Q9g","Q4",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q4=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q4` in (9,10) ");
            $Q4=mysqli_fetch_array($q4); 
            $Q4V = round(($Q4[0]/ $sql_base ) * 100,1);
            array_push($Q4_arr,$Q4V);
        /*Q4 script start*/

        /*Q5 script start*/
            $columnS=array("record","Q9g","Q5",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q5_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q5` in (0,1,2,3,4,5,6,7) ");
            $Q5_1=mysqli_fetch_array($q5_1); 
            $Q5_1V = round(($Q5_1[0]/ $sql_base ) * 100,1);
            
            
            $q5_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q5` in (8,9) ");
            $Q5_2=mysqli_fetch_array($q5_2); 
            $Q5_2V = round(($Q5_2[0]/ $sql_base ) * 100,1);
            
            
            $q5_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q5` in (10,11) ");
            $Q5_3=mysqli_fetch_array($q5_3); 
            $Q5_3V = round(($Q5_3[0]/ $sql_base ) * 100,1);
            
            $Q5_NPS = $Q5_3V - $Q5_1V;
            array_push($Q5_arr,$Q5_NPS);
        /*Q5 script end*/

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

        /*Q9 script start*/
            $columnS=array("record","Q9g","Q9",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
         $q9_1=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q9` in (1,2,3,4,5,6,7) ");
         $Q9_1=mysqli_fetch_array($q9_1); 
         $Q9_1V = round(($Q9_1[0]/ $sql_base ) * 100,1);
         
         
         $q9_2=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q9` in (8,9) ");
         $Q9_2=mysqli_fetch_array($q9_2); 
         $Q9_2V = round(($Q9_2[0]/ $sql_base ) * 100,1);
         
         
         $q9_3=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q9` in (10,11) ");
         $Q9_3=mysqli_fetch_array($q9_3); 
         $Q9_3V = round(($Q9_3[0]/ $sql_base ) * 100,1);
         
         $Q9_NPS = $Q9_3V - $Q9_1V;
         array_push($Q9_arr,$Q9_NPS);
        /*Q9 script end*/
      }
     /*SSI script start*/
        $columnS=array("record","Q9g","ssi_v",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
      $ssi=mysqli_query($conn,"SELECT avg(`ssi_v`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
      $SSI=mysqli_fetch_array($ssi); 
      $ssiV = round($SSI[0]);
      array_push($ssi_arr,$ssiV);
     /*SSI script end*/

      

      
     /*Q4 script end*/
   }
   $data['ssi'] = $ssi_arr;
   $data['q4'] = $Q4_arr;
   $data['q5'] = $Q5_arr;
   $data['q8'] = $Q8_arr;
   $data['q9'] = $Q9_arr;
   

 echo json_encode($data);

?>