<?php
include '../config.php';

$data = array();
$QBD_arr = array();
$QBD1_arr = array();
$QBDOVERALL_arr = array();
$QBD_count = array(1,2,3,4);
$QBDY_arr = array();
$QBDY_count = array(1);
$QBDX_arr = array();
$QBDX_count = array(1,2);
$yes_no_array = array(1);
$QBDX4_count = array(1,2,3,4,5,6,7);
$QBDX4_arr = array();
$QBDX3_count = array(1,2,3);
$QBDX3_arr = array();
$QBD_label_arr = array();
$QBDOVERALL_label_arr = array();
$QBDY_label_arr = array();
$QBDX_label_arr = array();
$QBDX4_label_arr = array();
$QBDX3_label_arr = array();

$QBD_b = '';
$QBD_m = '';
$QBD_p = '';
$QBD5_b = '';
$QBD5_m = '';
$QBD5_p = '';
$QBDY_b = '';
$QBDY_t = '';
$QBDX_b = '';
$QBDX_t = '';
$QBDX4_b = '';
$QBDX4_t = '';
$QBDX3_b = '';
$QBDX3_t = '';


$condX = $_REQUEST['condX'];
$conX = $_REQUEST['conX'];
$oemVal = $_POST['oemVal'];

if($oemVal==0)
{
    $brand_C = $brand;
    $column_name = 'Q13b';
}
else
{
    $brand_C = $model[$oemVal-1];
    $column_name = 'Q13';
}

