<?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:50%;
			height:500px;
			float:right;
			text-align:center;
			padding-top:50px;
		}
		.div1 i{
			font-size:300px;
			color:#6b5127;
			margin:0 10px;
			
		}
		.div1 input,.div1 select, .div1 textarea{
			width:90%;
			height:55px;
			margin:10px;
			padding:2px 10px;
			border-radius:5px;
			border:1px solid #6b5127;
			font-size:19px;
		}
		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">
<?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($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>";
		    }
		    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 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))
					{	
					    
						//$DisEwalletId = mysqli_insert_id($con);
						
						
	                    $DisLiveBal=getBal($userid);
	                    
	                    $quUpDis="update distributer set Balance='$DisLiveBal' where Userid='$userid'";
						mysqli_query($con,$quUpDis);
						
								
						$sql6="insert into 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))
						{
							//$ewalletid=mysqli_insert_id($con);
							
	                         $RetLiveBal=getBal($uid);               
	                        
							$sql7 ="update retailer set Balance='$RetLiveBal' where Userid='$uid'";
							if(mysqli_query($con,$sql7))
							{
								
				                $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">
						<i class="fa fa-inr"></i>
					</div>
					<div class="div1">
					    <p>My Wallet Balance : <?php echo floor($OldDisBal); ?> Rs.</p>
						<input type="text" list="user" name="retailer" placeholder="Retailer..." required>
						<datalist id="user">
						    <?php
						        $quGetRet="SELECT * FROM retailer WHERE Parentid='$userid' AND Active='0'";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            echo "<option value='".$dataGetRet['Userid']."'>".$dataGetRet['Name']." - ".$dataGetRet['Mobile']."</option>";
						        }
						        
						        
						    ?>
						</datalist>
						<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>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>