<?php
include "../config.php";
include "classes.php";

$new = array();
$new2 = array();
$condX = $_REQUEST['condX'];
$conX = $_REQUEST['conX'];
$tblX = $_REQUEST['tblX'];
$countryValX = $_REQUEST['countryValX'];
$allOptions = $_REQUEST['allOptions'];

$spcX = "1=1";
$spcX2 = "1=1";


$recordCounter1 = new RecordCounter1($conn,$condX,$tblX,$spcX,$spcX2);
$recordCounter2 = new RecordCounter2($conn,$condX,$tblX,$spcX,$spcX2);
$recordCounter3 = new RecordCounter3($conn,$condX,$tblX,$spcX,$spcX2);
$recordCounter5 = new RecordCounter5($conn,$condX,$tblX,$spcX,$spcX2);
$recordCounter6 = new RecordCounter6($conn,$condX,$tblX,$spcX,$spcX2);

if($countryValX[0]=="1")
{    
    $data['race'] = $recordCounter1->countRecords1('S2xSG', array(1,2,3,98));
}
else if($countryValX[0]=="5")
{    
    $data['race'] = $recordCounter1->countRecords1('S2xMY', array(1,2,3,98));
}
else
{
    $data['race'] = [0];
}

if($countryValX[0]!="0")
{
    $data['S7'] = $recordCounter1->countRecords1('S7', $allOptions);
}
else
{
    $data['S7'] = [0];
}

$data['HAGE'] = $recordCounter1->countRecords1('HAGE', array(2,3,4,5,6));
$data['HAGE2'] = $recordCounter5->countRecords5('S5', array(41,42,43,44));
$data['S4'] = $recordCounter1->countRecords1('S4', array(1,2));
$data['P5'] = $recordCounter1->countRecords1('P5', array(1,2,3,33,6,7,32));
$data['S2xSG'] = $recordCounter1->countRecords1('S2xSG', array(1,2,3,4));
$data['P2'] = $recordCounter1->countRecords1('P2', array(1,2));
$data['S8'] = $recordCounter1->countRecords1('S8', array(1,2,3,4,5));
$data['S10'] = $recordCounter1->countRecords1('S10', array(1,2,3));
$data['S1'] = $recordCounter6->countRecords6('HCITY', array(1,2),array(2,3,4,5,6));
$data['P6'] = $recordCounter1->countRecords1('P6', array(1,2,3));

$basesql = mysqli_query($conn,"SELECT COUNT(RESPID) FROM $tblX WHERE P2 IN (1,2) AND $condX");
$basesqlX=mysqli_fetch_array($basesql);   
$base = $basesqlX[0];

$count4sql = mysqli_query($conn,"SELECT COUNT(RESPID) FROM $tblX WHERE P2 = 2 AND $condX");
$count4sqlX=mysqli_fetch_array($count4sql);   
$count4 = $count4sqlX[0];
$percentage4 = round(($count4/$base)*100);

$new[] = [
    'bCount' => $base,
    'cCount' => $count4,
    'percentage' => $percentage4,
];

$count1sql = mysqli_query($conn,"SELECT COUNT(RESPID) FROM $tblX WHERE (P3_1 > 0 OR P3_2 > 0) AND $condX");
$count1sqlX=mysqli_fetch_array($count1sql);   
$count1 = $count1sqlX[0];
$percentage1 = round(($count1/$base)*100);

$new[] = [
    'bCount' => $base,
    'cCount' => $count1,
    'percentage' => $percentage1,
];

//array_push($new2,$new);

$count2sql = mysqli_query($conn,"SELECT COUNT(RESPID) FROM $tblX WHERE P3_3 > 0 AND $condX");
$count2sqlX=mysqli_fetch_array($count2sql);   
$count2 = $count2sqlX[0];
$percentage2 = round(($count2/$base)*100);

$new[] = [
    'bCount' => $base,
    'cCount' => $count2,
    'percentage' => $percentage2,
];

//array_push($new2,$new);

$count3sql = mysqli_query($conn,"SELECT COUNT(RESPID) FROM $tblX WHERE P3_4 > 0 AND $condX");
$count3sqlX=mysqli_fetch_array($count3sql);   
$count3 = $count3sqlX[0];
$percentage3 = round(($count3/$base)*100);

$new[] = [
    'bCount' => $base,
    'cCount' => $count3,
    'percentage' => $percentage3,
];

//array_push($new2,$new);

$data['children_combine'] = $new;


echo json_encode($data);

?>