if($conX==1)
{    
    $condX="1=1";   
}
else
{

}
foreach($brand_C as $key=> $value)
{
    if($oemVal==0)
    {
        if($key==9)
        {
            foreach($QBD_count as $key2 => $value2)
            {
                $QBD_b = $QBD_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QBD1_'.$value2.'` != "" then record end) as T_QBD1_'.$value2.'_b'.$key.',';
                $QBD_m = $QBD_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.',';
                $QBD_p = $QBD_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QBD1_'.$value2.'` IN (9,10) then record end) as T_QBD1_'.$value2.'_p'.$key.',';
            }

            $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBD5` != "" then record end) as QBD5_b'.$key.',';
            $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) then QBD5 end) as QBD5_m'.$key.',';
            $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBD5` IN (9,10) then record end) as QBD5_p'.$key.','; 

            foreach($QBDY_count as $key3 => $value3)
            {
                $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDY'.$value3.'` != "" then record end) as QBDY'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDY'.$value3.'` = '.$value4.' then record end) as QBDY'.$value3.'_t'.$key.',';
                }
            }

            foreach($QBDX_count as $key3 => $value3)
            {
                $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX'.$value3.'` != "" then record end) as QBDX'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX'.$value3.'` = '.$value4.' then record end) as QBDX'.$value3.'_t'.$key.',';
                }
            }

            $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX4` != "" then record end) as QBDX4_b'.$key.',';
            foreach($QBDX4_count as $key4 => $value4)
            {
                $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX4` = '.$value4.' then record end) as QBDX4_'.$value4.'_t'.$key.',';
            }

            $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX3` != "" then record end) as QBDX3_b'.$key.',';
            foreach($QBDX3_count as $key4 => $value4)
            {
                $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QBDX3` = '.$value4.' then record end) as QBDX3_'.$value4.'_t'.$key.',';
            }
        }
        else if($key==10)
        {
            foreach($QBD_count as $key2 => $value2)
            {
                $QBD_b = $QBD_b.' count(case when '.$column_name.' in (4,5) AND `T_QBD1_'.$value2.'` != "" then record end) as T_QBD1_'.$value2.'_b'.$key.',';
                $QBD_m = $QBD_m.' avg(case when '.$column_name.' in (4,5) AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.',';
                $QBD_p = $QBD_p.' count(case when '.$column_name.' in (4,5) AND `T_QBD1_'.$value2.'` IN (9,10) then record end) as T_QBD1_'.$value2.'_p'.$key.',';
            }

            $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in (4,5) AND `QBD5` != "" then record end) as QBD5_b'.$key.',';
            $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in (4,5) AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) then QBD5 end) as QBD5_m'.$key.',';
            $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in (4,5) AND `QBD5` IN (9,10) then record end) as QBD5_p'.$key.','; 

            foreach($QBDY_count as $key3 => $value3)
            {
                $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in (4,5) AND `QBDY'.$value3.'` != "" then record end) as QBDY'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in (4,5) AND `QBDY'.$value3.'` = '.$value4.' then record end) as QBDY'.$value3.'_t'.$key.',';
                }
            }

            foreach($QBDX_count as $key3 => $value3)
            {
                $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in (4,5) AND `QBDX'.$value3.'` != "" then record end) as QBDX'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in (4,5) AND `QBDX'.$value3.'` = '.$value4.' then record end) as QBDX'.$value3.'_t'.$key.',';
                }
            }

            $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in (4,5) AND `QBDX4` != "" then record end) as QBDX4_b'.$key.',';
            foreach($QBDX4_count as $key4 => $value4)
            {
                $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in (4,5) AND `QBDX4` = '.$value4.' then record end) as QBDX4_'.$value4.'_t'.$key.',';
            }

            $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in (4,5) AND `QBDX3` != "" then record end) as QBDX3_b'.$key.',';
            foreach($QBDX3_count as $key4 => $value4)
            {
                $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in (4,5) AND `QBDX3` = '.$value4.' then record end) as QBDX3_'.$value4.'_t'.$key.',';
            }
        }
        else
        {
            foreach($QBD_count as $key2 => $value2)
            {
                $QBD_b = $QBD_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" then record end) as T_QBD1_'.$value2.'_b'.$key.',';
                $QBD_m = $QBD_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.',';
                $QBD_p = $QBD_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` IN (9,10) then record end) as T_QBD1_'.$value2.'_p'.$key.',';
            }

            $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" then record end) as QBD5_b'.$key.',';
            $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) then QBD5 end) as QBD5_m'.$key.',';
            $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` IN (9,10) then record end) as QBD5_p'.$key.','; 

            foreach($QBDY_count as $key3 => $value3)
            {
                $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` != "" then record end) as QBDY'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` = '.$value4.' then record end) as QBDY'.$value3.'_t'.$key.',';
                }
            }

            foreach($QBDX_count as $key3 => $value3)
            {
                $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` != "" then record end) as QBDX'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` = '.$value4.' then record end) as QBDX'.$value3.'_t'.$key.',';
                }
            }

            $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` != "" then record end) as QBDX4_b'.$key.',';
            foreach($QBDX4_count as $key4 => $value4)
            {
                $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` = '.$value4.' then record end) as QBDX4_'.$value4.'_t'.$key.',';
            }

            $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` != "" then record end) as QBDX3_b'.$key.',';
            foreach($QBDX3_count as $key4 => $value4)
            {
                $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` = '.$value4.' then record end) as QBDX3_'.$value4.'_t'.$key.',';
            }
        }
    }
    else
    {
        foreach($QBD_count as $key2 => $value2)
        {
            $QBD_b = $QBD_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" then record end) as T_QBD1_'.$value2.'_b'.$key.',';
            $QBD_m = $QBD_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` != "" AND `T_QBD1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QBD1_'.$value2.' end) as T_QBD1_'.$value2.'_m'.$key.',';
            $QBD_p = $QBD_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QBD1_'.$value2.'` IN (9,10) then record end) as T_QBD1_'.$value2.'_p'.$key.',';
        }

        $QBD5_b = $QBD5_b.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" then record end) as QBD5_b'.$key.',';
        $QBD5_m = $QBD5_m.' avg(case when '.$column_name.' in ('.$key.') AND `QBD5` != "" AND `QBD5` IN (1,2,3,4,5,6,7,8,9,10) then QBD5 end) as QBD5_m'.$key.',';
        $QBD5_p = $QBD5_p.' count(case when '.$column_name.' in ('.$key.') AND `QBD5` IN (9,10) then record end) as QBD5_p'.$key.','; 

        foreach($QBDY_count as $key3 => $value3)
        {
            $QBDY_b = $QBDY_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` != "" then record end) as QBDY'.$value3.'_b'.$key.',';
            foreach($yes_no_array as $key4 => $value4)
            {
                $QBDY_t = $QBDY_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDY'.$value3.'` = '.$value4.' then record end) as QBDY'.$value3.'_t'.$key.',';
            }
        }

        foreach($QBDX_count as $key3 => $value3)
        {
            $QBDX_b = $QBDX_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` != "" then record end) as QBDX'.$value3.'_b'.$key.',';
            foreach($yes_no_array as $key4 => $value4)
            {
                $QBDX_t = $QBDX_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX'.$value3.'` = '.$value4.' then record end) as QBDX'.$value3.'_t'.$key.',';
            }
        }

        $QBDX4_b = $QBDX4_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` != "" then record end) as QBDX4_b'.$key.',';
        foreach($QBDX4_count as $key4 => $value4)
        {
            $QBDX4_t = $QBDX4_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX4` = '.$value4.' then record end) as QBDX4_'.$value4.'_t'.$key.',';
        }

        $QBDX3_b = $QBDX3_b.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` != "" then record end) as QBDX3_b'.$key.',';
        foreach($QBDX3_count as $key4 => $value4)
        {
            $QBDX3_t = $QBDX3_t.' count(case when '.$column_name.' in ('.$key.') AND `QBDX3` = '.$value4.' then record end) as QBDX3_'.$value4.'_t'.$key.',';
        }
    }
    
}

