<?php
 include '../config.php';
 $data = array();
 $sql_base;
 $dfSSI_arr = array();
 $Q18a_arr = array();
 $Q18c_arr = array(); $Q18d_arr = array();  
 $Q17_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) ");
         $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($Q18a_arr,0);
        array_push($Q18c_arr,0);        
        array_push($Q18d_arr,0);        
        array_push($Q17_arr,0);
      }
      else
      {		  		  
          /*Q17 script start*/		
          $columnS=array("record","Q9g","Q17",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
          $q17Q=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q17` IN (1,2,3,4,5,6,7,8)");		
          //echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN $timeZ AND $condX Q11 = '2' ";		 
          //echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX Q17 IN (1,2,3,4,5,6,7,8)";		 
          $Q17=mysqli_fetch_array($q17Q); 		 
          $Q17b = round(($Q17[0] / $sql_base ) * 100,1);				
          array_push($Q17_arr,$Q17b);		
          /*Q17 script end*/
        /*Q18a script start*/
        $columnS=array("record","Q9g","Q18a",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
            $Baseq18a = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q18a` != '' ");
            $baseq18a=mysqli_fetch_array($Baseq18a);

            $q18a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q18a` = '2' ");
            //echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX Q11 = '2' ";
            $Q18a=mysqli_fetch_array($q18a); 
            $Q18aV = round(($Q18a[0] / $sql_base ) * 100,1);
            array_push($Q18a_arr,$Q18aV);
        /*Q18a script end*/
        
        /*Q18c script start*/
        $columnS=array("record","Q9g","Q18c",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
            $Baseq18c = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q18c` != '' ");
            $baseq18c=mysqli_fetch_array($Baseq18c);
            $q18c=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t  WHERE t.`Q9g` IN ($value) AND $condX t.`Q18c` = '2' ");
            $Q18c=mysqli_fetch_array($q18c); 
            $Q18cV = round(($Q18c[0] / $sql_base ) * 100,1);
            array_push($Q18c_arr,$Q18cV);
        /*Q18c script end*/
        
        /*Q18d script start*/
        $columnS=array("record","Q9g","Q18d",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
            $Baseq18d = mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q18d` != '' ");
            $baseq18d=mysqli_fetch_array($Baseq18d);
            $q18d=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t  WHERE t.`Q9g` IN ($value) AND $condX t.`Q18d` = '2' ");
            $Q18d=mysqli_fetch_array($q18d); 
            $Q18dV = round(($Q18d[0] / $sql_base ) * 100,1);
            array_push($Q18d_arr,$Q18dV);
        /*Q18d script end*/
      }
      /*dfSSI script start*/
      $columnS=array("record","Q9g","Q17_n1",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
      $dfSSI=mysqli_query($conn,"SELECT avg(`Q17_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['overallfacility'] = $dfSSI_arr;
    $data['Q18a'] = $Q18a_arr;
    $data['Q18c'] = $Q18c_arr;    
    $data['Q18d'] = $Q18d_arr;   
    $data['Q17b'] = $Q17_arr;
  
   

 echo json_encode($data);

?>