<?php 
require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   
  
   $user_id=$_POST['user_id'];
   $coupon_id=$_POST['coupon_id'];
   
   $checkEmailExists=mysqli_query($con,"SELECT * FROM apply_coupon WHERE user_id='".$user_id."' AND coupon_id=".$coupon_id."");
	
	if(mysqli_num_rows($checkEmailExists)>0){
		$error=true;
		$errors['success']=false;
		$errors['message']='You have already used this couppon';
	}
	
	
	if($error==false)
	{
		$createitem=mysqli_query($con,"INSERT INTO apply_coupon (user_id,coupon_id,status) VALUES('".$user_id."','".$coupon_id."',1)");
		 if($createitem){
		 $sql="SELECT * FROM coupons where id=".$coupon_id;

		$row = mysqli_query($con,$sql);
     
			if (mysqli_num_rows($row) >= 1) {
				//$r=mysqli_fetch_array($row)	;
					
				while($r=mysqli_fetch_array($row)){
					 
					$cities[]=['id'=>$r['id'],'coupon_code'=>$r['coupon_code'],'discount_type'=>$r['discount_type'],'discount'=>$r['discount'],'start_date'=>$r['start_date'],'end_date'=>$r['end_date']];
					
					// $cities['images']=$images;
					
					
				}   
				$errors['coupons']=$cities;
				//$errors['product_images']=$images;
				$errors['message']='coupons apply successfully';
				$errors['success']=true;
			} 
	 }else{
	     $errors['success']=false;
	     $errors['message']='Somthing went wrong !!';
	 }
	}
  
 
 	 
	 	echo json_encode($errors);
}