$all = mysqli_query($conn,"SELECT $QBD_b $QBD_m $QBD_p $QBD5_b $QBD5_m $QBD5_p $QBDY_b $QBDY_t $QBDX_b $QBDX_t $QBDX4_b $QBDX4_t $QBDX3_b $QBDX3_t record FROM `cherry` WHERE $condX");
$ALL= mysqli_fetch_array($all);

foreach($QBD_count as $key2 => $value2)
{
    $QBD1_arrX = array();
    $QBD_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QBD1_arr2 = array();
        $QBD_b = $ALL['T_QBD1_'.$value2.'_b'.''.$key.''];
        if($QBD_b==0)
        {
            $QBD_b=1;
        }
        if(is_numeric($ALL['T_QBD1_'.$value2.'_m'.''.$key.'']))
        {
            $QBD_m = round($ALL['T_QBD1_'.$value2.'_m'.''.$key.'']);
        }
        else
        {
            $QBD_m = 0;
        }
        $QBD_p2 = $ALL['T_QBD1_'.$value2.'_p'.''.$key.''];
        $QBD_p = round(($QBD_p2 / $QBD_b ) * 100);
        array_push($QBD1_arr2,$QBD_m,$QBD_p,$QBD_b);
        array_push($QBD1_arrX,$QBD1_arr2);
        array_push($QBD_label_arrX,$value);
    }
    if(sizeof($QBD1_arrX)!=0)
    {
        array_push($QBD1_arr,$QBD1_arrX);
        array_push($QBD_label_arr,$QBD_label_arrX);
    }
}
foreach($brand_C as $key => $value)
{
    $QBDOVERALL_arrX = array();
    $QBD5_b = $ALL['QBD5_b'.''.$key.''];
    if($QBD5_b==0)
    {
        $QBD5_b=1;
    }
    if(is_numeric($ALL['QBD5_m'.''.$key.'']))
    {
        $QBD5_m = round($ALL['QBD5_m'.''.$key.'']);
    }
    else
    {
        $QBD5_m = 0;
    }
    $QBD5_p2 = $ALL['QBD5_p'.''.$key.''];
    $QBD5_p = round(($QBD5_p2 / $QBD5_b ) * 100);
    array_push($QBDOVERALL_arrX,$QBD5_m,$QBD5_p,$QBD5_b);
    array_push($QBDOVERALL_arr,$QBDOVERALL_arrX);
    array_push($QBDOVERALL_label_arr,$value);
}

