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

$data = array();
$QSL_arr = array();
$QSL1_arr = array();
$QSLOVERALL_arr = array();
$QSL_count = array(1,2,3,4,5);
$QSIX2_arr = array();
$QSIX2_count = array(2);
$QSIX3_arr = array();
$QSIX3_count = array(1,2);
$yes_no_array = array(1);
$yes_no_array2 = array(2);
$QSIX1_count = array(1,2,3);
$QSIX1_arr = array();
$QSIX1_label_arr = array();
$QSI_label_arr = array();
$QSIOVERALL_label_arr = array();
$QSI2_count = array(1,2,3,4,5,6);
$QSI2_arr = array();
$QSI2_label_arr = array();
$QSIX2_label_arr = array();
$QSIX3_label_arr = array();
$QSI_b = '';
$QSI_m = '';
$QSI_p = '';
$QSI3_b = '';
$QSI3_m = '';
$QSI3_p = '';
$QSIX2_b = '';
$QSIX2_t = '';
$QSIX3_b = '';
$QSIX3_t = '';
$QSIX1_b = '';
$QSIX1_t = '';
$QSI2_b = '';
$QSI2_t = '';
$condX = $_REQUEST['condX'];
$conX = $_REQUEST['conX'];
$oemVal = $_POST['oemVal'];

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

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

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

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

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

            $QSIX1_b = $QSIX1_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QSIX1` != "" then record end) as QSIX1_b'.$key.',';
            foreach($QSIX1_count as $key4 => $value4)
            {
                $QSIX1_t = $QSIX1_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QSIX1` = '.$value4.' then record end) as QSIX1_'.$value4.'_t'.$key.',';
            }

            $QSI2_b = $QSI2_b.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QSI2` != "" then record end) as QSI2_b'.$key.',';
            foreach($QSI2_count as $key4 => $value4)
            {
                $QSI2_t = $QSI2_t.' count(case when '.$column_name.' in (1,2,3,4,5,6,7,8) AND `QSI2` = '.$value4.' then record end) as QSI2_'.$value4.'_t'.$key.',';
            }
        }
        else if($key==10)
        {
            foreach($QSL_count as $key2 => $value2)
            {
                $QSI_b = $QSI_b.' count(case when '.$column_name.' in (4,5) AND `T_QSI1_'.$value2.'` != "" then record end) as T_QSI1_'.$value2.'_b'.$key.',';
                $QSI_m = $QSI_m.' avg(case when '.$column_name.' in (4,5) AND `T_QSI1_'.$value2.'` != "" AND `T_QSI1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QSI1_'.$value2.' end) as T_QSI1_'.$value2.'_m'.$key.',';
                $QSI_p = $QSI_p.' count(case when '.$column_name.' in (4,5) AND `T_QSI1_'.$value2.'` IN (9,10) then record end) as T_QSI1_'.$value2.'_p'.$key.',';
            }

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

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

            foreach($QSIX3_count as $key3 => $value3)
            {
                $QSIX3_b = $QSIX3_b.' count(case when '.$column_name.' in (4,5) AND `T_QSIX3_'.$value3.'` != "" then record end) as T_QSIX3_'.$value3.'_b'.$key.',';
                foreach($yes_no_array2 as $key4 => $value4)
                {
                    $QSIX3_t = $QSIX3_t.' count(case when '.$column_name.' in (4,5) AND `T_QSIX3_'.$value3.'` = '.$value4.' then record end) as T_QSIX3_'.$value3.'_'.$value4.'_t'.$key.',';
                }
            }

            $QSIX1_b = $QSIX1_b.' count(case when '.$column_name.' in (4,5) AND `QSIX1` != "" then record end) as QSIX1_b'.$key.',';
            foreach($QSIX1_count as $key4 => $value4)
            {
                $QSIX1_t = $QSIX1_t.' count(case when '.$column_name.' in (4,5) AND `QSIX1` = '.$value4.' then record end) as QSIX1_'.$value4.'_t'.$key.',';
            }

            $QSI2_b = $QSI2_b.' count(case when '.$column_name.' in (4,5) AND `QSI2` != "" then record end) as QSI2_b'.$key.',';
            foreach($QSI2_count as $key4 => $value4)
            {
                $QSI2_t = $QSI2_t.' count(case when '.$column_name.' in (4,5) AND `QSI2` = '.$value4.' then record end) as QSI2_'.$value4.'_t'.$key.',';
            }
        }
        else
        {
            foreach($QSL_count as $key2 => $value2)
            {
                $QSI_b = $QSI_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QSI1_'.$value2.'` != "" then record end) as T_QSI1_'.$value2.'_b'.$key.',';
                $QSI_m = $QSI_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QSI1_'.$value2.'` != "" AND `T_QSI1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QSI1_'.$value2.' end) as T_QSI1_'.$value2.'_m'.$key.',';
                $QSI_p = $QSI_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QSI1_'.$value2.'` IN (9,10) then record end) as T_QSI1_'.$value2.'_p'.$key.',';
            }

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

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

            foreach($QSIX3_count as $key3 => $value3)
            {
                $QSIX3_b = $QSIX3_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QSIX3_'.$value3.'` != "" then record end) as T_QSIX3_'.$value3.'_b'.$key.',';
                foreach($yes_no_array2 as $key4 => $value4)
                {
                    $QSIX3_t = $QSIX3_t.' count(case when '.$column_name.' in ('.$key.') AND `T_QSIX3_'.$value3.'` = '.$value4.' then record end) as T_QSIX3_'.$value3.'_'.$value4.'_t'.$key.',';
                }
            }

            $QSIX1_b = $QSIX1_b.' count(case when '.$column_name.' in ('.$key.') AND `QSIX1` != "" then record end) as QSIX1_b'.$key.',';
            foreach($QSIX1_count as $key4 => $value4)
            {
                $QSIX1_t = $QSIX1_t.' count(case when '.$column_name.' in ('.$key.') AND `QSIX1` = '.$value4.' then record end) as QSIX1_'.$value4.'_t'.$key.',';
            }

            $QSI2_b = $QSI2_b.' count(case when '.$column_name.' in ('.$key.') AND `QSI2` != "" then record end) as QSI2_b'.$key.',';
            foreach($QSI2_count as $key4 => $value4)
            {
                $QSI2_t = $QSI2_t.' count(case when '.$column_name.' in ('.$key.') AND `QSI2` = '.$value4.' then record end) as QSI2_'.$value4.'_t'.$key.',';
            }
        }
    }
    else
    {
        foreach($QSL_count as $key2 => $value2)
        {
            $QSI_b = $QSI_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QSI1_'.$value2.'` != "" then record end) as T_QSI1_'.$value2.'_b'.$key.',';
            $QSI_m = $QSI_m.' avg(case when '.$column_name.' in ('.$key.') AND `T_QSI1_'.$value2.'` != "" AND `T_QSI1_'.$value2.'` IN (1,2,3,4,5,6,7,8,9,10) then T_QSI1_'.$value2.' end) as T_QSI1_'.$value2.'_m'.$key.',';
            $QSI_p = $QSI_p.' count(case when '.$column_name.' in ('.$key.') AND `T_QSI1_'.$value2.'` IN (9,10) then record end) as T_QSI1_'.$value2.'_p'.$key.',';
        }

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

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

        foreach($QSIX3_count as $key3 => $value3)
        {
            $QSIX3_b = $QSIX3_b.' count(case when '.$column_name.' in ('.$key.') AND `T_QSIX3_'.$value3.'` != "" then record end) as T_QSIX3_'.$value3.'_b'.$key.',';
            foreach($yes_no_array2 as $key4 => $value4)
            {
                $QSIX3_t = $QSIX3_t.' count(case when '.$column_name.' in ('.$key.') AND `T_QSIX3_'.$value3.'` = '.$value4.' then record end) as T_QSIX3_'.$value3.'_'.$value4.'_t'.$key.',';
            }
        }

        $QSIX1_b = $QSIX1_b.' count(case when '.$column_name.' in ('.$key.') AND `QSIX1` != "" then record end) as QSIX1_b'.$key.',';
        foreach($QSIX1_count as $key4 => $value4)
        {
            $QSIX1_t = $QSIX1_t.' count(case when '.$column_name.' in ('.$key.') AND `QSIX1` = '.$value4.' then record end) as QSIX1_'.$value4.'_t'.$key.',';
        }

        $QSI2_b = $QSI2_b.' count(case when '.$column_name.' in ('.$key.') AND `QSI2` != "" then record end) as QSI2_b'.$key.',';
        foreach($QSI2_count as $key4 => $value4)
        {
            $QSI2_t = $QSI2_t.' count(case when '.$column_name.' in ('.$key.') AND `QSI2` = '.$value4.' then record end) as QSI2_'.$value4.'_t'.$key.',';
        }
    }
    
    
}
$all = mysqli_query($conn,"SELECT $QSI_b $QSI_m $QSI_p $QSI3_b $QSI3_m $QSI3_p $QSIX2_b $QSIX2_t $QSIX3_b $QSIX3_t $QSIX1_b $QSIX1_t $QSI2_b $QSI2_t record FROM `cherry` WHERE $condX");

