<?php
	include_once('../config.php');  
	if(!isset($_SESSION['userId']))
	{ 
		 header("Location: ../");
		 die;
	}   
			  
    			  
            
            $stmt=$conn->prepare("select availableFund   from UI__Fundtransfer    where userid =:userid  order by id desc ");   
            $stmt->bindValue(':userid',  $_SESSION['userId']  , PDO::PARAM_STR);  
            $stmt->execute(); 
            $UI__WithDrawx = $stmt->fetch(PDO::FETCH_OBJ);   
            $AvlBl +=$UI__WithDrawx->availableFund;  
            $MyWallet = $AvlBl;
            
			if(isset($_POST['submit'])) 
			{      			
				if($MyWallet >=$_POST['Amt']) 
				{ 	    
				    
					$MyWallet -=$_POST['Amt'];   
					$stmt=$conn->prepare("insert into UI__Fundtransfer (userid,Tdate,amt,availableFund) values(:userid,:Tdate,:amt,:availableFund) ");   
					$stmt->bindValue(':userid', $_SESSION['userId']  , PDO::PARAM_STR);  
					$stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);  
					$stmt->bindValue(':amt', $_POST['Amt'], PDO::PARAM_STR);  
					$stmt->bindValue(':availableFund', $MyWallet, PDO::PARAM_STR);   
					$stmt->execute();      
                    
                    $stmt=$conn->prepare("select availableFund   from UI__Fundtransfer    where userid =:userid  order by id desc ");   
                    $stmt->bindValue(':userid',  $_POST['cuurentId']  , PDO::PARAM_STR);  
                    $stmt->execute();
                    $UI__WithDrawx = $stmt->fetch(PDO::FETCH_OBJ);   
                    $AvlBl = $UI__WithDrawx->availableFund+$_POST['Amt'];  
                    
                	$stmt=$conn->prepare("insert into UI__Fundtransfer (userid,Tdate,amt,availableFund) values(:userid,:Tdate,:amt,:availableFund) ");   
					$stmt->bindValue(':userid',  $_POST['cuurentId']  , PDO::PARAM_STR);  
					$stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);  
					$stmt->bindValue(':amt', $_POST['Amt'], PDO::PARAM_STR);  
					$stmt->bindValue(':availableFund', $AvlBl, PDO::PARAM_STR);   
					$stmt->execute();   
					
					$_SESSION['Regis'] = "Successful!!";
					$_SESSION['topSuc'] =  1;
			  
				}else 
				{    
				    $_SESSION['Regis'] = "Failed";  
				} 					 
				header("Location: FwalletTrn.php"); 
				die;  
			}         
			
include_once("../view/dashboard/FwalletTrn.php");
?>