<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);

$uid =$data['uid'];
$sl = $con->query("select count(uid) as idcount from orders where uid = '$uid' and (status = 'completed' or status = 'preparing' or status = 'processing')");
if($row = $sl->fetch_assoc())
{
$countval = $row['idcount'];
}

if($countval == 0)
{
    if($data['keyword'] != '')
    {
        
        $cid = $data['keyword'];
        
         $counter = $con->query("select * from product where pname like '%".$cid."%' and status=1");
        if($counter->num_rows != 0)
        {
        $query = $con->query("select * from product where pname like '%".$cid."%' and status=1");
        $result = array();
        
        while($row = $query->fetch_assoc())
        {
            
        $result['id'] = $row['id'];
        $result['product_name'] = $row['pname'];
        $result['product_image'] = $row['pimg'];
        $result['product_type'] = $row['ptype'];
    	$result['product_related_image'] = $row['prel'];
        $result['seller_name'] = $row['sname'];
        $result['short_desc'] = $row['psdesc'];
        $a = explode('$;',$row['pgms']);
        //print_r($a[0]);
        $ab = explode('$;',$row['pprice']);
        $k=array();
        for($i=0;$i<count($a);$i++)
        {
            $k[$i] = array("product_type"=>$a[$i],"product_price"=>$ab[$i]);
        }
        
        $result['price'] = $k;
        $result['stock'] = $row['stock'];
    	$result['discount'] = $row['discount'];
        $pp[] = $result;
        }
        $returnArr = array("data"=>$pp,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Product List Get successfully!");
        }
        else
        {
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Product List Not Found!");
        }
    echo json_encode($returnArr);
    }
    else
    {
    echo "dont touch";
    }
}
else if($countval == 1)
{
    if($data['keyword'] != '')
    {
        
        $cid = $data['keyword'];
        
         $counter = $con->query("select * from product where pname like '%".$cid."%' and status=1");
        if($counter->num_rows != 0)
        {
        $query = $con->query("select * from product where pname like '%".$cid."%' and status=1");
        $result = array();
        
        while($row = $query->fetch_assoc())
        {
            
        $result['id'] = $row['id'];
        $result['product_name'] = $row['pname'];
        $result['product_image'] = $row['pimg'];
        $result['product_type'] = $row['ptype'];
    	$result['product_related_image'] = $row['prel'];
        $result['seller_name'] = $row['sname'];
        $result['short_desc'] = $row['psdesc'];
        $a = explode('$;',$row['pgms']);
        //print_r($a[0]);
        $ab = explode('$;',$row['pprice']);
        $k=array();
        for($i=0;$i<count($a);$i++)
        {
            $k[$i] = array("product_type"=>$a[$i],"product_price"=>$ab[$i]);
        }
        
        $result['price'] = $k;
        $result['stock'] = $row['stock'];
    	$result['discount'] =$row['discount'];
        $pp[] = $result;
        }
        $returnArr = array("data"=>$pp,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Product List Get successfully!");
        }
        else
        {
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Product List Not Found!");
        }
    echo json_encode($returnArr);
    }
    else
    {
    echo "dont touch";
    }
}
else
{
    $dis = $con->query("select dis_regular from setting where id = '1'");
    if($row = $dis->fetch_assoc())
    {
        $dis_val = $row['dis_regular'];
    }
    
    if($dis_val != 0)
    {
        if($data['keyword'] != '')
        {
            
            $cid = $data['keyword'];
            
             $counter = $con->query("select * from product where pname like '%".$cid."%' and status=1");
            if($counter->num_rows != 0)
            {
            $query = $con->query("select * from product where pname like '%".$cid."%' and status=1");
            $result = array();
            
            while($row = $query->fetch_assoc())
            {
                
            $result['id'] = $row['id'];
            $result['product_name'] = $row['pname'];
            $result['product_image'] = $row['pimg'];
            $result['product_type'] = $row['ptype'];
        	$result['product_related_image'] = $row['prel'];
            $result['seller_name'] = $row['sname'];
            $result['short_desc'] = $row['psdesc'];
            $a = explode('$;',$row['pgms']);
            //print_r($a[0]);
            $ab = explode('$;',$row['pprice']);
            $k=array();
            for($i=0;$i<count($a);$i++)
            {
                $k[$i] = array("product_type"=>$a[$i],"product_price"=>$ab[$i]);
            }
            
            $result['price'] = $k;
            $result['stock'] = $row['stock'];
        	$result['discount'] = $dis_val;
            $pp[] = $result;
            }
            $returnArr = array("data"=>$pp,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Product List Get successfully!");
            }
            else
            {
                $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Product List Not Found!");
            }
        echo json_encode($returnArr);
        }
        else
        {
        echo "dont touch";
        }
    }
    else
    {
        if($data['keyword'] != '')
        {
            
            $cid = $data['keyword'];
            
             $counter = $con->query("select * from product where pname like '%".$cid."%' and status=1");
            if($counter->num_rows != 0)
            {
            $query = $con->query("select * from product where pname like '%".$cid."%' and status=1");
            $result = array();
            
            while($row = $query->fetch_assoc())
            {
                
            $result['id'] = $row['id'];
            $result['product_name'] = $row['pname'];
            $result['product_image'] = $row['pimg'];
            $result['product_type'] = $row['ptype'];
        	$result['product_related_image'] = $row['prel'];
            $result['seller_name'] = $row['sname'];
            $result['short_desc'] = $row['psdesc'];
            $a = explode('$;',$row['pgms']);
            //print_r($a[0]);
            $ab = explode('$;',$row['pprice']);
            $k=array();
            for($i=0;$i<count($a);$i++)
            {
                $k[$i] = array("product_type"=>$a[$i],"product_price"=>$ab[$i]);
            }
            
            $result['price'] = $k;
            $result['stock'] = $row['stock'];
        	$result['discount'] = $row['discount'];
            $pp[] = $result;
            }
            $returnArr = array("data"=>$pp,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Product List Get successfully!");
            }
            else
            {
                $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Product List Not Found!");
            }
        echo json_encode($returnArr);
        }
        else
        {
        echo "dont touch";
        }
    }

}

?>