$ALL= mysqli_fetch_array($all);

foreach($QSL_count as $key2 => $value2)
{
    $QSL1_arrX = array();
    $QSI_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QSL1_arr2 = array();
        $QSI_b = $ALL['T_QSI1_'.$value2.'_b'.''.$key.''];
        if($QSI_b==0)
        {
            $QSI_b =1;
        }
        if(is_numeric($ALL['T_QSI1_'.$value2.'_m'.''.$key.'']))
        {
            $QSI_m = round($ALL['T_QSI1_'.$value2.'_m'.''.$key.'']);
        }
        else
        {
            $QSI_m = 0;
        }
        $QSI_p2 = $ALL['T_QSI1_'.$value2.'_p'.''.$key.''];
        $QSI_p = round(($QSI_p2 / $QSI_b ) * 100);
        array_push($QSL1_arr2,$QSI_m,$QSI_p,$QSI_b);
        array_push($QSL1_arrX,$QSL1_arr2);
        array_push($QSI_label_arrX,$value);
        array_push($QSL1_arr2,$QSI_b);
    }
    if(sizeof($QSL1_arrX)!=0)
    {
        array_push($QSL1_arr,$QSL1_arrX);
        array_push($QSI_label_arr,$QSI_label_arrX);
    }
}

foreach($brand_C as $key => $value)
{
    $QSLOVERALL_arrX = array();
    $QSI3_b = $ALL['QSI3_b'.''.$key.''];
    if($QSI3_b==0)
    {
        $QSI3_b=1;
    }
    if(is_numeric($ALL['QSI3_m'.''.$key.'']))
    {
        $QSI3_m = round($ALL['QSI3_m'.''.$key.'']);
    }
    else
    {
        $QSI3_m = 0;
    }
    $QSI3_p2 = $ALL['QSI3_p'.''.$key.''];
    $QSI3_p = round(($QSI3_p2 / $QSI3_b ) * 100);
    array_push($QSLOVERALL_arrX,$QSI3_m,$QSI3_p,$QSI3_b);
    array_push($QSLOVERALL_arr,$QSLOVERALL_arrX);
    array_push($QSIOVERALL_label_arr,$value);
}


