<?php
include "../config.php";
include "classes.php";
$data = array();
$ever = array();
$consumed1Y = array();
$consumed3M = array();
$consumed7D = array();
$condX = $_REQUEST['condX'];
$conX = $_REQUEST['conX'];
$tblX = $_REQUEST['tblX'];
$spcX = "1=1";
$spcX2 = "1=1";

if($conX==1)
{    
    $condX="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);

// cheese ever tried
$rangeXX = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

foreach($rangeXX as $key => $value)
{
    $arr1 = array();

    $c1bsql = mysqli_query($conn,"SELECT COUNT(`S11_".$value."`) FROM `w1` WHERE $condX AND S11_".$value." IN (1,2,3)");
    $c1bsqlX= mysqli_fetch_array($c1bsql);   
    $c1bsqlXX = $c1bsqlX[0];
    
    $c1csql = mysqli_query($conn,"SELECT COUNT(`S11_".$value."`) FROM `w1` WHERE ( S11_".$value." IN (1) OR S12_".$value." IN (1,2,3) ) AND $condX");
    $c1csqlX=mysqli_fetch_array($c1csql);   
    $c1csqlXX = $c1csqlX[0];

    if($c1bsqlXX==0)
    {
        $c1percentage = 0;
    }
    else
    {
        $c1percentage = round(($c1csqlXX/$c1bsqlXX)*100);
    }
    
    array_push($arr1,$c1percentage,$c1bsqlXX,$c1csqlXX);

    $ever[] = [
        'bCount' => $arr1[1],
        'cCount' => $arr1[2],
        'percentage' => $arr1[0],
    ];

    $arr2 = array();

    $c2bsql = mysqli_query($conn,"SELECT COUNT(`S11_".$value."`) FROM `w1` WHERE S11_".$value." IN (1,2,3) AND $condX");
    $c2bsqlX= mysqli_fetch_array($c2bsql);   
    $c2bsqlXX = $c2bsqlX[0];
    
    $c2csql = mysqli_query($conn,"SELECT COUNT(`RESPID`) FROM `w1` WHERE ( S11_".$value." IN (1) OR S12_".$value." IN (1,2) ) AND $condX");
    $c2csqlX=mysqli_fetch_array($c2csql);   
    $c2csqlXX = $c2csqlX[0];

    if($c2bsqlXX==0)
    {
        $c2percentage = 0;
    }
    else
    {
        $c2percentage = round(($c2csqlXX/$c2bsqlXX)*100);
    }
    
    array_push($arr2,$c2percentage,$c2bsqlXX,$c2csqlXX);

    $consumed1Y[] = [
        'bCount' => $arr2[1],
        'cCount' => $arr2[2],
        'percentage' => $arr2[0],
    ];

    $arr3 = array();

    $c3bsql = mysqli_query($conn,"SELECT COUNT(`S11_".$value."`) FROM `w1` WHERE S11_".$value." IN (1,2,3) AND $condX");
    $c3bsqlX= mysqli_fetch_array($c3bsql);   
    $c3bsqlXX = $c3bsqlX[0];
    
    $c3csql = mysqli_query($conn,"SELECT COUNT(`RESPID`) FROM `w1` WHERE ( S11_".$value." IN (1) OR S12_".$value." IN (1) ) AND $condX");
    $c3csqlX=mysqli_fetch_array($c3csql);   
    $c3csqlXX = $c3csqlX[0];

    if($c3bsqlXX==0)
    {
        $c3percentage = 0;
    }
    else
    {
        $c3percentage = round(($c3csqlXX/$c3bsqlXX)*100);
    }
    
    array_push($arr3,$c3percentage,$c3bsqlXX,$c3csqlXX);

    $consumed3M[] = [
        'bCount' => $arr3[1],
        'cCount' => $arr3[2],
        'percentage' => $arr3[0],
    ];
    
    $arr4 = array();

    $c4bsql = mysqli_query($conn,"SELECT COUNT(`S11_".$value."`) FROM `w1` WHERE S11_".$value." IN (1,2,3) AND $condX");
    $c4bsqlX= mysqli_fetch_array($c4bsql);   
    $c4bsqlXX = $c4bsqlX[0];
    
    $c4csql = mysqli_query($conn,"SELECT COUNT(`RESPID`) FROM `w1` WHERE S11_".$value." IN (1) AND $condX");
    $c4csqlX=mysqli_fetch_array($c4csql);   
    $c4csqlXX = $c4csqlX[0];

    if($c4bsqlXX==0)
    {
        $c4percentage = 0;
    }
    else
    {
        $c4percentage = round(($c4csqlXX/$c4bsqlXX)*100);
    }

    array_push($arr4,$c4percentage,$c4bsqlXX,$c4csqlXX);

    $consumed7D[] = [
        'bCount' => $arr4[1],
        'cCount' => $arr4[2],
        'percentage' => $arr4[0],
    ];
}

$data['ever'] = $ever;
$data['consumed1Y'] = $consumed1Y;
$data['consumed3M'] = $consumed3M;
$data['consumed7D'] = $consumed7D;

// $data['S13_1'] = $recordCounter1->countRecords1('S13_1', array(1,2,3,4,5,6,7));
// $data['S13_3'] = $recordCounter1->countRecords1('S13_3', array(1,2,3,4,5,6,7));
// $data['S13_8'] = $recordCounter1->countRecords1('S13_8', array(1,2,3,4,5,6,7));
// $data['S13_6'] = $recordCounter1->countRecords1('S13_6', array(1,2,3,4,5,6,7));

// iske neeche ka add krna hai
// $data['S13_2'] = $recordCounter1->countRecords1('S13_2', array(1,2,3,4,5,6,7));
// $data['S13_4'] = $recordCounter1->countRecords1('S13_4', array(1,2,3,4,5,6,7));
// $data['S13_5'] = $recordCounter1->countRecords1('S13_5', array(1,2,3,4,5,6,7));

// $data['S13_7'] = $recordCounter1->countRecords1('S13_7', array(1,2,3,4,5,6,7));
// $data['S13_9'] = $recordCounter1->countRecords1('S13_9', array(1,2,3,4,5,6,7));
// $data['S13_10'] = $recordCounter1->countRecords1('S13_10', array(1,2,3,4,5,6,7));
// $data['S13_11'] = $recordCounter1->countRecords1('S13_11', array(1,2,3,4,5,6,7));
// $data['S13_12'] = $recordCounter1->countRecords1('S13_12', array(1,2,3,4,5,6,7));
// $data['S13_13'] = $recordCounter1->countRecords1('S13_13', array(1,2,3,4,5,6,7));
// $data['S13_14'] = $recordCounter1->countRecords1('S13_14', array(1,2,3,4,5,6,7));
// $data['S13_15'] = $recordCounter1->countRecords1('S13_15', array(1,2,3,4,5,6,7));

echo json_encode($data);

?>