<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/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:17px;
			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;
		}
		
		#dist,#reta,#apiUsr,#mlmUsr{display:none;}
		@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'];
		$tableWalletName="wallet";
		if($usrType=="M.Distributers")
		{
		    $mdis = $_POST['mdis'];
		    $clintId = $mdis;
		    $tableName="masterdis";
		}
		elseif($usrType=="Distributers")
		{
		    $distrbuter = $_POST['distrbuter'];
		    $clintId = $distrbuter;
		    $tableName="distributer";
		}
		elseif($usrType=="Retailers")
		{
		    $retailer = $_POST['retailer'];
		    $clintId = $retailer;
		    $tableName="retailer";
		}
		elseif($usrType=="Api Partners")
		{
		    $apiUser = $_POST['apiUser'];
		    $clintId = $apiUser;
		    $tableName="retailer";
		}
		elseif($usrType=="MLM Users")
		{
		    $mlmUser = $_POST['mlmUser'];
		    $clintId = $mlmUser;
		    $tableName="MLM_Users";
		    $tableWalletName="MLM_Wallet";
		}
		
		
		$inputbal = $_POST['amount'];
		$rmark=$_POST['rmark'];
		 
	    $adminName=getName($userid);	
	    $clintName=getName($clintId);	
	    
		$OldClintBal=getBal($clintId);
		$BothOldBal=$OldAdminBal+$OldClintBal;
		    
		if($OldClintBal<$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','$clintId','$inputbal','$inputbal')";
			if(mysqli_query($con,$sql1))
			{
				$paymentid=mysqli_insert_id($con);
					
				$NewAdminBal=$OldAdminBal+$inputbal;
				$NewClintBal=$OldClintBal-$inputbal;
					
	            $sqlDis="INSERT INTO wallet (Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time,APBU)
				VALUES('$userid','$OldAdminBal','$inputbal','$NewAdminBal','FUND DEDUCT','$clintId','$paymentid','$rmark','$date','$time','$userid')";
				if(mysqli_query($con,$sqlDis))
				{	
				    $quUpDis="UPDATE admin SET Balance='$NewAdminBal' WHERE Userid='$userid'";
					mysqli_query($con,$quUpDis);
								
					$sql6="INSERT INTO $tableWalletName (Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Date,Time,APBU)
					VALUES ('$clintId','$OldClintBal','-$inputbal','$NewClintBal','FUND DEBIT','$userid','$paymentid','$date','$time','$userid')";
					if(mysqli_query($con,$sql6))
					{
					    $sql7 ="UPDATE $tableName SET Balance='$NewClintBal' WHERE Userid='$clintId'";
						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 Deduct 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);
	}
	
?>
				<script>
				    function usrTyp()
				    {
				        var usrType = document.getElementById("usrType").value;
				        if(usrType=="M.Distributers")
				        {
				            document.getElementById("mdist").style.display = "inline";
				            document.getElementById("dist").style.display = "none";
				            document.getElementById("reta").style.display = "none";
				            document.getElementById("apiUsr").style.display = "none";
				            document.getElementById("mlmUsr").style.display = "none";
				        }
				        else if(usrType=="Distributers")
				        {
				            document.getElementById("mdist").style.display = "none";
				            document.getElementById("dist").style.display = "inline";
				            document.getElementById("reta").style.display = "none";
				            document.getElementById("apiUsr").style.display = "none";
				            document.getElementById("mlmUsr").style.display = "none";
				        }
				        else if(usrType=="Retailers")
				        {
				            document.getElementById("mdist").style.display = "none";
				            document.getElementById("dist").style.display = "none";
				            document.getElementById("reta").style.display = "inline";
				            document.getElementById("apiUsr").style.display = "none";
				            document.getElementById("mlmUsr").style.display = "none";
				        }
				        else if(usrType=="Api Partners")
				        {
				            document.getElementById("mdist").style.display = "none";
				            document.getElementById("dist").style.display = "none";
				            document.getElementById("reta").style.display = "none";
				            document.getElementById("apiUsr").style.display = "inline";
				            document.getElementById("mlmUsr").style.display = "none";
				        }
				        else if(usrType=="MLM Users")
				        {
				            document.getElementById("mdist").style.display = "none";
				            document.getElementById("dist").style.display = "none";
				            document.getElementById("reta").style.display = "none";
				            document.getElementById("apiUsr").style.display = "none";
				            document.getElementById("mlmUsr").style.display = "inline";
				        }
				    }
				</script>
				<div class="div1" style="padding-top:50px">
				    <form method="post">
					    <p>My Wallet Balance : <?php echo floor($OldAdminBal); ?> Rs.</p>
					    <select name="usrType" id="usrType" onchange="usrTyp()">
					        <option>M.Distributers</option>
					        <option>Distributers</option>
					        <option>Retailers</option>
					        <option>Api Partners</option>
					        <option>MLM Users</option>
					    </select>
						<input type="text" list="mdis" name="mdis" placeholder="Master Distributer..." id="mdist">
						<datalist id="mdis">
						    <?php
						        $quGetRet="SELECT * FROM masterdis";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            echo "<option value='".$dataGetRet['Userid']."'>".$dataGetRet['Name']." - ".$dataGetRet['Mobile']."</option>";
						        }
						    ?>
						</datalist>
						<input type="text" list="distrbuter" name="distrbuter" placeholder="Distributer..." id="dist">
						<datalist id="distrbuter">
						    <?php
						        $quGetRet="SELECT * FROM distributer";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            echo "<option value='".$dataGetRet['Userid']."'>".$dataGetRet['Name']." - ".$dataGetRet['Mobile']."</option>";
						        }
						        
						        
						    ?>
						</datalist>
						<input type="text" list="retaler" name="retailer" placeholder="Retailer..." id="reta">
						<datalist id="retaler">
						    <?php
						        $quGetRet="SELECT * FROM retailer WHERE Active='0' AND ApiActive='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="text" list="api" name="apiUser" placeholder="Api Users..." id="apiUsr">
						<datalist id="api">
						    <?php
						        $quGetRet="SELECT * FROM retailer WHERE ApiActive='1'";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            echo "<option value='".$dataGetRet['Userid']."'>".$dataGetRet['Name']." - ".$dataGetRet['Mobile']."</option>";
						        }
						        
						        
						    ?>
						</datalist>
						<input type="text" list="mlm" name="mlmUser" placeholder="MLM Users..." id="mlmUsr">
						<datalist id="mlm">
						    <?php
						        $quGetRet="SELECT * FROM MLM_Users";
						        $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>Date Time</th>
							<th>Discription</th>
						</tr>
						<?php
						    include'connection.php';
							$quGetTxn="SELECT * FROM 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>