<?php
	include'session.php';
	include'connection.php';
	include'functions.php';
?>
<html>
<head>
	<title>Fund Transfer</title>
	<!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
	<link rel="icon" href="images/logo.png" type="image/gif" sizes="16x16">
	
	<!-- Admin CSS File Link -->
	<link rel="stylesheet" href="css/style.css">
	
	<!-- User JAVA SCRIPT File Link -->
	<script type="text/javascript" src="js/script.js"></script>

	
	<!-- Font Awesome Icone -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
	
	<style>
		.div1{
			background:#f7f7f7;
			width:48%;
			height:auto;
			float:left;
			text-align:center;
			margin:3% 1%;
			
		}
		.div1 table{
			width:94%;
			text-align:center;
			font-size:14px;
			border-collapse:collapse;
			margin:10px;
			padding:2px 10px;
		}
		.div1 hr{
			margin:5px 10px;
			padding:0 10px;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
		}
		.div1 input,.div1 select, .div1 textarea{
			width:90%;
			height:55px;
			margin:10px;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:14px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			font-size:30px;
			transition:1s;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			border:1px solid #6b5127;
			cursor:pointer;
			transition:1s;
		}
		
		@media(max-width:768px)
		{
			.div1{
				width:100%;
				height:auto;
			}
			.nav,.container{
				height:auto;
			}
		}
	</style>
	<link rel="stylesheet" href="css/mediaStyle.css">
</head>
<body>
	<!--Header Divison-->
	<?php
		require'header.php';
	?>
	
	<!--Container Division-->
	<div class="container">
		<?php
			include'menu.php';
		?>
		<div class="content">
			<div class="head">
				<div class="title"><p>Fund Transfer :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			    <br><center><h3 style="color:blue">You can transfer fund of Rs. 100 or above only !</h3></center>
<?php
        
	$OldDisBal=getBal($userid); 
	    
	if(isset($_POST['submit']))
	{
		$uid = $_POST['retailer'];
		$inputbal = $_POST['amount'];
		$rmark=$_POST['rmark'];
		 
	    $disName=getName($userid);	
	    $retaName=getName($uid);	
		
	    $OldRetBal=getBal($uid);
	    
		$BothOldBal=$OldDisBal+$OldRetBal;
		    
		    
		    if($inputbal<100)
		    {
			    $flag=0;
			    echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Amount Not Allowed Less Than 100 !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
		    }
		    elseif($uid=="--SELECT USER--")
		    {
			    $flag=0;
			    echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Select User Please !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
		    }
		    elseif($OldDisBal<$inputbal)
		    {
			    $flag=0;
			    echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>Insufficiant Balance !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
		    }
		    elseif("true"!=checkChild($userid,$uid))
		    {
		        $flag=0;
			    echo "<div class='msg-div'>
			            <form method='post'>
				            <h3>User Is Not In Your Downline !</h3><br>
				            <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				            <button type='submit' class='btnPopup'>OK</button><br><br>
			            </form>
		            </div>";
		    }
		    else
		    {
		        
		        mysqli_query($con,"START TRANSACTION");
				    
				$sql1="INSERT INTO transaction(Fromid,Toid,Credit,Debit) VALUES('$userid','$uid','$inputbal','$inputbal')";
				if(mysqli_query($con,$sql1))
				{
					$paymentid=mysqli_insert_id($con);
					
					$NewDisBal=$OldDisBal-$inputbal;
					$NewRetBal=$OldRetBal+$inputbal;
					
	                $sqlDis="INSERT INTO MLM_Wallet(Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time,APBU)
					VALUES ('$userid','$OldDisBal','-$inputbal','$NewDisBal','FUND TRANSFER','$uid','$paymentid','$rmark','$date','$time','$userid')";
					if(mysqli_query($con,$sqlDis))
					{	
					    $quUpDis="UPDATE MLM_Users SET Balance='$NewDisBal' WHERE Userid='$userid'";
						mysqli_query($con,$quUpDis);
								
						$sql6="INSERT INTO MLM_Wallet(Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Date,Time,APBU)
						VALUES ('$uid','$OldRetBal','$inputbal','$NewRetBal','FUND RECEIVED','$userid','$paymentid','$date','$time','$userid')";
						if(mysqli_query($con,$sql6))
						{
							$RetLiveBal=getBal($uid);               
	                        
							$sql7 ="UPDATE MLM_Users SET Balance='$RetLiveBal' WHERE Userid='$uid'";
							if(mysqli_query($con,$sql7))
							{
								$DisLiveBal=getBal($userid);
	                    
	                            $BothNewBal=$DisLiveBal+$RetLiveBal;
				                if(floor($BothOldBal)==floor($BothNewBal))
				                {
									echo "<div class='msg-div'>
							                <form method='post'>
							                <h3>Payment Transfered successfully !</h3><br>
							                <i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
							                <button type='submit' class='btnPopup'>OK</button><br><br>
							                </form>
						                </div>";
                                    mysqli_query($con,"COMMIT");
                                                
				                }
				                else
				                {
				                    echo "<error query Sum>";
									mysqli_query($con,"ROLLBACK"); 
				                }
							}
							else
							{
								echo "<error query 7>";
								mysqli_query($con,"ROLLBACK");
							}
						}
						else
						{
							echo "<error query 6>";
							mysqli_query($con,"ROLLBACK");
						}
					}
					else
					{
						echo "error in query 5";
						mysqli_query($con,"ROLLBACK");
					}
				}
				else
				{
					echo "<error query 2>";
					mysqli_query($con,"ROLLBACK");
				}
			}
			mysqli_close($con);
			
	}
	
?>
				<form method="post">
					<div class="div1" style="padding-top:50px">
					    <p>My Wallet Balance : <?php echo floor($OldDisBal); ?> Rs.</p>
						<select id="user" name="retailer">
						    <option>--SELECT USER--</option>
						    <?php
						        $quGetRet="SELECT * FROM MLM_Users ORDER BY Name ASC";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            if("true"==checkChild($userid,$dataGetRet['Userid']))
						            {
						                echo "<option value='".$dataGetRet['Userid']."'>".$dataGetRet['Name']." - ".$dataGetRet['Mobile']."</option>";
						            }
						        }
						    ?>
						</select>
						<input type="number" name="amount" placeholder="Enter Amount..." required>
						<textarea name="rmark" placeholder="Remarks..."></textarea>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>	
				<div class="div1">
					<center><h1>Recent Transfer</h1></center><hr>
					<table cellpadding="20" border="2">
						<tr>
							<th>Date Time</th>
							<th>Discription</th>
						</tr>
						<?php
						    include'connection.php';
							$quGetTxn="SELECT * FROM MLM_Wallet WHERE Userid='$userid' AND TxnType='FUND TRANSFER' ORDER BY Id DESC LIMIT 10";
							$reGetTxn=mysqli_query($con,$quGetTxn);
							while($dataGetTxn=mysqli_fetch_array($reGetTxn))
							{
							    if($usrName=getName($dataGetTxn['TxnWith']))
							    {
							        $txnWith=$usrName;
							    }
							    else
							    {
							        $txnWith="SELF";
							    }
								echo "<tr>";
									echo "<td>".$dataGetTxn['Date']." ".$dataGetTxn['Time']."</td>";
									echo "<td>".$txnWith." <i class='fa fa-inr'></i>".abs($dataGetTxn['Txn'])." ".$dataGetTxn['Remarks']."</td>";
								echo "</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>