<?php 
	require_once 'inserts.php';
	if(
		isset($_POST['_token']) &&
		isset($_POST['email']) &&
		isset($_POST['amount']) &&
		isset($_POST['note'])
		){
		
			$token  = mysqli_real_escape_string($conn,trim($_POST['_token']));
			$d_email  = mysqli_real_escape_string($conn,trim($_POST['email']));
			$amount  = mysqli_real_escape_string($conn,trim($_POST['amount']));
			$note  = mysqli_real_escape_string($conn,trim($_POST['note']));
			
			if(
			!empty($token)&&
			!empty($d_email)&&
			!empty($amount)
			// !empty($note)
			){
				if(($infos['bal'] >= $amount) && ($amount > 0) ){

		$q="select uname from info where email='$d_email' ORDER BY id DESC LIMIT 1";
		$r = mysqli_query($conn,$q);
		$user =  mysqli_R($r,0);
		if(!empty($user)){			

		$q="select bal from info where email='$d_email' ORDER BY id DESC LIMIT 1";
		$r= mysqli_query($conn,$q);
		$user_bal = mysqli_R($r,0);

		$q2="select bal from info where email='$mail' ORDER BY id DESC LIMIT 1";
		$r= mysqli_query($conn,$q2);
		$my_bal = mysqli_R($r,0);

		$new_user_bal = $user_bal + $amount;
		$new_my_bal = ($my_bal - ($amount + ($amount*0.06)));

		

		$stmt_update_user_bal =$conn->prepare("UPDATE info SET bal = '$new_user_bal' WHERE email =?");
		$stmt_update_user_bal->bind_param("s",$d_email);
		if($stmt_update_user_bal ->execute()){

			$stmt_update_my_bal = $conn->prepare("UPDATE info SET bal = '$new_my_bal' WHERE email =?");
			$stmt_update_my_bal->bind_param("s",$mail);
			$stmt_update_user_bal ->execute();

			$new_send_email = $d_email;		
			$new_send_from = $mail ;
			$new_send_to = $d_email;
			
			$new_send_amount  = $amount;
			$new_send_note = $note;

			   
				   if($stmt_send->execute()){
		   
					$trans_id =	$_SESSION['trans_id']	= strtoupper('TRX'.substr(md5(rand(1,99999999)),rand(1,15),10));
					$trans_id2 =	$_SESSION['trans_id']	= strtoupper('TRX'.substr(md5(rand(1,99999999)),rand(1,15),10));
		   
					   $stmt_send->close();
		   
			$new_trans_id = $trans_id;
			$new_description = 'Transfer Money To '.$user ;
			$new_email = $mail;
			$new_amount = $amount;
			$new_fee = $amount*0.06;
			$new_status = 'success';
			$new_method = 'system';
			$new_type ='send money';
			$created_at;
			$updated_at;

			$_SESSION['notifyUser2']= ucwords($user);
		
			if($stmt_trans->execute()){
				$_SESSION['send_money_pay']=true;
				$_SESSION['send_money_amount']= $amount;

				$new_trans_id = $trans_id2;
				$new_description = 'Received Money From '.ucfirst($infos['uname']) ;
				$new_email = $d_email;
				$new_amount = $amount;
				$new_fee = 0;
				$new_status = 'success';
				$new_method = 'system';
				$new_type ='received money';
				$created_at;
				$updated_at;
				$stmt_trans->execute();
				note_msg('Money Sent ','../send-money/',1);


				   }else{
					note_msg('An error occurred','../send-money/',2);
	   
				   }


		}else{
			note_msg('Database error occurred','../send-money/',2);

		}

	
				
                // echo 'Name: '.ucwords($user);
            }else{
                note_msg('User Not Found','../send-money/',2);
            }		
				
		
			}else{
                note_msg('User Not Found','../send-money/',2);
			}
		}else{
			note_msg('User Not Found','../send-money/',2);

		}

	}else{
		note_msg('All fields required','../send-money/',2);
	}
}else{
	note_msg('Error 500','../send-money/',2);
}

?>