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

$data = array();
$QWP_arr = array();
$QWP1_arr = array();
$QWPOVERALL_arr = array();
$QWP_count = array(1,2);
$condX = $_REQUEST['condX'];
$conX = $_REQUEST['conX'];
$oemVal = $_POST['oemVal'];
$QWP1_label_arr = array();
$QWPOVERALL_label_arr = array();

$QWP_b = '';
$QWP_m = '';
$QWP_p = '';
$QWP2_b = '';
$QWP2_m = '';
$QWP2_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($QWP_count as $key2 => $value2)
            {
                $QWP_b = $QWP_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QWP1_'.$value2.'` != "" then record end) as T_QWP1_'.$value2.'_b'.$key.',';
                $QWP_m = $QWP_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QWP1_'.$value2.'` != "" AND `T_QWP1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QWP1_'.$value2.' end) as T_QWP1_'.$value2.'_m'.$key.',';
                $QWP_p = $QWP_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `T_QWP1_'.$value2.'` IN (9,10) then record end) as T_QWP1_'.$value2.'_p'.$key.',';
            }

            $QWP2_b = $QWP2_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QWP_2` != "" then record end) as QWP2_b'.$key.',';
            $QWP2_m = $QWP2_m.' avg(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QWP_2` != "" AND `QWP_2` IN (1,2,3,4,5,6,7,8,9,10) then QWP_2 end) as QWP2_m'.$key.',';
            $QWP2_p = $QWP2_p.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QWP_2` IN (9,10) then record end) as QWP2_p'.$key.',';
        }
        else if($key==10)
        {
            foreach($QWP_count as $key2 => $value2)
            {
                $QWP_b = $QWP_b.' count(case when '.$column_name.' in (4,5) AND `T_QWP1_'.$value2.'` != "" then record end) as T_QWP1_'.$value2.'_b'.$key.',';
                $QWP_m = $QWP_m.' avg(case when '.$column_name.' in (4,5) AND `T_QWP1_'.$value2.'` != "" AND `T_QWP1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QWP1_'.$value2.' end) as T_QWP1_'.$value2.'_m'.$key.',';
                $QWP_p = $QWP_p.' count(case when '.$column_name.' in (4,5) AND `T_QWP1_'.$value2.'` IN (9,10) then record end) as T_QWP1_'.$value2.'_p'.$key.',';
            }

            $QWP2_b = $QWP2_b.' count(case when '.$column_name.' in (4,5) AND `QWP_2` != "" then record end) as QWP2_b'.$key.',';
            $QWP2_m = $QWP2_m.' avg(case when '.$column_name.' in (4,5) AND `QWP_2` != "" AND `QWP_2` IN (1,2,3,4,5,6,7,8,9,10) then QWP_2 end) as QWP2_m'.$key.',';
            $QWP2_p = $QWP2_p.' count(case when '.$column_name.' in (4,5) AND `QWP_2` IN (9,10) then record end) as QWP2_p'.$key.',';
        }
        else
        {
            foreach($QWP_count as $key2 => $value2)
            {
                $QWP_b = $QWP_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QWP1_'.$value2.'` != "" then record end) as T_QWP1_'.$value2.'_b'.$key.',';
                $QWP_m = $QWP_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QWP1_'.$value2.'` != "" AND `T_QWP1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QWP1_'.$value2.' end) as T_QWP1_'.$value2.'_m'.$key.',';
                $QWP_p = $QWP_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QWP1_'.$value2.'` IN (9,10) then record end) as T_QWP1_'.$value2.'_p'.$key.',';
            }

            $QWP2_b = $QWP2_b.' count(case when '.$column_name.' in ('.$key.') AND `QWP_2` != "" then record end) as QWP2_b'.$key.',';
            $QWP2_m = $QWP2_m.' avg(case when '.$column_name.' in ('.$key.') AND `QWP_2` != "" AND `QWP_2` IN (1,2,3,4,5,6,7,8,9,10) then QWP_2 end) as QWP2_m'.$key.',';
            $QWP2_p = $QWP2_p.' count(case when '.$column_name.' in ('.$key.') AND `QWP_2` IN (9,10) then record end) as QWP2_p'.$key.',';
        }
    }
    else
    {
        foreach($QWP_count as $key2 => $value2)
        {
            $QWP_b = $QWP_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QWP1_'.$value2.'` != "" then record end) as T_QWP1_'.$value2.'_b'.$key.',';
            $QWP_m = $QWP_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QWP1_'.$value2.'` != "" AND `T_QWP1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QWP1_'.$value2.' end) as T_QWP1_'.$value2.'_m'.$key.',';
            $QWP_p = $QWP_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QWP1_'.$value2.'` IN (9,10) then record end) as T_QWP1_'.$value2.'_p'.$key.',';
        }

        $QWP2_b = $QWP2_b.' count(case when '.$column_name.' in ('.$key.') AND `QWP_2` != "" then record end) as QWP2_b'.$key.',';
        $QWP2_m = $QWP2_m.' avg(case when '.$column_name.' in ('.$key.') AND `QWP_2` != "" AND `QWP_2` IN (1,2,3,4,5,6,7,8,9,10) then QWP_2 end) as QWP2_m'.$key.',';
        $QWP2_p = $QWP2_p.' count(case when '.$column_name.' in ('.$key.') AND `QWP_2` IN (9,10) then record end) as QWP2_p'.$key.',';
    }
    
}
$all = mysqli_query($conn,"SELECT $QWP_b $QWP_m $QWP_p $QWP2_b $QWP2_m $QWP2_p record FROM `cherry` WHERE $condX");
$ALL= mysqli_fetch_array($all);

