<?php
session_start();
require_once('include/constant.php');
include 'get_user_info.php';
require_once('include/functions.php');


if(isset($_REQUEST['btn_send_money']))
{    
	$security_check = isset($_REQUEST['send_money_security']) ? $_REQUEST['send_money_security'] : '';
	if(isset($_SESSION['send_money_security']) && $_SESSION['send_money_security'] == $security_check)
	{
		if(isset($_REQUEST['user_idd']) && $_REQUEST['user_idd'] !='' && isset($_REQUEST['amount']) && $_REQUEST['amount'] !='' && isset($_REQUEST['take_user_id']) && $_REQUEST['take_user_id'] !='')
		{	
		   if(isset($_REQUEST['amount']) && $_REQUEST['amount'] !='' && (int)$total_balance >=(int)$_REQUEST['amount'])
		   {
			$sender_id =$_REQUEST['take_user_id'];
			$receiver_id =$_REQUEST['user_idd'];
			$balance=$_REQUEST['amount'];			
			$remarks=mysqli_real_escape_string($conn,$_REQUEST['remarks']);
			//  1= wallet amount transffer  5= amazon amount transffer
			$transffer_type = isset($_REQUEST['transffer_type']) && $_REQUEST['transffer_type'] !='' ? (int)$_REQUEST['transffer_type'] : 1;
			$affiliate_id = $transffer_type !=1 && isset($_REQUEST['affiliate_id']) && $_REQUEST['affiliate_id'] !='' ? $_REQUEST['affiliate_id'] : null;
			
			/* Money send sender account to receiver account */
			wallet($wallet = array('sender_id'=> $sender_id, 'receiver_id'=>  $receiver_id, 'amount'=> $balance));

			$wallet_arr = array(
				'sender_id'			=>	$sender_id,
				'receiver_id'		=>	$receiver_id,
				'amount'			=>	$balance,
				'respective_user'	=>	null,
				'transaction_type'	=>	$transffer_type,  //  Purchase product
				'remarks'			=>  $remarks,
				'status'			=>	1,
				'affiliate_id'		=>	$affiliate_id,
				'transaction_id'	=>  get_unique_id('t')
			);
			add_wallet_history($wallet_arr);
			
			if($affiliate_id !='' && $affiliate_id !=0)
			{
				// update affiliate status 
				$sql = "UPDATE affiliate SET status = 1, last_update = '".$today_date."' WHERE id = '".(int)$affiliate_id."' ";
				$con = mysqli_query($conn, $sql);
			}
			
			echo '<script> alert("Money send successfully"); window.location="money_send.php"; </script>'; 
		   }
		   else
		   {
			  echo '<script> alert("Insufficient wallet balance. Please add money to wallet"); window.location="money_send.php"; </script>'; 
		   }
		}
		else
		{
			echo '<script> alert("Sorry, some details are missing"); window.location="money_send.php"; </script>';
		}
	}
	else
	{
		echo '<script> alert("Sorry, something went wrong"); window.location="money_send.php"; </script>';
	}
     
} 
else
{
	echo '<script> alert("Sorry, something went wrong"); window.location="money_send.php"; </script>';
}

?>