<?php
	include_once('../config.php');  
	if(!isset($_SESSION['userId']))
	{ 
		 header("Location: ../");
		 die;
	}   
			  
    			 
    	    $stmt=$conn->prepare("select *  from UI__DailyROIGene    where custId=:sId     ");   
            $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $XUI__DailyROIGene = $stmt->fetchAll(PDO::FETCH_OBJ);  
            
            $Tamnt = 0;
            foreach($XUI__DailyROIGene as $xx)
            {
                $Tamnt +=$xx->amount;
            }
            
            
			
			$stmt=$conn->prepare("select *  from UI__Bns    where Rid =:sId   ");   
            $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__Bns = $stmt->fetchAll(PDO::FETCH_OBJ); 
            
             $LevelI = 0;
             foreach($UI__Bns as $dddm)
             {
                 $LevelI +=$dddm->Tamt;
             }
			 

			$stmt=$conn->prepare("select Amt   from UI__Tpm where TopUpId=:refId   ");   
			$stmt->bindValue(':refId',  $_SESSION['userId'] , PDO::PARAM_INT);  
			$stmt->execute(); 
			$MdirectB = $stmt->fetchAll(PDO::FETCH_OBJ);  
                
                $directIncome = 0;
                foreach($MdirectB as $dmm)
                    $directIncome += $dmm->Amt;    
					
			
			
			
			$stmt=$conn->prepare("select *  from UI__Bns    where Rid =:sId  and paidStatus=1  ");   
            $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__BnsPaids = $stmt->fetchAll(PDO::FETCH_OBJ); 
            
             $UI__BnsPaidsR = 0;
             foreach($UI__BnsPaids as $dddm)
             {
                 $UI__BnsPaidsR +=$dddm->Tamt;
             }
             		
					
			 $stmt=$conn->prepare("select *  from UI__DailyROIGene    where custId=:sId   and paidStatus=1    ");   
            $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $XUI__DailyROIGenePaids = $stmt->fetchAll(PDO::FETCH_OBJ);  
            
            $TamntPaidsR = 0;
            foreach($XUI__DailyROIGenePaids as $xx)
            {
                $TamntPaidsR +=$xx->amount;
            }		
			  
			
			$stmt=$conn->prepare("select *  from UI__WithDraw    where userid=:sId   ");   
            $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__WithDraw = $stmt->fetchAll(PDO::FETCH_OBJ); 
            
             
            $totalWithdrwa = 0;
            foreach($UI__WithDraw as $tl)
            {
                $totalWithdrwa +=$tl->orgAmt;
            }  
            
            
			$stmt=$conn->prepare("select *  from UI__Mtransfer    where userid=:sId   ");   
            $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__Mtransfer = $stmt->fetchAll(PDO::FETCH_OBJ); 
            
              
            foreach($UI__Mtransfer as $tlx)
            {
                $totalWithdrwaxx +=$tlx->amont;
            }   
            
            
            $MyWallet = ($Tamnt+$LevelI+$directIncome)-($UI__BnsPaidsR+$TamntPaidsR+$totalWithdrwa+$totalWithdrwaxx); 
            
			if(isset($_POST['submit'])) 
			{      			
				if($MyWallet >=$_POST['Amt']) 
				{ 	  
				    
				    
				    $stmt=$conn->prepare("insert into UI__Mtransfer (userid,Tdate,amont) values(:userid,:Tdate,:amont) ");   
					$stmt->bindValue(':userid', $_SESSION['userId']  , PDO::PARAM_STR);  
					$stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);  
					$stmt->bindValue(':amont', $_POST['Amt'], PDO::PARAM_STR);   
					$stmt->execute();     
					
				    
                    $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+$_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', $AvlBl, PDO::PARAM_STR);   
					$stmt->execute();     
					
					$_SESSION['Regis'] = "Successful!!";
					$_SESSION['topSuc'] =  1;
			  
				}else 
				{    
				    $_SESSION['Regis'] = "Failed";  
				} 					 
				header("Location: MwalletTrn.php"); 
				die;  
			}         
			
include_once("../view/dashboard/MwalletTrn.php");
?>