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

$data = array();
$QPJ_arr = array();
$QPJ1_arr = array();
$QPJOVERALL_arr = array();
$QPJ1_label_arr = array();
$QPJOVERALL_label_arr = array();
$QPJ_count = array(1,2,3,4);

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

$QPJ_b = '';
$QPJ_m = '';
$QPJ_p = '';

$QPJ5_b = '';
$QPJ5_m = '';
$QPJ5_p = '';

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($QPJ_count as $key2 => $value2)
            {
                $QPJ_b = $QPJ_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QPJ1_'.$value2.'` != "" then record end) as T_QPJ1_'.$value2.'_b'.$key.',';
                $QPJ_m = $QPJ_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QPJ1_'.$value2.'` != "" AND `T_QPJ1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QPJ1_'.$value2.' end) as T_QPJ1_'.$value2.'_m'.$key.',';
                $QPJ_p = $QPJ_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QPJ1_'.$value2.'` IN (9,10) then record end) as T_QPJ1_'.$value2.'_p'.$key.',';
            }

            $QPJ5_b = $QPJ5_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QPJ5` != "" then record end) as QPJ5_b'.$key.',';
            $QPJ5_m = $QPJ5_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QPJ5` AND `QPJ5` IN (1,2,3,4,5,6,7,8,9,10) != "" then QPJ5 end) as QPJ5_m'.$key.',';
            $QPJ5_p = $QPJ5_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QPJ5` IN (9,10) then record end) as QPJ5_p'.$key.',';
        }
        else if($key==10)
        {
            foreach($QPJ_count as $key2 => $value2)
            {
                $QPJ_b = $QPJ_b.' count(case when '.$column_name.' in (4,5) AND `T_QPJ1_'.$value2.'` != "" then record end) as T_QPJ1_'.$value2.'_b'.$key.',';
                $QPJ_m = $QPJ_m.' avg(case when '.$column_name.' in (4,5) AND `T_QPJ1_'.$value2.'` != "" AND `T_QPJ1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QPJ1_'.$value2.' end) as T_QPJ1_'.$value2.'_m'.$key.',';
                $QPJ_p = $QPJ_p.' count(case when '.$column_name.' in (4,5) AND `T_QPJ1_'.$value2.'` IN (9,10) then record end) as T_QPJ1_'.$value2.'_p'.$key.',';
            }

            $QPJ5_b = $QPJ5_b.' count(case when '.$column_name.' in (4,5) AND `QPJ5` != "" then record end) as QPJ5_b'.$key.',';
            $QPJ5_m = $QPJ5_m.' avg(case when '.$column_name.' in (4,5) AND `QPJ5` AND `QPJ5` IN (1,2,3,4,5,6,7,8,9,10) != "" then QPJ5 end) as QPJ5_m'.$key.',';
            $QPJ5_p = $QPJ5_p.' count(case when '.$column_name.' in (4,5) AND `QPJ5` IN (9,10) then record end) as QPJ5_p'.$key.',';
        }
        else
        {
            foreach($QPJ_count as $key2 => $value2)
            {
                $QPJ_b = $QPJ_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QPJ1_'.$value2.'` != "" then record end) as T_QPJ1_'.$value2.'_b'.$key.',';
                $QPJ_m = $QPJ_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QPJ1_'.$value2.'` != "" AND `T_QPJ1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QPJ1_'.$value2.' end) as T_QPJ1_'.$value2.'_m'.$key.',';
                $QPJ_p = $QPJ_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QPJ1_'.$value2.'` IN (9,10) then record end) as T_QPJ1_'.$value2.'_p'.$key.',';
            }

            $QPJ5_b = $QPJ5_b.' count(case when '.$column_name.' in ('.$key.') AND `QPJ5` != "" then record end) as QPJ5_b'.$key.',';
            $QPJ5_m = $QPJ5_m.' avg(case when '.$column_name.' in ('.$key.') AND `QPJ5` AND `QPJ5` IN (1,2,3,4,5,6,7,8,9,10) != "" then QPJ5 end) as QPJ5_m'.$key.',';
            $QPJ5_p = $QPJ5_p.' count(case when '.$column_name.' in ('.$key.') AND `QPJ5` IN (9,10) then record end) as QPJ5_p'.$key.',';
        }
    }
    else
    {
        foreach($QPJ_count as $key2 => $value2)
        {
            $QPJ_b = $QPJ_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QPJ1_'.$value2.'` != "" then record end) as T_QPJ1_'.$value2.'_b'.$key.',';
            $QPJ_m = $QPJ_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QPJ1_'.$value2.'` != "" AND `T_QPJ1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QPJ1_'.$value2.' end) as T_QPJ1_'.$value2.'_m'.$key.',';
            $QPJ_p = $QPJ_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QPJ1_'.$value2.'` IN (9,10) then record end) as T_QPJ1_'.$value2.'_p'.$key.',';
        }

        $QPJ5_b = $QPJ5_b.' count(case when '.$column_name.' in ('.$key.') AND `QPJ5` != "" then record end) as QPJ5_b'.$key.',';
        $QPJ5_m = $QPJ5_m.' avg(case when '.$column_name.' in ('.$key.') AND `QPJ5` AND `QPJ5` IN (1,2,3,4,5,6,7,8,9,10) != "" then QPJ5 end) as QPJ5_m'.$key.',';
        $QPJ5_p = $QPJ5_p.' count(case when '.$column_name.' in ('.$key.') AND `QPJ5` IN (9,10) then record end) as QPJ5_p'.$key.',';
    }
    
}

