<?php 	include_once('../../config.php');   	
		include_once(ABS_PATH.'/admin/login_check.php');    
 
			if(isset($_POST['submitF']))
			{   
			    $_POST['userid'] = trim($_POST['userid'],"");        
                $stmt=$conn->prepare("select  cuurentId from   UI__Rgst   where cuurentId=:cuurentId");  
                $stmt->bindValue(':cuurentId', $_POST['userid'] , PDO::PARAM_STR);
                $stmt->execute();    
                if($stmt->rowCount() > 0)
                {    
                    $stmt=$conn->prepare("select  amt,availableFund from  UI__Fundtransfer   where userid=:cuurentId order by id desc");  
                    $stmt->bindValue(':cuurentId', $_POST['userid'] , PDO::PARAM_STR);
                    $stmt->execute();  
                    
                    if($stmt->rowCount() > 0)
                    {
                       $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
                       $totalAvaib = $AllUsers->availableFund+$_POST['fund'];  
                    }else
                        $totalAvaib =  $_POST['fund'];     
                     
        			$stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund) values(:userid,:Tdate,:amt,:availableFund)    "); 
                    $stmt->bindValue(':userid',$_POST['userid'], PDO::PARAM_STR); 
                    $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                    $stmt->bindValue(':amt',$_POST['fund'], PDO::PARAM_STR);  
                    $stmt->bindValue(':availableFund', $totalAvaib , PDO::PARAM_STR);  
                     $stmt->execute(); 
                     
                      
                    $stmt=$conn->prepare("select *  from UI__DailyROIGene    where custId=:sId     ");   
                    $stmt->bindValue(':sId',  $_POST['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',  $_POST['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',  $_POST['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__WithDraw    where userid=:sId and (accept=1 ||accept=2)  ");   
                    $stmt->bindValue(':sId', $_POST['userid'] , PDO::PARAM_INT);  
                    $stmt->execute(); 
                    $UI__WithDraw = $stmt->fetchAll(PDO::FETCH_OBJ); 
                    
                    
                    $totalWithdrwa = 0;
                    foreach($UI__WithDraw as $tl)
                    {
                    $totalWithdrwa +=$tl->amt;
                    }
                    
                      
                    $stmt=$conn->prepare("select availableFund from UI__Fundtransfer  where  userid=:userid  order by id desc  ");   
                    $stmt->bindValue(':userid',  $_POST['userid'] , PDO::PARAM_INT);  
                    $stmt->execute(); 
                    $UI__Fundtransfer = $stmt->fetch(PDO::FETCH_OBJ);   
                    
                       
                    $PAYWAllet =  ($Tamnt+$LevelI+$directIncome+$UI__Fundtransfer->availableFund)-($totalWithdrwa);     
                    
                   
                    $stmt=$conn->prepare("insert into  UI__DebitCredit (custId,addDel,availableBal,Tdate,type,Intype,byw) values(:custId,:addDel,:availableBal,:Tdate,:type,'Fund Credited By User','NA')"); 
                    $stmt->bindValue(':custId',$_POST['userid'], PDO::PARAM_STR); 
                    $stmt->bindValue(':addDel',$_POST['fund'], PDO::PARAM_STR); 
                    $stmt->bindValue(':availableBal',$PAYWAllet, PDO::PARAM_STR);  
                    $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);  
                    $stmt->bindValue(':type', 1 , PDO::PARAM_STR);   
                    $stmt->execute();     
                    $LastIntId = $conn->lastInsertId();
                    
                    $stmt=$conn->prepare("select id from UI__Rgst  where  cuurentId=:cuurentId    ");   
                    $stmt->bindValue(':cuurentId',  $_POST['userid'] , PDO::PARAM_STR);  
                    $stmt->execute(); 
                    $UIid = $stmt->fetch(PDO::FETCH_OBJ);   
                    
                    $amount = password_hash(($PAYWAllet),PASSWORD_DEFAULT);  
                    $stmt =  $conn->prepare("update _pkCc__ set bl1=:bl1,tm=:tm where Uxd=:Uxd "); 
                    $stmt->bindValue(':bl1', $amount, PDO::PARAM_STR); 
                    $stmt->bindValue(':tm', date("d-M-Y H:i"), PDO::PARAM_STR); 
                    $stmt->bindValue(':Uxd', $UIid->id, PDO::PARAM_STR);  
                    $stmt->execute();   
                    
                    $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                    $stmt->bindValue(':cuurentId',  $_POST['userid'] , PDO::PARAM_STR);  
                    $stmt->execute();
                    $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);     	
                    
                    
                    $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                    $stmt->bindValue(':type',   11 , PDO::PARAM_STR);   
                    $stmt->bindValue(':bank',  "Fund Added By User" , PDO::PARAM_STR);   
                    $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                    $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                    $stmt->bindValue(':amountPaid', $_POST['fund']  , PDO::PARAM_STR); 
                    $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                    $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                    $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                    $stmt->bindValue(':custId', $_POST['userid'], PDO::PARAM_STR);  
                    $stmt->bindValue(':cdId', $LastIntId  , PDO::PARAM_STR); 
                    $stmt->bindValue(':Topay',  $_POST['userid'] , PDO::PARAM_STR); 
                    $stmt->bindValue(':TopayName',  $_POST['userid'] , PDO::PARAM_STR);     
                    $stmt->execute();    

                    header("Location: funtransfer.php ");  
    				die;
                } 
			}  
include ABS_PATH.'/admin/header.php';	
?>


 <script>
	$(function() {
		$( "#datepicker" ).datepicker(); 
	}); 
  
	$(document).ready(function()
	{   

	
	$("#product_frm").validate({
		
		errorElement:'div',
		rules:{ 
			userid:{
				required:true				 
			} 
			
			
			
		},
		messages:{				
			 
			 city:{
				required:"Enter city name"				 
			} 
		}
	});


	});
 


</script>



<script> 
$(document).ready(function()
{   
  
	
	    var URL = "<?php echo ROOTPATH; ?>";    
        $( "#userid" ).change(function() 
        {      
                   
                     
          $.ajax({	
                type: "POST",
                url: URL+"/ajax/validation.php",
                data: {userDatacheck:1,Data:$("#userid").val()},
                beforeSend: function(){ 
                },
                success: function(data)
                    {        
                        $("#AvailbleId").html("<span style='color:green;'>"+data+" &#x2713</span>");
                    }
                });   
        });
 
 
}); 

</script>    

		 
		 <div class="col-md-10">

	  			<div class="row">
	  				<div class="col-md-12">
	  					<div class="content-box-large">
							<div class="panel-heading">
								<div class="panel-title"> <h3> Add Fund</h3></div>    
								
								<div class="panel-options"> 

								   <input type="button"  class="btn btn-warning" name="btnback" id="btnback" value=" Back " onClick="javascript:location.href='funtransfer.php';" />
								  
								</div>
							  
								 
							</div>
							
							
			  				<div class="panel-body">    
			  					<form class="form-horizontal" role="form" method="post" enctype="multipart/form-data" id="product_frm"> 
							   	 <div class="form-group">
								    <label for="inputEmail3" class="col-sm-2 control-label">Userid</label>
								    <div class="col-sm-10">
								      <input name="userid"  id="userid" class="form-control"   placeholder="Enter UserId"    required >
								      <span id="AvailbleId"></span>
								    </div>
								 </div>
								 <hr>
								     
								     
								 <div class="form-group">
								    <label for="inputEmail3" class="col-sm-2 control-label">Fund</label>
								    <div class="col-sm-10">
								      <input name="fund"  type="number" class="form-control"   placeholder="Enter Fund"   required >
								    </div>
								 </div>
								 <hr>
								 
								  <div class="form-group">
										<label for="inputEmail3" class="col-sm-2 control-label">Add Fund</label>
										<div class="col-sm-10">
										  <input type="submit"    class="btn btn-primary"  name="submitF" value="Add Fund">
										</div>
								  </div>   
								</form>  
			  				</div>
			  			</div>
	  				</div> 
	  			</div>  


	  		<!--  Page content -->
		  </div>
		</div>
    </div>

<?php  include ABS_PATH.'/admin/footer.php';	?>    