<?php 
require 'db.php';
header('Content-type: text/json');
$data = json_decode(file_get_contents('php://input'), true);
$current_time = date("H:i");
$uid =$data['uid'];
$shop_id = strip_tags(mysqli_real_escape_string($con,$data['shop_id']));
$shop_code = strip_tags(mysqli_real_escape_string($con,$data['shop_code']));

$cid = $data['cid'];
$sid = $data['sid'];

if($data['cid'] != 0 and $data['sid'] != 0)
{
	$counter = $con->query("SELECT product.*, shop_settings.start_time, shop_settings.end_time 
    FROM product INNER JOIN shop_settings on product.protime != '' and product.shop_id = shop_settings.shop_id
    where product.cid=".$cid." and product.sid=".$sid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
    and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
    or product.cid=".$cid." and product.sid=".$sid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
    TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
    and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time')");
    
    // $counter = $con->query("select * from product where cid=".$cid." and sid=".$sid." and TIME_TO_SEC(prostarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(prostoptime) < TIME_TO_SEC('$current_time') and status=1");
    if($counter->num_rows != 0)
    {
        $query = $con->query("SELECT product.*, shop_settings.start_time, shop_settings.end_time 
        FROM product 
        INNER JOIN shop_settings on product.protime != '' and product.shop_id = shop_settings.shop_id
        where product.cid=".$cid." and product.sid=".$sid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
        and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
        or product.cid=".$cid." and product.sid=".$sid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
        and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time') Order by product.stock DESC , product.pname ASC");
        
    // $query = $con->query("select * from product where cid=".$cid." and sid=".$sid." and TIME_TO_SEC(prostarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(prostoptime) < TIME_TO_SEC('$current_time') and status=1 Order by stock DESC , pname ASC");
    
    $result = array();
    $pp=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['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"=>"No Products!");
    }
echo json_encode($returnArr);
}
else if($data['cid'] == 0 and $data['sid'] == 0)
{
    $counter = $con->query("SELECT product.*, shop_settings.start_time, shop_settings.end_time 
        FROM product INNER JOIN shop_settings on product.protime != '' and product.shop_id = shop_settings.shop_id
        where product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
        and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
        or product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
        and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time')");
    
    // $counter = $con->query("select * from product where TIME_TO_SEC(prostarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(prostoptime) > TIME_TO_SEC('$current_time') and status=1 Order by id DESC");
    
    if($counter->num_rows != 0)
    {
        
        $query = $con->query("SELECT 
                        product.id,
                        category.catname,
                        product.pname,
                        product.pimg,
                        product.ptype,
                        product.psdesc,
                        product.pgms,
                        product.pprice,
                        product.stock,
                        product.discount,
                        shop_settings.start_time, 
                        shop_settings.end_time 
                        FROM product
                        INNER JOIN category ON
                        product.cid = category.id and product.shop_id = category.shop_id 
                        INNER JOIN shop_settings on product.protime != '' and product.shop_id = shop_settings.shop_id
                        WHERE (category.catstatus='1' and category.shop_id = '$shop_id' and category.shop_code = '$shop_code' and
                        TIME_TO_SEC(IF (category.cattime = 'Default', shop_settings.start_time, category.catstarttime)) < TIME_TO_SEC('$current_time')
                        and TIME_TO_SEC(IF (category.cattime = 'Default', shop_settings.end_time, category.catstoptime)) > TIME_TO_SEC('$current_time') 
                        or category.catstatus='1' and category.shop_id = '$shop_id' and category.shop_code = '$shop_code' and
                        TIME_TO_SEC(IF (category.cattime = 'Customise',category.catstarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
                        and TIME_TO_SEC(IF (category.cattime = 'Customise',category.catstoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time')) and
                        (product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
                        TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
                        and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
                        or product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
                        TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
                        and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time')) Order by product.stock DESC, category.catname ASC");
        
    // $query = $con->query("SELECT 
    //                     product.id,
    //                     category.catname,
    //                     product.pname,
    //                     product.pimg,
    //                     product.ptype,
    //                     product.psdesc,
    //                     product.pgms,
    //                     product.pprice,
    //                     product.stock,
    //                     product.discount,
    //                     shop_settings.start_time, 
    //                     shop_settings.end_time 
    //                     FROM product
    //                     INNER JOIN category ON
    //                     product.cid = category.id
    //                     INNER JOIN shop_settings on product.protime != ''
    //                     WHERE TIME_TO_SEC(product.prostarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(product.prostoptime) > TIME_TO_SEC('$current_time') and product.status = 1 Order by product.stock DESC,category.catname ASC");
                        
        $result = array();
        $pp=array();
    	
        while($row = $query->fetch_assoc())
        {
            
        $result['id'] = $row['id'];
        $result['catname'] = $row['catname'];
        $result['product_name'] = $row['pname'];
        $result['product_image'] = $row['pimg'];
        $result['product_type'] = $row['ptype'];
        $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"=>"No Products!");
    }
echo json_encode($returnArr);
}
else if($data['cid'] != 0)
{
    $counter = $con->query("SELECT product.*, shop_settings.start_time, shop_settings.end_time 
        FROM product INNER JOIN shop_settings on product.protime != '' and product.shop_id = shop_settings.shop_id
        where product.cid=".$cid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
        and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
        or product.cid=".$cid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
        TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
        and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time')");
    
    // $counter = $con->query("select * from product where TIME_TO_SEC(prostarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(prostoptime) > TIME_TO_SEC('$current_time') and status=1 Order by id DESC");
    
    if($counter->num_rows != 0)
    {
        
        $query = $con->query("SELECT 
                        product.id,
                        category.catname,
                        product.pname,
                        product.pimg,
                        product.ptype,
                        product.psdesc,
                        product.pgms,
                        product.pprice,
                        product.stock,
                        product.discount,
                        shop_settings.start_time, 
                        shop_settings.end_time 
                        FROM product
                        INNER JOIN category ON
                        product.cid = category.id and product.shop_id = category.shop_id
                        INNER JOIN shop_settings on product.protime != '' and product.shop_id = shop_settings.shop_id
                        WHERE product.cid=".$cid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
                        TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.start_time, product.prostarttime)) < TIME_TO_SEC('$current_time')
                        and TIME_TO_SEC(IF (product.protime = 'Default', shop_settings.end_time, product.prostoptime)) > TIME_TO_SEC('$current_time') 
                        or product.cid=".$cid." and product.status='1' and product.shop_id = '$shop_id' and product.shop_code = '$shop_code' and
                        TIME_TO_SEC(IF (product.protime = 'Customise',product.prostarttime, shop_settings.start_time)) < TIME_TO_SEC('$current_time') 
                        and TIME_TO_SEC(IF (product.protime = 'Customise',product.prostoptime, shop_settings.end_time)) > TIME_TO_SEC('$current_time') Order by product.stock DESC, category.catname ASC");
        
    // $query = $con->query("SELECT 
    //                     product.id,
    //                     category.catname,
    //                     product.pname,
    //                     product.pimg,
    //                     product.ptype,
    //                     product.psdesc,
    //                     product.pgms,
    //                     product.pprice,
    //                     product.stock,
    //                     product.discount,
    //                     shop_settings.start_time, 
    //                     shop_settings.end_time 
    //                     FROM product
    //                     INNER JOIN category ON
    //                     product.cid = category.id
    //                     INNER JOIN shop_settings on product.protime != ''
    //                     WHERE TIME_TO_SEC(product.prostarttime) > TIME_TO_SEC('$current_time') and TIME_TO_SEC(product.prostoptime) > TIME_TO_SEC('$current_time') and product.status = 1 Order by product.stock DESC,category.catname ASC");
                        
        $result = array();
        $pp=array();
    	
        while($row = $query->fetch_assoc())
        {
            
        $result['id'] = $row['id'];
        $result['catname'] = $row['catname'];
        $result['product_name'] = $row['pname'];
        $result['product_image'] = $row['pimg'];
        $result['product_type'] = $row['ptype'];
        $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"=>"No Products!");
    }
echo json_encode($returnArr);
}
else
{
echo "dont touch";
}

?>