<?php 
 
 require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
$uid = $data['uid'];
// $uid = 1;
if($uid == '')
{
	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong  try again !");
}
else 
{
    $shop_id = strip_tags(mysqli_real_escape_string($con,$data['shop_id']));
    $shop_code = strip_tags(mysqli_real_escape_string($con,$data['shop_code']));
    
	$cy = $con->query("select * from coupon_code where status='1' and admin_status='1' and shop_id = '$shop_id' and shop_code = '$shop_code' order by priority ASC");
	$coupon_count = $con->query("select * from coupon_code where status='1' and admin_status='1' and shop_id = '$shop_id' and shop_code = '$shop_code' order by priority ASC")->num_rows;
	if($coupon_count != 0)
	{
    	$p = array();
    	$q = array();
	    while($row = $cy->fetch_assoc())
    	{
    	    $code = $row['code'];
    	    $count = $con->query("select * from orders where uid='$uid' and coupon_code='$code'")->num_rows;
    	    $use_limit = intval($row['max_use_limit']);
    	    if($use_limit != 0)
    	    {
        	    if($count < $use_limit)
        	    {
            		$p['id'] = $row['id'];
            		$p['name'] = $row['name'];
            		$p['image'] = $row['image'];
            		$p['code'] = $row['code'];
            		$p['discount'] = $row['discount'];
            		$p['description'] = $row['description'];
            		$p['max_order'] = $row['max_order_amount'];
            		$p['max_discount'] = $row['max_discount_amount'];
            		$p['status'] = $row['status'];
            		$q[] = $p;
        	    }
    	        
    	    }
    	    else
    	    {
    	        $p['id'] = $row['id'];
        		$p['name'] = $row['name'];
        		$p['image'] = $row['image'];
        		$p['code'] = $row['code'];
        		$p['discount'] = $row['discount'];
        		$p['description'] = $row['description'];
        		$p['max_order'] = $row['max_order_amount'];
        		$p['max_discount'] = $row['max_discount_amount'];
        		$p['status'] = $row['status'];
        		$q[] = $p;
    	    }
    	}
        $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Coupon Code Get Successfully!","ResultData"=>$q);
	}
	else{
	    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"No Available Coupon codes!");
	}
	
}
echo json_encode($returnArr);
mysqli_close($con);
?>