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

$data = array();
$QDP_arr = array();
$QDP1_arr = array();
$QDPOVERALL_arr = array();
$QDP_count = array(1,2,3,4,5,6);
//$QDP_count = array(6);
$QDPD_arr = array();
$QDPD_label_arr = array();
$QDPD_count = array(2,3,4,5,6,7);
$yes_no_array = array(1);
$QDP1D_count = array(1,2,3);
$QDP1D_arr = array();
$QDP1_label_arr = array();
$QDPOVERALL_label_arr = array();
$QDP1D_label_arr = array();
$QDP_b = '';
$QDP_m = '';
$QDP_p = '';
$QDP2_b = '';
$QDP2_m = '';
$QDP2_p = '';
$QDPD_b = '';
$QDPD_t = '';
$QDP1D_b = '';
$QDP1D_t = '';


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

// $condX = '';
// $conX = 1;
// $oemVal = 3;

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
{

}

// $brand_C = array("30"=>"NOVO");

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

            $QDP2_b = $QDP2_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QDP10` != "" then record end) as QDP10_b'.$key.',';
            $QDP2_m = $QDP2_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QDP10` != "" AND `QDP10` IN (1,2,3,4,5,6,7,8,9,10) then QDP10 end) as QDP10_m'.$key.',';
            $QDP2_p = $QDP2_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QDP10` IN (9,10) then record end) as QDP10_p'.$key.',';

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

            $QDP1D_b = $QDP1D_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QDP1` != "" then record end) as QDP1_b'.$key.',';
            foreach($QDP1D_count as $key4 => $value4)
            {
                $QDP1D_t = $QDP1D_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QDP1` = '.$value4.' then record end) as QDP1_'.$value4.'_t'.$key.',';
            }
        }
        else if($key==10)
        {
            foreach($QDP_count as $key2 => $value2)
            {
                $QDP_b = $QDP_b.' count(case when '.$column_name.' in (4,5) AND `T_QDP8_'.$value2.'` != "" then record end) as T_QDP8_'.$value2.'_b'.$key.',';
                $QDP_m = $QDP_m.' avg(case when '.$column_name.' in (4,5) AND `T_QDP8_'.$value2.'` != "" AND `T_QDP8_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QDP8_'.$value2.' end) as T_QDP8_'.$value2.'_m'.$key.',';
                $QDP_p = $QDP_p.' count(case when '.$column_name.' in (4,5) AND `T_QDP8_'.$value2.'` IN (9,10) then record end) as T_QDP8_'.$value2.'_p'.$key.',';
            }

            $QDP2_b = $QDP2_b.' count(case when '.$column_name.' in (4,5) AND `QDP10` != "" then record end) as QDP10_b'.$key.',';
            $QDP2_m = $QDP2_m.' avg(case when '.$column_name.' in (4,5) AND `QDP10` != "" AND `QDP10` IN (1,2,3,4,5,6,7,8,9,10) then QDP10 end) as QDP10_m'.$key.',';
            $QDP2_p = $QDP2_p.' count(case when '.$column_name.' in (4,5) AND `QDP10` IN (9,10) then record end) as QDP10_p'.$key.',';

            foreach($QDPD_count as $key3 => $value3)
            {
                $QDPD_b = $QDPD_b.' count(case when '.$column_name.' in (4,5) AND `QDP'.$value3.'` != "" then record end) as QDP'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QDPD_t = $QDPD_t.' count(case when '.$column_name.' in (4,5) AND `QDP'.$value3.'` = '.$value4.' then record end) as QDP'.$value3.'_'.$value4.'_t'.$key.',';
                }
            }

            $QDP1D_b = $QDP1D_b.' count(case when '.$column_name.' in (4,5) AND `QDP1` != "" then record end) as QDP1_b'.$key.',';
            foreach($QDP1D_count as $key4 => $value4)
            {
                $QDP1D_t = $QDP1D_t.' count(case when '.$column_name.' in (4,5) AND `QDP1` = '.$value4.' then record end) as QDP1_'.$value4.'_t'.$key.',';
            }
        }
        else
        {
            foreach($QDP_count as $key2 => $value2)
            {
                $QDP_b = $QDP_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QDP8_'.$value2.'` != "" then record end) as T_QDP8_'.$value2.'_b'.$key.',';
                $QDP_m = $QDP_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QDP8_'.$value2.'` != "" AND `T_QDP8_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QDP8_'.$value2.' end) as T_QDP8_'.$value2.'_m'.$key.',';
                $QDP_p = $QDP_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QDP8_'.$value2.'` IN (9,10) then record end) as T_QDP8_'.$value2.'_p'.$key.',';
            }

            $QDP2_b = $QDP2_b.' count(case when '.$column_name.' in ('.$key.') AND `QDP10` != "" then record end) as QDP10_b'.$key.',';
            $QDP2_m = $QDP2_m.' avg(case when '.$column_name.' in ('.$key.') AND `QDP10` != "" AND `QDP10` IN (1,2,3,4,5,6,7,8,9,10) then QDP10 end) as QDP10_m'.$key.',';
            $QDP2_p = $QDP2_p.' count(case when '.$column_name.' in ('.$key.') AND `QDP10` IN (9,10) then record end) as QDP10_p'.$key.',';

            foreach($QDPD_count as $key3 => $value3)
            {
                $QDPD_b = $QDPD_b.' count(case when '.$column_name.' in ('.$key.') AND `QDP'.$value3.'` != "" then record end) as QDP'.$value3.'_b'.$key.',';
                foreach($yes_no_array as $key4 => $value4)
                {
                    $QDPD_t = $QDPD_t.' count(case when '.$column_name.' in ('.$key.') AND `QDP'.$value3.'` = '.$value4.' then record end) as QDP'.$value3.'_'.$value4.'_t'.$key.',';
                }
            }

            $QDP1D_b = $QDP1D_b.' count(case when '.$column_name.' in ('.$key.') AND `QDP1` != "" then record end) as QDP1_b'.$key.',';
            foreach($QDP1D_count as $key4 => $value4)
            {
                $QDP1D_t = $QDP1D_t.' count(case when '.$column_name.' in ('.$key.') AND `QDP1` = '.$value4.' then record end) as QDP1_'.$value4.'_t'.$key.',';
            }
        }
    }
    else
    {
        foreach($QDP_count as $key2 => $value2)
        {
            $QDP_b = $QDP_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QDP8_'.$value2.'` != "" then record end) as T_QDP8_'.$value2.'_b'.$key.',';
            $QDP_m = $QDP_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QDP8_'.$value2.'` != "" AND `T_QDP8_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QDP8_'.$value2.' end) as T_QDP8_'.$value2.'_m'.$key.',';
            $QDP_p = $QDP_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QDP8_'.$value2.'` IN (9,10) then record end) as T_QDP8_'.$value2.'_p'.$key.',';
        }

        $QDP2_b = $QDP2_b.' count(case when '.$column_name.' in ('.$key.') AND `QDP10` != "" then record end) as QDP10_b'.$key.',';
        $QDP2_m = $QDP2_m.' avg(case when '.$column_name.' in ('.$key.') AND `QDP10` != "" AND `QDP10` IN (1,2,3,4,5,6,7,8,9,10) then QDP10 end) as QDP10_m'.$key.',';
        $QDP2_p = $QDP2_p.' count(case when '.$column_name.' in ('.$key.') AND `QDP10` IN (9,10) then record end) as QDP10_p'.$key.',';

        foreach($QDPD_count as $key3 => $value3)
        {
            $QDPD_b = $QDPD_b.' count(case when '.$column_name.' in ('.$key.') AND `QDP'.$value3.'` != "" then record end) as QDP'.$value3.'_b'.$key.',';
            foreach($yes_no_array as $key4 => $value4)
            {
                $QDPD_t = $QDPD_t.' count(case when '.$column_name.' in ('.$key.') AND `QDP'.$value3.'` = '.$value4.' then record end) as QDP'.$value3.'_'.$value4.'_t'.$key.',';
            }
        }

        $QDP1D_b = $QDP1D_b.' count(case when '.$column_name.' in ('.$key.') AND `QDP1` != "" then record end) as QDP1_b'.$key.',';
        foreach($QDP1D_count as $key4 => $value4)
        {
            $QDP1D_t = $QDP1D_t.' count(case when '.$column_name.' in ('.$key.') AND `QDP1` = '.$value4.' then record end) as QDP1_'.$value4.'_t'.$key.',';
        }
    }
    
}
$all = mysqli_query($conn,"SELECT $QDP_b $QDP_m $QDP_p $QDP2_b $QDP2_m $QDP2_p $QDPD_b $QDPD_t $QDP1D_b $QDP1D_t record FROM `cherry` WHERE $condX");
$ALL= mysqli_fetch_array($all);

