<?php
 include '../config.php';
 $data = array();
 $sql_base;
 $dfSSI_arr = array();
 $Q25b_arr = array();
 $Q25c_arr = array();
 $Q25d_arr = array();
 $Q25e_arr = array();
 $Q24_arr = array();
 $condX = $_POST['condX'];
 $conX = $_POST['conX'];
 $timeZ = $_POST['timeX'];
 $poor="1,2,3,4";
 $average="5,6";
 $Outstanding="7,8,9,10";
 $tablecondX = $_POST['tablecondX'];
$columnS=array("record","Q9g",$tablecondX);
$mq=query_marge($timeZ,$columnS);
   foreach($timeZ as $key => $value)
   {
    $Q23V_arr = array();
      if($conX==1)
      {    
         $condX="";   
         $q=mysqli_query($conn,"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($Q25b_arr,0);
        array_push($Q25c_arr,0);
        array_push($Q25d_arr,0);
        array_push($Q25e_arr,0);
        array_push($Q24_arr,0);
      }
      else
      {
          /*Q24 script start*/
            $columnS=array("record","Q9g","Q24",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq24 = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q24` != '' ");
            $baseq24=mysqli_fetch_array($Baseq24);
            $q24=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q24` IN (1,2,3,4,5,6,7,8) ");
            $Q24=mysqli_fetch_array($q24); 
            $Q24V = round(($Q24[0] / $sql_base ) * 100,1);
            array_push($Q24_arr,$Q24V);
            
         /*Q24 script end*/
        
        /*Q25b script start*/
        $columnS=array("record","Q9g","Q25b",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq25b = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25b` != '' ");
            $baseq25b=mysqli_fetch_array($Baseq25b);
            $q25b=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25b` = '1' ");
            $Q25b=mysqli_fetch_array($q25b); 
            $Q25B = round(($Q25b[0] / $sql_base ) * 100,1);
            array_push($Q25b_arr,$Q25B);
        /*Q25b script end*/

        /*Q25c script start*/
        $columnS=array("record","Q9g","Q25c",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq25c = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25c` != '' ");
            $baseq25c=mysqli_fetch_array($Baseq25c);
            $q25c=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25c` = '1' ");
            $Q25c=mysqli_fetch_array($q25c); 
            $Q25C = round(($Q25c[0] / $sql_base ) * 100,1);
            array_push($Q25c_arr,$Q25C);
        /*Q19b script end*/

        /*Q25d script start*/
        $columnS=array("record","Q9g","Q25d",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq25d = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25d` != '' ");
            $baseq25d=mysqli_fetch_array($Baseq25d);
            $q25d=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25d` = '1' ");
            $Q25d=mysqli_fetch_array($q25d); 
            $Q25D = round(($Q25d[0] / $sql_base ) * 100,1);
            array_push($Q25d_arr,$Q25D);
        /*Q25d script end*/
        
        /*Q25e script start*/
        $columnS=array("record","Q9g","Q25e",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $Baseq25e = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25e` != '' ");
            $baseq25e=mysqli_fetch_array($Baseq25e);
            $q25e=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q25e` = '1' ");
            $Q25e=mysqli_fetch_array($q25e); 
            $Q25E = round(($Q25e[0] / $sql_base ) * 100,1);
            array_push($Q25e_arr,$Q25E);
        /*Q25e script end*/
      }
        
        /*dfSSI script start*/
       $columnS=array("record","Q9g","Q24_n1",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $dfSSI=mysqli_query($conn,"SELECT avg(`Q24_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
            $dfSSIV=mysqli_fetch_array($dfSSI); 
            $dfSSIV2 = round($dfSSIV[0]);
            array_push($dfSSI_arr,$dfSSIV2);
        /*dfSSI script end*/
   }

   $data['overalldeal'] = $dfSSI_arr;
   $data['Q25b'] = $Q25b_arr;
   $data['Q25c'] = $Q25c_arr;
   $data['Q25d'] = $Q25d_arr;
   $data['Q25e'] = $Q25e_arr;
   $data['Q24'] = $Q24_arr;

  
 echo json_encode($data);

?>
