foreach($QBDY_count as $key3 => $value3)
{
    $QBDY_arr2 = array();
    $QBDY_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QBDY_arr3 = array();
        $QBDY_b = $ALL['QBDY'.$value3.'_b'.''.$key.''];
        if($QBDY_b==0)
        {
            $QBDY_b=1;
        }
        foreach($yes_no_array as $key4 => $value4)
        {
            $QBDY_t2 = $ALL['QBDY'.$value3.'_t'.''.$key.''];
            $QBDY_t = round(($QBDY_t2/$QBDY_b)*100);
            // if($QBDY_t!=0)
            // {
                array_push($QBDY_arr3,$QBDY_t);
                array_push($QBDY_label_arrX,$value);
            // }
        }
        if(sizeof($QBDY_arr3)!=0)
        {
            array_push($QBDY_arr2,$QBDY_arr3);
        }
    }
    if(sizeof($QBDY_arr2)!=0)
    {
        array_push($QBDY_arr,$QBDY_arr2);
        array_push($QBDY_label_arr,$QBDY_label_arrX);
    }
}

foreach($QBDX_count as $key3 => $value3)
{
    $QBDX_arr2 = array();
    $QBDX_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QBDX_arr3 = array();
        $QBDX_b = $ALL['QBDX'.$value3.'_b'.''.$key.''];
        if($QBDX_b==0)
        {
            $QBDX_b=1;
        }
        foreach($yes_no_array as $key4 => $value4)
        {
            $QBDX_t2 = $ALL['QBDX'.$value3.'_t'.''.$key.''];
            $QBDX_t = round(($QBDX_t2/$QBDX_b)*100);
            // if($QBDX_t!=0)
            // {
                array_push($QBDX_arr3,$QBDX_t);
                array_push($QBDX_label_arrX,$value);
            // }
        }
        if(sizeof($QBDX_arr3)!=0)
        {
            array_push($QBDX_arr2,$QBDX_arr3);
        }
    }
    if(sizeof($QBDX_arr2)!=0)
    {
        array_push($QBDX_arr,$QBDX_arr2);
        array_push($QBDX_label_arr,$QBDX_label_arrX);
    }
}

foreach($QBDX4_count as $key4 => $value4)
{
    $QBDX4_arr2 = array();
    $QBDX4_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QBDX4_b = $ALL['QBDX4_b'.$key.''];
        if($QBDX4_b==0)
        {
            $QBDX4_b=1;
        }
        $QBDX4_t2 = $ALL['QBDX4_'.$value4.'_t'.$key.''];
        $QBDX4_t = round(($QBDX4_t2 / $QBDX4_b) *100);
        array_push($QBDX4_arr2,$QBDX4_t);
        array_push($QBDX4_label_arrX,$value);
    }
    if(sizeof($QBDX4_arr2)!=0)
    {
        array_push($QBDX4_arr,$QBDX4_arr2);
        array_push($QBDX4_label_arr,$QBDX4_label_arrX);
    }
}
    
foreach($QBDX3_count as $key4 => $value4)
{
    $QBDX3_arr2 = array();
    $QBDX3_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QBDX3_b = $ALL['QBDX3_b'.$key.''];
        if($QBDX3_b==0)
        {
            $QBDX3_b=1;
        }
        $QBDX3_t2 = $ALL['QBDX3_'.$value4.'_t'.$key.''];
        $QBDX3_t = round(($QBDX3_t2 / $QBDX3_b) *100);
        array_push($QBDX3_arr2,$QBDX3_t);
        array_push($QBDX3_label_arrX,$value);
    }
    if(sizeof($QBDX3_arr2)!=0)
    {
        array_push($QBDX3_arr,$QBDX3_arr2);
        array_push($QBDX3_label_arr,$QBDX3_label_arrX);
    }
}
array_push($QBD_arr,$QBD1_arr);
array_push($QBD_arr,$QBDOVERALL_arr);
$data['QBD'] = $QBD_arr;
$data['QBDL'] = $QBD_label_arr;
$data['QBDOVERALLL'] = $QBDOVERALL_label_arr;
$data['QBDY'] = $QBDY_arr;
$data['QBDYL'] = $QBDY_label_arr;
$data['QBDX'] = $QBDX_arr;
$data['QBDXL'] = $QBDX_label_arr;
$data['QBDX4'] = $QBDX4_arr;
$data['QBDX4L'] = $QBDX4_label_arr;
$data['QBDX3'] = $QBDX3_arr;
$data['QBDX3L'] = $QBDX3_label_arr;


echo json_encode($data);

?>