foreach($QDP_count as $key2=> $value2)
{
    $QDP1_arrX = array();
    $QDP1_label_arrX = array();

    foreach($brand_C as $key => $value)
    {
        $QDP1_arr2 = array();
        $QDP_b = $ALL['T_QDP8_'.$value2.'_b'.''.$key.''];
        if($QDP_b==0)
        {
            $QDP_b=1;
        }
        if(is_numeric($ALL['T_QDP8_'.$value2.'_m'.''.$key.'']))
        {
            $QDP_m = round($ALL['T_QDP8_'.$value2.'_m'.''.$key.'']);
        }
        else
        {
            $QDP_m = 0;
        }
        $QDP_p2 = $ALL['T_QDP8_'.$value2.'_p'.''.$key.''];
        $QDP_p = round(($QDP_p2/$QDP_b)*100);
        array_push($QDP1_arr2,$QDP_m,$QDP_p,$QDP_b);
        array_push($QDP1_arrX,$QDP1_arr2);
        array_push($QDP1_label_arrX,$value);
    }
    if(sizeof($QDP1_arrX)!='0')
    {
        array_push($QDP1_arr,$QDP1_arrX);
        array_push($QDP1_label_arr,$QDP1_label_arrX);
    }
}

foreach($brand_C as $key => $value)
{
    $QDPOVERALL_arrX = array();
    $QDP2_b = $ALL['QDP10_b'.''.$key.''];
    if($QDP2_b==0)
    {
        $QDP2_b=1;
    }
    if(is_numeric($ALL['QDP10_m'.''.$key.'']))
    {
        $QDP2_m = round($ALL['QDP10_m'.''.$key.'']);
    }
    else
    {
        $QDP2_m = 0;
    }
    $QDP2_p2 = $ALL['QDP10_p'.''.$key.''];
    $QDP2_p = round(($QDP2_p2 / $QDP2_b ) * 100);
    array_push($QDPOVERALL_arrX,$QDP2_m,$QDP2_p,$QDP2_b);
    array_push($QDPOVERALL_arr,$QDPOVERALL_arrX);
    array_push($QDPOVERALL_label_arr,$value);
}
foreach($QDPD_count as $key3 => $value3)
{
    $QDPD_arr2 = array();
    $QDPD_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QDPD_arr3 = array();
        $QDPD_b = $ALL['QDP'.$value3.'_b'.''.$key.''];
        if($QDPD_b==0)
        {
            $QDPD_b=1;
        }
        foreach($yes_no_array as $key4 => $value4)
        {
            $QDPD_t2 = $ALL['QDP'.$value3.'_'.$value4.'_t'.''.$key.''];
            $QDPD_t = round(($QDPD_t2/$QDPD_b)*100);
            // if($QDPD_t!=0)
            // {
                array_push($QDPD_arr3,$QDPD_t);
                array_push($QDPD_label_arrX,$value);
            // }
        }
        if(sizeof($QDPD_arr3)!=0)
        {
            array_push($QDPD_arr2,$QDPD_arr3);
        }
    }
    if(sizeof($QDPD_arr2)!=0)
    {
        array_push($QDPD_arr,$QDPD_arr2);
        array_push($QDPD_label_arr,$QDPD_label_arrX);
    }
}

