<?php
 include '../config.php';
 $data = array();
 $sql_base;
 $digiSSI_arr = array();
 $dfSSI_arr = array();
 $scSSI_arr = array();
 $dSSI_arr = array();
 $paperSSI_arr = array();
 $deSSI_arr = array();
 $condX = $_POST['condX'];
 $conX = $_POST['conX'];
 $timeZ = $_POST['timeX'];
 $tablecondX = $_POST['tablecondX'];
   foreach($timeZ as $key => $value)
   {
        /*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*/

        /*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*/

        /*scSSI script start*/
        $columnS=array("record","Q9g","Q19_n1",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
        $scSSI=mysqli_query($conn,"SELECT avg(`Q19_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
        $scSSIV=mysqli_fetch_array($scSSI); 
        $scSSIV2 = round($scSSIV[0]);
        array_push($scSSI_arr,$scSSIV2);
        /*scSSI script end*/

        /*dSSI script start*/
        $columnS=array("record","Q9g","Q24_n1",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
        $dSSI=mysqli_query($conn,"SELECT avg(`Q24_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
        $dSSIV=mysqli_fetch_array($dSSI); 
        $dSSIV2 = round($dSSIV[0]);
        array_push($dSSI_arr,$dSSIV2);
        /*dSSI script end*/

        /*paperSSI script start*/
         $columnS=array("record","Q9g","Q26_n1",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
        $paperSSI=mysqli_query($conn,"SELECT avg(`Q26_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
        $paperSSIV=mysqli_fetch_array($paperSSI); 
        $paperSSIV2 = round($paperSSIV[0]);
        array_push($paperSSI_arr,$paperSSIV2);
        /*paperSSI script end*/

        /*deSSI script start*/
        $columnS=array("record","Q9g","Q27_n1",$tablecondX);
        $mq=query_marge($timeZ,$columnS);
        $deSSI=mysqli_query($conn,"SELECT avg(`Q27_n1`) FROM ($mq) AS t WHERE t.`Q9g` IN ($value) AND $condX 1=1");
        $deSSIV=mysqli_fetch_array($deSSI); 
        $deSSIV2 = round($deSSIV[0]);
        array_push($deSSI_arr,$deSSIV2);
        /*deSSI script end*/
   }
   $data['f1'] = $digiSSI_arr;
   $data['f2'] = $dfSSI_arr;
   $data['f3'] = $scSSI_arr;
   $data['f4'] = $dSSI_arr;
   $data['f5'] = $paperSSI_arr;
   $data['f6'] = $deSSI_arr;
   

 echo json_encode($data);

?>