<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';

?>
<html>
<head>
	<title>Minus Lapu Amount</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:;
			width:98%;
			height:450px;
			margin:0 1%;
			padding:10px;
			overflow:auto;
			margin:10px 0;
		}
		.div1 input, .div1 select{
			height:35px;
			width:200px;
			font-size:15px;
			padding:2px 10px;
			border-radius:5px;
			border:solid #6b5127 1px;
			margin:5px 10px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			cursor:pointer;
			transition:1s;
		}
		input[type=submit]:hover{
			background:white;
			color:#883333;
			border:solid #6b5127 1px;
			transition:1s;
		}
		.div2{
			background:#f7f7f7;
			width:100%;
			height:550px;
			float:left;
			text-align:center;
			padding:10px 20px;
			overflow:auto;
		}
		INPUT{padding:5px 10px; border-radius:4px;}
		.div2 table{
			width:98%;
			text-align:center;
			font-size:14px;
			border-collapse:collapse;
			padding:2px 10px;
			margin:1%;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
			padding:2px 5px;
		}
		table td{
			background:;
			height:35px;
			border:solid black 1px;
			padding:2px 5px;
		}
		table tr:hover{background-color:gray; color:white;}
		table tr:hover a{color:white;}
		
		.deactAnchor{background:green; color:white; padding:2px 5px; border-radius:3px; cursor:pointer;}
		
		#lapuList{display:none;}
		.fa-check, .fa-ban{font-size:20px;}
		@media(max-width:768px)
		{
			.div1{
				background:;
				width:100%;
				height:auto;
				margin:10px 0 0 0;
				padding:0;
				text-align:center;
			}
			.div1 input, .div1 select{
				width:90%;
				margin:10px;
			}
			.div2{
				width:100%;
				margin:10px 0 0 0;
				padding:0px;
				text-align:center;
			}
		}
	</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>Minus Lapu Amount :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
				<div class="div2" id="downlinePlace">
					<script>
						function deacMinus(Arg)
						{
						    var txnDeactiveId = Arg;
							var xmlhttpState = new XMLHttpRequest();
							xmlhttpState.open("GET","Ajax.php?txnDeactiveId="+txnDeactiveId, false)
							xmlhttpState.send(null);
							if(xmlhttpState.responseText=="Deactivate")
							{
							    document.getElementById(txnDeactiveId).style.display = "none";
							}
						}
					</script>
					<?php
					        if($_GET['lapuDate'])
						    {
						        $lapuDate=$_GET['lapuDate'];
						        $lapuNo=$_GET['lapuNo'];
						        echo '<form method="post"><input type="submit" name="update" value="DEACTIVE ALL"></form>';
						        if(isset($_POST['update']))
						        {
						            $UpdateQu="UPDATE recharge SET ActiveMinus='1' WHERE rechargedate='$lapuDate' AND LapuNo='$lapuNo'";
						            if(mysqli_query($con,$UpdateQu))
						            {
						                echo "UPDATED SUCCESSFULLY";
						            }
						        }
						        
						    }
						    
					?>
					<form method="post">
					    <input type="submit" Value="All Minus Deactive" name="AllDeactive">
					</form>
					<?php
					    if(isset($_POST['AllDeactive']))
					    {
					        $DeactiveAllMinus="UPDATE recharge SET ActiveMinus='1' WHERE ActiveMinus='0'";
					        if(mysqli_query($con,$DeactiveAllMinus))
					        {
					            echo "<center><h3>All Minus Deactivated Successfully !</h3></center>";
					        }
					    }
					?>
					<table cellpadding="20" border="2"> 
						<tr>
							<th>Sl.</th>
							<th>Date</th>
							<th>Time</th>
							<th>API</th>
							<th>Lapu Owner</th>
							<th>Lapu No.</th>
							<th>Lapu Bal</th>
							<th>Diff</th>
							<th>COUNT ROW</th>
							<th>Order Id</th>
							<th>#</th>
							<th>#</th>
							<th>LAD</th>
						</tr>
						<?php
						    if($_GET['lapuDate'])
						    {
						        $lapuDate=$_GET['lapuDate'];
						        $lapuNo=$_GET['lapuNo'];
						        $UsrQu="SELECT * FROM recharge WHERE Diff<-10 AND ActiveMinus='0' AND rechargedate='$lapuDate' AND LapuNo='$lapuNo' ORDER BY Id DESC";
						    }
						    else
						    {
							    $UsrQu="SELECT * FROM recharge WHERE Diff<-10 AND ActiveMinus='0' ORDER BY Id DESC";
						    }
							$UsrResult=mysqli_query($con,$UsrQu);
							while($UsrData=mysqli_fetch_array($UsrResult))
							{
								$sl=$sl+1;
								$slDiff=0;
								$orderId=$UsrData['Id'];
								$rechDate=$UsrData['Rechargedate'];
								$lapuNo=$UsrData['LapuNo'];
								
								$TotRechAmt1=0;
								$TotRechAmt2=0;
								$TotR_Offer1=0;
								$TotR_Offer2=0;
							    $quDiff1="SELECT Amount, R_Offer, LapuOldBal FROM recharge WHERE LapuNo='$lapuNo' AND Id<'$orderId' AND Status='SUCCESS' ORDER BY Id DESC LIMIT 5";
								$reDiff1=mysqli_query($con,$quDiff1);
								$count_reDiff1=mysqli_num_rows($reDiff1);
								while($dataDiff1=mysqli_fetch_array($reDiff1))
								{
								    $TotRechAmt1=$TotRechAmt1+$dataDiff1[0];
								    $TotR_Offer1=$TotR_Offer1+$dataDiff1[1];
								    $LapuOldBal=$dataDiff1[2];
								}
								
								$quDiff2="SELECT Amount, R_Offer, ApiBal FROM recharge WHERE LapuNo='$lapuNo' AND Id>='$orderId' AND Status='SUCCESS' ORDER BY Id ASC LIMIT 5";
								$reDiff2=mysqli_query($con,$quDiff2);
								$count_reDiff2=mysqli_num_rows($reDiff2);
								while($dataDiff2=mysqli_fetch_array($reDiff2))
								{
								    $TotRechAmt2=$TotRechAmt2+$dataDiff2[0];
								    $TotR_Offer2=$TotR_Offer2+$dataDiff2[1];
								    $LapuNewBal=$dataDiff2[2];
								}
								
								$totRechAmount=$TotRechAmt1+$TotRechAmt2;
								$totR_Offer=$TotR_Offer1+$TotR_Offer2;
								$remainBal=$LapuOldBal-$totRechAmount+$totR_Offer;
								$diff=$LapuNewBal-$remainBal;
								$countRow=0;
								if($count_reDiff1>=5 && $count_reDiff2>=5)
								{
								    $countRow=5;
								}
								$absDiff=abs($diff);
								if(floor($absDiff)==0  && $countRow==5)
								{
								    $fontColor="style='color:#126105'";
								    $rechId=$UsrData['Id'];
								    $quDeactiveTxn="UPDATE recharge SET ActiveMinus='1' WHERE Id='$rechId'";
								    mysqli_query($con,$quDeactiveTxn);
								}
								else
								{
								    $fontColor="style='color:red'";
								}
								
								echo "<tr >";
									echo "<td>".$sl."</td>";
									echo "<td><a href='minus_amount.php?lapuDate=".$UsrData['Rechargedate']."&lapuNo=".$UsrData['LapuNo']."' target='blank_'>".$UsrData['Rechargedate']."</td>";
									echo "<td>".$UsrData['Time']."</td>";
									echo "<td>".$UsrData['API']."</td>";
									echo "<td>".getLON($UsrData['LapuNo'])."</td>";
									echo "<td><a href='recharge_by_lapu.php?lapuNo=".$UsrData['LapuNo']."&rechId=".$UsrData['Id']."&rechDate=".$UsrData['Rechargedate']."' target='_blank'>".$UsrData['LapuNo']."</td>";
									echo "<td>".$UsrData['ApiBal']."</td>";
									echo "<td><h4 $fontColor>".$UsrData['Diff']."<h4></td>";
									echo "<td>".$count_reDiff1." - ".$count_reDiff2."</td>";
									echo "<td><a href='check_txn_status.php?rechId=".$UsrData['Id']."' target='_blank'>".$UsrData['Id']."</td>";
									echo "<td>";
									    if($UsrData['DiffLock']=="UNLOCK")
									    {
									        echo "<a class='deactAnchor' id='".$UsrData['Id']."' onclick='deacMinus(".$UsrData['Id'].")'>Deactive</a>";
									    }
								    echo "</td>";
								    echo "<td>";
	    							    if($UsrData['DiffLock']=="UNLOCK")
		   							    {
		    						        echo "<a href='Action.php?lockId=".$UsrData['Id']."'>UNLOCK</a>";
			    					    }
					    			    else
						    		    {
						    		        echo "<a href='Action.php?lockId=".$UsrData['Id']."'>LOCK</a>";
							    	    }
						        	echo "</td>";
									echo "<td>".round($diff)."</td>";
								echo "</tr>";
							}
							
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>