<?php
require "conn.php";
//$test=0;
//send_notification();
//die;

$UserID 	= $_POST["UserID"];
$Money      = $_POST["Money"];
$ReceiverID = $_POST["ReceiverID"];

$Token = "s";

foreach (getallheaders() as $name => $value) { 

	if($name == "token"){
		
		$Token = $value;
	
	}
	
}


$test=0;

	$res = mysqli_query($con,"SELECT * FROM Users WHERE UserToken='$Token'");
	while($row = mysqli_fetch_assoc($res)){
		
		
		$test=4;

	}
	
if($test==4){	


$res = mysqli_query($con,"SELECT SendMoneyPerc FROM OrdersJiblerpercentage");
                        
                                        $result = array();
                        
                                        while($row = mysqli_fetch_assoc($res)){
                                       
											$SendMoneyPerc = $row["SendMoneyPerc"];
											
                                                               
                                        }

$SendMoneyPercww = ($SendMoneyPerc * $Money / 100); 
$Moneym = $Money - $SendMoneyPercww;
$sql="UPDATE Users SET Balance = Balance + $Moneym WHERE UserID = $ReceiverID";
  if(mysqli_query($con,$sql))
  {}

$sql="UPDATE Users SET Balance = Balance - $Money WHERE UserID = $UserID";
  if(mysqli_query($con,$sql))
  {}

	$res = mysqli_query($con,"SELECT name,UserPhoto FROM Users WHERE UserID = $UserID");
	while($row = mysqli_fetch_assoc($res)){
		
		$SenderName  = $row["name"];
		$SenderPhoto = $row["UserPhoto"]; 
	}
	
	$res = mysqli_query($con,"SELECT name,UserFirebaseToken,UserPhoto FROM Users WHERE UserID = $ReceiverID");
	while($row = mysqli_fetch_assoc($res)){
		
		$RecieverName 		= $row["name"];
		$ReceiverPhoto      = $row["UserPhoto"];
		$UserFirebaseToken  = $row["UserFirebaseToken"];
	}
	
	
	
	
	
	
	$mes = $SenderName . " sent you $Money MAD";
	
	send_notification2($UserFirebaseToken,$mes);
	
	newNotfi($UserFirebaseToken,$SenderName,'en',$mes,$accessToken,$ProgID);
	
	
$sql="INSERT INTO SendMoneyTransactions (UserID,TotalMoney,CuttenMoney) VALUES ('$UserID','$Money','$SendMoneyPercww');";
  if(mysqli_query($con,$sql))
  {}
$sql="UPDATE Money SET TotalIncome=TotalIncome+$SendMoneyPercww,BalanceTraComm=BalanceTraComm+$SendMoneyPercww";
			   if(mysqli_query($con,$sql))
			   {}

$sql="INSERT INTO UserTransaction (UserID,Money,Method,DistnationName,DistnationPhoto,DriverID,OrderID,DriverName,Driverphoto,MoneyPlusOrLess,UserFees) VALUES ('$UserID','$Money','QOON Pay','$SenderName','$SenderPhoto','0','0','$RecieverName','$ReceiverPhoto','less','$SendMoneyPercww');";
  if(mysqli_query($con,$sql))
  {
	     
	  $TransID = $con->insert_id;
	  
  }



$sql="INSERT INTO UserTransaction (UserID,Money,Method,DistnationName,DistnationPhoto,DriverID,OrderID,DriverName,Driverphoto,MoneyPlusOrLess,UserFees) VALUES ('$ReceiverID','$Money','QOON Pay','$SenderName','$SenderPhoto','0','0','$RecieverName','$ReceiverPhoto','Add funds','$SendMoneyPercww');";
  if(mysqli_query($con,$sql))
  {
         
	 
		 
    $message ="Added sucssesfully";
    $success = true;
    $status_code = 200;
	 
	echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$TransID,"message"=>$message));		
  }
  else
  {
	   
	$message ="Error";
    $success = false;
    $status_code = 200;
	$result = "added";
	
	echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));

  }


}else{

 	$message ="Error Token Eror";
 	$success = false;
 	$status_code = 200;
 	$result = []; 
 	echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));

}


	function send_notification2($UserFirebaseToken,$Message)
	{
		$url = 'https://fcm.googleapis.com/fcm/send';
		$fields =array(
			 'to' => $UserFirebaseToken,
			 'notification'=>array(
			 'title' => "QOON Pay",
			 'body' => $Message,
			  "link"=> "http://sae-marketing.com",
			 "color"=>'$OrderID',
			 'data'=>'$OrderID')
			);

		$headers = array(
			'Authorization:key=AAAAEDOF67k:APA91bFMPNwvWHetPtqc1i--ztKxrPdSd7ZbTXvrm0LWFV6KHlkw5I-9yOdt6ZtBq1PXo3uVEDcJnFmbAKpNH7tTS9wiKLjAaeLzB0J0KMI6xvsZ5z0C-4Kn98VzSLp_fJs-ibpmOJY2',
			'Content-Type:application/json'
			);

	   $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);           
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
	} 
	
	
	
		function newNotfi($DriverToken,$FNamew,$LANGw,$Body,$accessTokenw,$Pid)
	{

		


		$url = 'https://fcm.googleapis.com/v1/projects/'.$Pid.'/messages:send';
		$fields =array(
			 'to' => $DriverToken,
			 'notification'=>array(
				 'title' => $Title, 
				 'body' => $Body)
			);

        $fields = array(
            'message' => array(
                'token' => $DriverToken,
                'notification' => array(
                    'title' => $Title,
                    'body' => $Body
                )
            )
        );

		$headers = array(         
			'Authorization:Bearer '.$accessTokenw,
			'Content-Type:application/json'
			);

	   $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    //   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: fcm.googleapis.com'));
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);           
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
	}







die;
mysqli_close($con);
?>