foreach($QSIX2_count as $key3 => $value3)
{
    $QSIX2_arr2 = array();
    $QSIX2_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QSIX2_arr3 = array();
        $QSIX2_b = $ALL['QSIX'.$value3.'_b'.''.$key.''];
        if($QSIX2_b==0)
        {
            $QSIX2_b=1;
        }
        foreach($yes_no_array as $key4 => $value4)
        {
            $QSIX2_t2 = $ALL['QSIX'.$value3.'_'.$value4.'_t'.''.$key.''];
            $QSIX2_t = round(($QSIX2_t2/$QSIX2_b)*100);
            // if($QSIX2_t!=0)
            // {
                array_push($QSIX2_arr3,$QSIX2_t);
                array_push($QSIX2_label_arrX,$value); 
            // }
        } 
        if(sizeof($QSIX2_arr3)!=0)
        {
            array_push($QSIX2_arr2,$QSIX2_arr3);
        }
    }
    if(sizeof($QSIX2_arr2)!=0)
    {
        array_push($QSIX2_arr,$QSIX2_arr2);
        array_push($QSIX2_label_arr,$QSIX2_label_arrX);
    }
}


foreach($QSIX3_count as $key3 => $value3)
{
    $QSIX3_arr2 = array();
    $QSIX3_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QSIX3_arr3 = array();
        $QSIX3_b = $ALL['T_QSIX3_'.$value3.'_b'.''.$key.''];
        if($QSIX3_b==0)
        {
            $QSIX3_b=1;
        }
        foreach($yes_no_array2 as $key4 => $value4)
        {
            $QSIX3_t2 = $ALL['T_QSIX3_'.$value3.'_'.$value4.'_t'.$key.''];
            $QSIX3_t = round(($QSIX3_t2/$QSIX3_b)*100);
            // if($QSIX3_t!=0)
            // {
                array_push($QSIX3_arr3,$QSIX3_t);
                array_push($QSIX3_label_arrX,$value); 
            // }
        }
        if(sizeof($QSIX3_arr3)!=0)
        {
            array_push($QSIX3_arr2,$QSIX3_arr3);
        }
    }
    if(sizeof($QSIX3_arr2)!=0)
    {
        array_push($QSIX3_arr,$QSIX3_arr2);
        array_push($QSIX3_label_arr,$QSIX3_label_arrX);
    }
}

