<?php
 include '../config.php';
 $data = array();
 $sql_base;
 $digiSSI_arr = array();
 $Q11_arr = array();
 $Q12_arr = array();
 $Q13_arr = array();
 $Q14_arr = array();
 $Q15_arr = array();
 $Q15a_arr = array();
 $Q15c_arr = array();
 $Q15b_arr = array();
 $Q15d_arr = array();
 $condX = $_POST['condX'];
 $conX = $_POST['conX'];
 $timeZ = $_POST['timeX'];
 $poor="1,2,3,4";
$average="5,6,7,8";
$Outstanding="9,10";
$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($Q11_arr,0);
        array_push($Q12_arr,0);
        array_push($Q13_arr,0);
        array_push($Q14_arr,0);

        array_push($Q15_arr,0);
        array_push($Q15a_arr,0);
        array_push($Q15c_arr,0);
        array_push($Q15b_arr,0);
        array_push($Q15d_arr,0);
      }
      else
      {
         /*Q11 script start*/
            $columnS=array("record","Q9g","Q11",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q11=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q11` = '1' ");
            //echo "SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX Q11 = '2' ";
            $Q11=mysqli_fetch_array($q11); 
            $Q11V = round(($Q11[0] / $sql_base ) * 100,1);
            array_push($Q11_arr,$Q11V);
        /*Q11 script end*/

        /*Q12 script start*/
            $columnS=array("record","Q9g","Q12",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            
            $q12pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q12 != '' ");
		    $Q12pbaseX=mysqli_fetch_array($q12pbase);
		    $Q12pbase = $Q12pbaseX[0];
		
            $q12o=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t  WHERE t.`Q9g` IN ($value) AND $condX t.`Q12` in ($Outstanding) ");
            $Q12o=mysqli_fetch_array($q12o); 
            if($Q12pbase==0)
            {
                $Q12oV = 0;
            }
            else
            {
                $Q12oV = round(($Q12o[0] / $Q12pbase ) * 100,1);
            }
            array_push($Q12_arr,$Q12oV);
        /*Q12 script end*/	

        /*Q13 script start*/
            $columnS=array("record","Q9g","Q13",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            
            $q13pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q13 != '' ");
    		$Q13pbaseX=mysqli_fetch_array($q13pbase);
    		$Q13pbase = $Q13pbaseX[0];
		
            $q13o=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t  WHERE t.`Q9g` IN ($value) AND $condX t.`Q13` in ($Outstanding) ");
            $Q13o=mysqli_fetch_array($q13o); 

            if($Q13pbase==0)
            {
                $Q13oV = 0;
            }
            else
            {
                $Q13oV = round(($Q13o[0] / $Q13pbase ) * 100,1);
            }

            array_push($Q13_arr,$Q13oV); 
        /*Q13 script end*/
        
        /*Q14 script start*/
            $columnS=array("record","Q9g","Q14",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            
            $q14pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q14 != '' ");
		    $Q14pbaseX=mysqli_fetch_array($q14pbase);
		    $Q14pbase = $Q14pbaseX[0];
		
            $q14o=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t  WHERE t.`Q9g` IN ($value) AND $condX t.`Q14` in ($Outstanding) ");
            $Q14o=mysqli_fetch_array($q14o); 

            if($Q14pbase==0)
            {
                $Q14oV = 0;
            }
            else
            {
                $Q14oV = round(($Q14o[0] / $Q14pbase ) * 100,1);
            }
            array_push($Q14_arr,$Q14oV); 
        /*Q14 script end*/

        /*Q15 script start*/
            $columnS=array("record","Q9g","Q15",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            $q15=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q15` = '1' ");
            $Q15=mysqli_fetch_array($q15); 
            $Q15V = round(($Q15[0] / $sql_base ) * 100,1);
            array_push($Q15_arr,$Q15V); 
        /*Q15 script end*/

        /*Q15a script start*/
            $columnS=array("record","Q9g","Q15a",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            
            $q15apbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q15a != '' ");
		    $Q15apbaseX=mysqli_fetch_array($q15apbase);
		    $Q15apbase = $Q15apbaseX[0];
		
            $q15a=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q15a` = '1' ");
            $Q15a=mysqli_fetch_array($q15a); 
            if($Q15apbase==0)
            {
                $Q15aV = 0 ;
            }
            else
            {
                $Q15aV = round(($Q15a[0] / $Q15apbase ) * 100,1);
            }
            
            array_push($Q15a_arr,$Q15aV); 
        /*Q15a script end*/
	
        /*Q15c script start*/
            $columnS=array("record","Q9g","Q15c",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            
            $q15cpbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q15c != '' ");
		    $Q15cpbaseX=mysqli_fetch_array($q15cpbase);
		    $Q15cpbase = $Q15cpbaseX[0];
		
            $q15c=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q15c` = '1' ");
            $Q15c=mysqli_fetch_array($q15c); 

            if($Q15cpbase==0)
            {
                $Q15cV = 0 ;
            }
            else
            {
                $Q15cV = round(($Q15c[0] / $Q15cpbase ) * 100,1);
            }

            
            array_push($Q15c_arr,$Q15cV);
        /*Q15c script end*/	

        /*Q15b script start*/
            $columnS=array("record","Q9g","Q15b",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            
            $q15pbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q15b != '' ");
		    $Q15pbaseX=mysqli_fetch_array($q15pbase);
            $Q15pbase = $Q15pbaseX[0];
        
            $q15bO=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q15b` in ($Outstanding) ");
            $Q15bO=mysqli_fetch_array($q15bO); 

            if($Q15pbase==0)
            {
                $Q15bOV = 0 ;
            }
            else
            {
                $Q15bOV = round(($Q15bO[0] / $Q15pbase ) * 100,1);
            }

            
            array_push($Q15b_arr,$Q15bOV);
	    /*Q15b script end*/

        /*Q15d script start*/
            $columnS=array("record","Q9g","Q15d",$tablecondX);
            $mq=query_marge($timeZ,$columnS);
            
            $q15dpbase=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.Q15d != '' ");
		    $Q15dpbaseX=mysqli_fetch_array($q15dpbase);
		    $Q15dpbase = $Q15dpbaseX[0];
		
            $q15dO=mysqli_query($conn,"SELECT COUNT(`record`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q15d` in ($Outstanding) ");
            $Q15dO=mysqli_fetch_array($q15dO); 

            if($Q15dpbase==0)
            {
                $Q15dOV = 0 ;
            }
            else
            {
                $Q15dOV = round(($Q15dO[0] / $Q15dpbase ) * 100,1); 
            }
            array_push($Q15d_arr,$Q15dOV);
	    /*Q15d script end*/
      }
       /*digiSSI script start*/
       $columnS=array("record","Q9g","Q11a_n1",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
       $digiSSI=mysqli_query($conn,"SELECT avg(`Q11a_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX t.`Q11a_n1` != ''");
            
       $digiSSIV=mysqli_fetch_array($digiSSI); 
       $digiSSIVXX = $digiSSIV[0] ?? 0; // Ensure it's not null
       array_push($digiSSI_arr,$digiSSIVXX);
       /*digiSSI script end*/
      
    }

    $data['dizioverall'] = $digiSSI_arr;

    $data['q11'] = $Q11_arr;
    $data['q12'] = $Q12_arr;
    $data['q13'] = $Q13_arr;
    $data['q14'] = $Q14_arr;

    $data['q15'] = $Q15_arr;
    $data['q15a'] = $Q15a_arr;
    $data['q15c'] = $Q15c_arr;
    $data['q15b'] = $Q15b_arr;
    $data['q15d'] = $Q15d_arr;
   
   

 echo json_encode($data);

?>