foreach($QDP1D_count as $key4 => $value4)
{
    $QDP1D_arr2 = array();
    $QDP1D_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QDP1D_b = $ALL['QDP1_b'.$key.''];
        if($QDP1D_b==0)
        {
            $QDP1D_b=1;
        }
        $QDP1D_t2 = $ALL['QDP1_'.$value4.'_t'.$key.''];
        $QDP1D_t = round(($QDP1D_t2 / $QDP1D_b) *100);
        array_push($QDP1D_arr2,$QDP1D_t);
        array_push($QDP1D_label_arrX,$value);
    }
    if(sizeof($QDP1D_arr2)!=0)
    {
        array_push($QDP1D_arr,$QDP1D_arr2);
        array_push($QDP1D_label_arr,$QDP1D_label_arrX);
    }
}

array_push($QDP_arr,$QDP1_arr);
array_push($QDP_arr,$QDPOVERALL_arr);
$data['QDP'] = $QDP_arr;
$data['QDPL'] = $QDP1_label_arr;
$data['QDPOVERALLL'] = $QDPOVERALL_label_arr;
$data['QDPD'] = $QDPD_arr;
$data['QDPDL'] = $QDPD_label_arr;
$data['QDP1D'] = $QDP1D_arr;
$data['QDP1DL'] = $QDP1D_label_arr;


echo json_encode($data);

?>