foreach($QSIX1_count as $key4 => $value4)
{
    $QSIX1_arr2 = array();
    $QSIX1_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QSIX1_b = $ALL['QSIX1_b'.$key.''];
        if($QSIX1_b==0)
        {
            $QSIX1_b=1;
        }
        $QSIX1_t2 = $ALL['QSIX1_'.$value4.'_t'.$key.''];
        $QSIX1_t = round(($QSIX1_t2 / $QSIX1_b) *100);
        array_push($QSIX1_arr2,$QSIX1_t);
        array_push($QSIX1_label_arrX,$value);
    }
    if(sizeof($QSIX1_arr2)!=0)
    {
        array_push($QSIX1_arr,$QSIX1_arr2);
        array_push($QSIX1_label_arr,$QSIX1_label_arrX);
    }
}

foreach($QSI2_count as $key4 => $value4)
{
    $QSI2_arr2 = array();
    $QSI2_label_arrX = array();
    foreach($brand_C as $key => $value)
    {
        $QSI2_b = $ALL['QSI2_b'.$key.''];
        if($QSI2_b==0)
        {
            $QSI2_b=1;
        }
        $QSI2_t2 = $ALL['QSI2_'.$value4.'_t'.$key.''];
        $QSI2_t = round(($QSI2_t2 / $QSI2_b) *100);
        array_push($QSI2_arr2,$QSI2_t);
        array_push($QSI2_label_arrX,$value);
    }
    if(sizeof($QSI2_arr2)!=0)
    {
        array_push($QSI2_arr,$QSI2_arr2);
        array_push($QSI2_label_arr,$QSI2_label_arrX);
    }
}



array_push($QSL_arr,$QSL1_arr);
array_push($QSL_arr,$QSLOVERALL_arr);

$data['QSI'] = $QSL_arr;
$data['QSIL'] = $QSI_label_arr;
$data['QSIOVERALLL'] = $QSIOVERALL_label_arr;
$data['QSIX2'] = $QSIX2_arr;
$data['QSIX2L'] = $QSIX2_label_arr;
$data['QSIX3'] = $QSIX3_arr;
$data['QSIX3L'] = $QSIX3_label_arr;
$data['QSIX1'] = $QSIX1_arr;
$data['QSIX1L'] = $QSIX1_label_arr;
$data['QSI2'] = $QSI2_arr;
$data['QSI2L'] = $QSI2_label_arr;



echo json_encode($data);

?>