$all = mysqli_query($conn,"SELECT $QPJ_b $QPJ_m $QPJ_p $QPJ5_b $QPJ5_m $QPJ5_p record FROM `cherry` WHERE $condX");
$ALL= mysqli_fetch_array($all);

foreach($QPJ_count as $key2 => $value2)
{
    $QPJ1_arrX = array();
    $QPJ1_label_arrX = array();
    foreach($brand_C as $key=> $value)
    {
        $QPJ1_arr2 = array();
        $QPJ_b = $ALL['T_QPJ1_'.$value2.'_b'.$key.''];
        if($QPJ_b==0)
        {
            $QPJ_b=1;
        }
        if(is_numeric($ALL['T_QPJ1_'.$value2.'_m'.$key.'']))
        {
            $QPJ_m = round($ALL['T_QPJ1_'.$value2.'_m'.$key.'']);
        }
        else
        {
            $QPJ_m = 0;
        }
        $QPJ_p2 = $ALL['T_QPJ1_'.$value2.'_p'.$key.''];
        $QPJ_p = round(($QPJ_p2 / $QPJ_b ) * 100);
        array_push($QPJ1_arr2,$QPJ_m,$QPJ_p,$QPJ_b);
        array_push($QPJ1_arrX,$QPJ1_arr2);
        array_push($QPJ1_label_arrX,$value);
    }
    if(sizeof($QPJ1_arrX)!=0)
    {
        array_push($QPJ1_arr,$QPJ1_arrX);
        array_push($QPJ1_label_arr,$QPJ1_label_arrX);
    }
}

foreach($brand_C as $key => $value)
{
    $QPJOVERALL_arrX = array();
    $QPJ5_b = $ALL['QPJ5_b'.''.$key.''];
    if($QPJ5_b==0)
    {
        $QPJ5_b=1;
    }
    if(is_numeric($ALL['QPJ5_m'.''.$key.'']))
    {
        $QPJ5_m = round($ALL['QPJ5_m'.''.$key.'']);
    }
    else
    {
        $QPJ5_m = 0;
    }
    
    $QPJ5_p2 = $ALL['QPJ5_p'.''.$key.''];
    $QPJ5_p = round(($QPJ5_p2 / $QPJ5_b ) * 100);
    array_push($QPJOVERALL_arrX,$QPJ5_m,$QPJ5_p,$QPJ5_b);
    array_push($QPJOVERALL_arr,$QPJOVERALL_arrX);
    array_push($QPJOVERALL_label_arr,$value);
}

array_push($QPJ_arr,$QPJ1_arr);
array_push($QPJ_arr,$QPJOVERALL_arr);
$data['QPJ'] = $QPJ_arr;
$data['QPJL'] = $QPJ1_label_arr;
$data['QPJOVERALLL'] = $QPJOVERALL_label_arr;


echo json_encode($data);

?>