<?php
	include'session.php';
	include'../connection.php';
	include'../functions.php';
?>
<html>
<head>
	<title>Fund Debit</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:0 1%;
			
		}
		.div1 table{
			width:94%;
			text-align:center;
			font-size:13px;
			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: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 Debit :-</p></div>
				<div class="msg"><marquee><?php include'../msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
<?php
        
	$OldAdminBal=getBal($userid); 
	    
	if(isset($_POST['submit']))
	{
		$usrType  = $_POST['usrType'];
		$amount   = $_POST['amount'];
		$clintId  = $_POST['retailer'];
		$rmark    = $_POST['rmark'];
		 
	    $adminName=getName($userid);	
	    $clintName=getName($clintId);	
	    
		$OldClintBal=getBal($clintId);
		$BothOldBal=$OldAdminBal+$OldClintBal;
		    
		if($OldClintBal<$amount)
	    {
		    $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
	    {
	        
	        	$NewAdminBal=$OldAdminBal+$amount;
				$NewClintBal=$OldClintBal-$amount;
					
	            $sqlDis="INSERT INTO wallet (Date,Time,Userid,OpBal,Txn,ClBal,TxnType,TxnWith,Remarks)
				VALUES('$date','$time','$userid','$OldAdminBal','$amount','$NewAdminBal','FUND CREDIT','$clintId','$rmark')";
				if(mysqli_query($con,$sqlDis))
				{
				    $adminWalletId = mysqli_insert_id($con);
				    
				    $sql6="INSERT INTO wallet (Date,Time,Userid,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith,Remarks)
					VALUES ('$date','$time','$clintId','$OldClintBal','-$amount','$NewClintBal','$adminWalletId','FUND DEBIT','ADMIN','$rmark')";
					if(mysqli_query($con,$sql6))
					{
					    $clinteWalletId = mysqli_insert_id($con);
				    
					    $sql7 = "UPDATE wallet SET TxnId='$clinteWalletId' WHERE Id='$adminWalletId'";
						if(mysqli_query($con,$sql7))
						{
						    $AdminLiveBal=getBal($userid);
	                        $clintLiveBal=getBal($clintId);
	                        
						    $BothNewBal=$AdminLiveBal+$clintLiveBal;
				            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");
				}
		}
		mysqli_close($con);
	}
	
?>
				<div class="div1" style="padding-top:50px">
				    <form method="post">
					    <p>My Wallet Balance : <?php echo floor($OldAdminBal); ?> Rs.</p>
						<input type="text" list="retaler" name="retailer" placeholder="Retailer..." id="reta">
						<datalist id="retaler">
						    <?php
						        $quGetRet="SELECT * FROM user WHERE Status='ACTIVE'";
						        $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()'>
				    </form>	
				</div>
				<div class="div1">
					<center><h1>Recent Transfer</h1></center><hr>
					<table cellpadding="20" border="2">
						<tr>
							<th>Id</th>
							<th>Date<br>Time</th>
							<th>Clinte</th>
							<th><i class='fa fa-inr'></i></th>
							<th>Remarks</th>
						</tr>
						<?php
						    $data1 = "w.Id, w.Date, w.Time, w.Txn, w.TxnWith, w.Remarks,";
						    $data2 = "u.Name";
						    
						    $LJwNu = "LEFT JOIN user AS u ON w.TxnWith=u.Userid";
						    $where1 = "w.Userid='$userid' AND w.TxnType='FUND DEBIT'";
						    $quGetTxn="SELECT $data1 $data2 FROM wallet AS w $LJwNu WHERE $where1 ORDER BY Id DESC LIMIT 20";
							$reGetTxn=mysqli_query($con,$quGetTxn);
							while($dataGetTxn=mysqli_fetch_array($reGetTxn))
							{
							    $WalletId   = $dataGetTxn[0];
							    $Date       = $dataGetTxn[1];
							    $Time       = $dataGetTxn[2];
							    $Amount     = $dataGetTxn[3];
							    $ClinteId   = $dataGetTxn[4];
							    $Remarks    = $dataGetTxn[5];
							    $ClinteName = $dataGetTxn[6];
							    echo "<tr>";
									echo "<td>".$WalletId."</td>";
									echo "<td>".$Date."<br>".$Time."</td>";
									echo "<td>".$ClinteName."<br>[ ".$ClinteId." ]</td>";
									echo "<td>".abs($Amount)."</td>";
									echo "<td><textarea style='font-size:12px; width:170px;'>".$Remarks."</textarea></td>";
								echo "</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>