<?php 
require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   
  
   $user_id=$_POST['user_id'];
   $points=$_POST['points'];
   $to_phone=$_POST['to_phone'];
 $fromuser=mysqli_fetch_array(mysqli_query($con,"SELECT id,point_wallet FROM users WHERE id='".$user_id."'"));
 
 $user=mysqli_fetch_array(mysqli_query($con,"SELECT id,point_wallet FROM users WHERE phone='".$to_phone."'"));
 
  if(!empty($user)){
 $to_userid=$user['id'];

		$createitem=mysqli_query($con,"INSERT INTO transfer_points (from_user,to_user,points,status) VALUES('".$user_id."','". $to_userid."','".$points."',1)");
	 if($createitem){
		
		$updatefrompoints=$fromuser['point_wallet']-$points;
		$updatetopoints=$user['point_wallet']+$points;
		 mysqli_query($con,"UPDATE users SET point_wallet=".$updatefrompoints." WHERE id=".$fromuser['id']);
		 mysqli_query($con,"UPDATE users SET point_wallet=".$updatetopoints." WHERE id=". $to_userid);
	     	$errors['success']='Points Transfer Sucessfully';
	 }else{
	     $errors['success']='Somthing went wrong !!';
	 }
 	 
	 	
  }else{
	  $errors['success']='User Not Found !!';
  }
  echo json_encode($errors);
}