foreach($QWP_count as $key2 => $value2)
{
    $QWP1_arrX = array();
    $QWP1_label_arrX = array();
    foreach($brand_C as $key=> $value)
    {
        $QWP1_arr2 = array();
        $QWP_b = $ALL['T_QWP1_'.$value2.'_b'.''.$key.''];
        if($QWP_b==0)
        {
            $QWP_b=1;
        }
        if(is_numeric($ALL['T_QWP1_'.$value2.'_m'.''.$key.'']))
        {
            $QWP_m = round($ALL['T_QWP1_'.$value2.'_m'.''.$key.'']);
        }
        else
        {
            $QWP_m = 0;
        }
        
        $QWP_p2 = $ALL['T_QWP1_'.$value2.'_p'.''.$key.''];
        $QWP_p = round(($QWP_p2 / $QWP_b ) * 100);
        array_push($QWP1_arr2,$QWP_m,$QWP_p,$QWP_b);
        array_push($QWP1_arrX,$QWP1_arr2);
        array_push($QWP1_label_arrX,$value);
    }
    if(sizeof($QWP1_arrX)!=0)
    {
        array_push($QWP1_arr,$QWP1_arrX);
        array_push($QWP1_label_arr,$QWP1_label_arrX);
    }
}
foreach($brand_C as $key=> $value)
{
    $QWPOVERALL_arrX = array();
    $QWP2_b = $ALL['QWP2_b'.''.$key.''];
    if($QWP2_b==0)
    {
        $QWP2_b=1;
    }
    if(is_numeric($ALL['QWP2_m'.''.$key.'']))
    {
        $QWP2_m = round($ALL['QWP2_m'.''.$key.'']);
    }
    else
    {
        $QWP2_m = 0;
    }
    
    $QWP2_p2 = $ALL['QWP2_p'.''.$key.''];
    $QWP2_p = round(($QWP2_p2 / $QWP2_b ) * 100);
    array_push($QWPOVERALL_arrX,$QWP2_m,$QWP2_p,$QWP2_b);
    array_push($QWPOVERALL_arr,$QWPOVERALL_arrX);
    array_push($QWPOVERALL_label_arr,$value);
}

array_push($QWP_arr,$QWP1_arr);
array_push($QWP_arr,$QWPOVERALL_arr);
$data['QWP'] = $QWP_arr;
$data['QWPL'] = $QWP1_label_arr;
$data['QWPOVERALL'] = $QWPOVERALL_label_arr;


echo json_encode($data);

?>