<?php
	include'session.php';
	include'../connection.php';
	include'../functions.php';
?>
<html>
<head>
	<title>Recharge By Lapu</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:96%;
			margin:0 2%;
			padding:5px 10px;
		}
		.div1 input, .div1 select{
			height:40px;
			max-width:160px;
			font-size:14px;
			padding:5px;
			border-radius:5px;
			border:solid #6b5127 1px;
			overflow:hidden;
			margin:10px 5px;
		}
		input[type=submit]{
			background:#6b5127;
			color:white;
			transition:1s;
			cursor:pointer;
		}
		input[type=submit]:hover{
			background:white;
			color:#6b5127;
			transition:1s;
		}
		.div2{
			background:#f7f7f7;
			width:100%;
			height:450px;
			float:left;
			text-align:center;
			padding:5px;
			overflow:auto;
		}
		.div2 table{
			width:98%;
			text-align:center;
			font-size:13px;
			border-collapse:collapse;
			padding:2px 5px;
			margin:1%;
		}
        .div2 table tr:nth-child(odd){background:#f6f0e8;}
        .div2 table tr:nth-child(even){background:white; }
		table th{
			background:#6b5127;
			color:white;
			height:35px;
			border:solid white 1px;
			padding:2px 5px;
		}
		table td{
			height:30px;
			border:solid black 1px;
			padding:2px 5px;
		}
		
        .div2 table tr:hover{background:gray; color:white;}
		#inpNum, #lapuValue, #value1, #value2{display:none;}
		#analysisData{margin-left:2%;}
		#analysisData table td{padding:2px 5px;}
		@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" id="contentDiv">
			<div class="head">
				<div class="title"><i class="fa fa-bars" onclick="hideMenu()" style="float:left; margin:10px;"></i><p>Recharge By Lapu :-</p></div>
				<div class="msg"><marquee><?php include'../msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			    <script>
			        function hideMenu()
			        {
			            if(document.getElementById("navHide").style.display!="none")
			            {
			                document.getElementById("navHide").style.display = "none";
			                document.getElementById("contentDiv").style.width = "100%";
			            }
			            else
			            {
			                document.getElementById("navHide").style.display = "inline";
			                document.getElementById("contentDiv").style.width = "82%";
			            }
			        }
			        
			        
			        function diffType()
			        {
			            var DiffType = document.getElementById("Diff").value;
			            
			            if(DiffType=="ALL")
			            {
			                document.getElementById("status").style.display= "inline";
			                document.getElementById("inpNum").style.display= "none";
			            }
			            else
			            {
			                document.getElementById("status").style.display= "none";
			                document.getElementById("inpNum").style.display= "inline";
			            }
			        }
			    </script>
			    <?php
			        $lapuNo    = $_GET['lapuNo'];
			        $orderId   = $_GET['rechId'];
			        $fDate     = $_GET['fDate'];
			        $lDate     = $_GET['lDate'];
			        $rechDate  = $_GET['rechDate'];
			        $OpCode    = $_GET['OpCode'];
			        
			        if(empty($OpCode))
			        {
			            $sqlQu = "";
			        }
			        elseif($OpCode==35)
			        {
			            $sqlQu = "AND OpCode=35";
			        }
			        else
			        {
			            $sqlQu = "AND OpCode!=35";
			        }
			        
			        
			        if(isset($_POST['submit']))
			        {
			            $fDateValue = $_POST['fdate'];
			            $lDateValue = $_POST['ldate'];
						$diffValue  = $_POST['DiffAmt'];
						$seller     = $_POST['seller'];
			        }
			        elseif($_GET['rechDate'])
			        {
			            $fDateValue=$rechDate;
			            $lDateValue=$rechDate;
			            $diffValue  = 0;
			        }
			        elseif($_GET['fDate'] && $_GET['lDate'])
			        {
			            $fDateValue=$fDate;
			            $lDateValue=$lDate;
			            $diffValue  = 0;
			        }
			        else
			        {
			            $fDateValue = $date;
			            $lDateValue = $date;
			            $diffValue  = 0;
			        }
			    ?>
				<form method="post">
					<div class="div1">
						<input type="date" name="fdate" id="date1" value="<?php echo $fDateValue; ?>">
						<input type="date" name="ldate" id="date2" value="<?php echo $lDateValue; ?>">
		                <input style="width:70px" type="number" name="DiffAmt" value="<?php echo $diffValue; ?>">
						<select name="status" id="status">
						    <option>SUCCESS</option>
						    <option>FAILED</option>
						    <option>PENDING</option>
						    <option>ALL</option>
						</select>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
				</form>
						<input type="text" value="0"  id="value1">
						<input type="text" value="0"  id="value2">
						<input type="button" value="Analysis"  onclick="analysisLapu()">
					</div>
				<script>
				    function setInpValue(Arg)
				    {
				        var inpVal1 = document.getElementById("value1").value;
				        var inpVal2 = document.getElementById("value2").value;
				        if(inpVal1 < 1)
				        {
				            document.getElementById("value1").value = Arg;
				        }
				        else if(inpVal2 < 1)
				        {
				            document.getElementById("value2").value = Arg;
				        }
				    }
				    
				    function analysisLapu()
				    {
				        var AjaxName  ="Ajax14";
				        var inpVal1   = document.getElementById("value1").value;
				        var inpVal2   = document.getElementById("value2").value;
				        var xmlhttpState = new XMLHttpRequest();
						xmlhttpState.open("GET","Ajax.php?AjaxName="+AjaxName+"&inpVal1="+inpVal1+"&inpVal2="+inpVal2, false)
						xmlhttpState.send(null);
						document.getElementById("analysisData").innerHTML = xmlhttpState.responseText;
				    }
				</script>
				<br><div id="analysisData"></div><br>
				<div class="div2">
					<table cellpadding="20" border="2"> 
						<tr>
							<th><i class="fa fa-check"></i></th>
							<th>Order Id</th>
							<th>Date Time</th>
							<th>Name</th>
							<th>Bal.</th>
							<th>Op</th>
							<th>Mobile</th>
							<th>Amt.</th>
							<th>RO</th>
							<th>Seller</th>
							<th>Lapu / API</th>
							<th>Api Bal</th>
							<th>Api Diff</th>
							<th>OpId</th>
							<th>Status</th>
						</tr>
						<?PHP
						    
							$queryRun="";
							if(isset($_POST['submit']))
							{
								$fdate   = $_POST['fdate'];
								$ldate   = $_POST['ldate'];
								$DiffAmt = $_POST['DiffAmt'];
								$status  = $_POST['status'];
								
								$sl=0;
								
								$DiffQu ="";
							    if($DiffAmt>0)
							    {
							        $DiffQu = "AND Diff>=$DiffAmt";
							    }
							    elseif($DiffAmt<0)
							    {
							        $DiffQu = "AND Diff<=$DiffAmt";
							    }
							    
							    $statusQu = "";
							    if($status!="ALL")
							    {
							        $statusQu = "AND Status='$status'";
							    }
							        
						        $quLapuTxn = "SELECT * FROM recharge WHERE Lapu='$lapuNo' $DiffQu AND Date>='$fdate' AND Date<='$ldate' $statusQu ORDER BY Id DESC";
							    $queryRun="true";
							}
		                    elseif($_GET['fDate'] && $_GET['lDate'])
			                {
			                    $fdate=$_GET['fDate'];
			                    $ldate=$_GET['lDate'];
            			        $quLapuTxn="SELECT * FROM recharge WHERE Lapu='$lapuNo' AND Status!='FAILED' AND Date>='$fdate' AND Date<='$ldate' ORDER BY Id DESC";
							    $queryRun="true";
			                }
		                    elseif($_GET['rechDate'])
			                {
			                    $fdate     = $_GET['rechDate'];
			                    $ldate     = $_GET['rechDate'];
            			        $quLapuTxn = "SELECT * FROM recharge WHERE Lapu='$lapuNo' AND Status!='FAILED' AND Date>='$fdate' AND Date<='$ldate' ORDER BY Id DESC";
							    $queryRun ="true";
			                }
							else
							{
							    $quLapuTxn="SELECT * FROM recharge WHERE Lapu='$lapuNo' ORDER BY Id DESC LIMIT 20";
							    $queryRun="true";
							}
							
							if($queryRun!="true")
							{
							    echo"<tr>";
							        echo"<td><h3>Data Not Found !</h3></td>";
							    echo"</tr>";
							}
							else
							{
							    $reLapuTxn=mysqli_query($con,$quLapuTxn);
								
								$totalRound500=0;
								$totalDiff=0;
								$totalRound1000=0;
								$totalAmount=0;
								$totalRO=0;
								while($dataLapuTxn=mysqli_fetch_array($reLapuTxn))
								{
									if($dataLapuTxn['Status']=="FAILED")
									{
										$color="red";
									}
									elseif($dataLapuTxn['Status']=="SUCCESS")
									{
										$color="green";
									}
									else
									{
										$color="blue";
									}
									
									if($dataLapuTxn['Id']==$orderId)
									{
									    $background="background:#F2BBBF";
									}
									else
									{
									    $background="";
									}
									$usrName=getName($dataLapuTxn['Userid']);
									if($dataLapuTxn['ApiBal']>1000 || $dataLapuTxn['ApiBal']==0)
									{
									    $colorBal="green";
									}
									else
									{
									    $colorBal="red";
									}
									if($dataLapuTxn['Id']==$orderId)
									{
									    $background="background:#F2BBBF";
									}
									else
									{
									    $background="";
									}
									$queryRun="false";
            			            echo"<tr style='".$background."'>";
										    echo "<td><input type='checkbox' value='".$dataLapuTxn['Id']."' id='".$dataLapuTxn['Id']."' onclick='setInpValue(this.value)'></td>";
										echo "<td><a href='check_txn_status.php?rechId=".$dataLapuTxn['Id']."' target='_blank'>".$dataLapuTxn['Id']."</a></td>";
										echo "<td>".$dataLapuTxn['Date']."<br>".$dataLapuTxn['Time']."</td>";
										echo "<td>".$usrName."</td>";
										echo "<td>".floor($dataLapuTxn['UsrBal'])."</td>";
										echo "<td>".$dataLapuTxn['OpName']."</td>";
										echo "<td>".$dataLapuTxn['Mobile']."</td>";
										echo "<td>".$dataLapuTxn['Amount']."</td>";
										echo "<td>".getRA($dataLapuTxn['R_offer'])."</td>";
										echo "<td style='font-size:10px;'>".getLON($dataLapuTxn['Lapu'])."<br>".getLFN($dataLapuTxn['Lapu'])."</td>";
										echo "<td>".$dataLapuTxn['Lapu']."</td>";
										echo "<td style='color:".$colorBal."; font-size:13px'><b>".floor($dataLapuTxn['ApiBal'])."</b></td>";
										echo "<td>".getRA($dataLapuTxn['Diff'])."</td>";
										echo "<td><textarea>".$dataLapuTxn['OpId']."</textarea></td>";
										echo "<td><p id='".$color."'>".$dataLapuTxn['Status']."</p></td>";
									echo"</tr>";
									$totalAmount=$totalAmount+$dataLapuTxn['Amount'];
									$totalRO=$totalRO+$dataLapuTxn['R_Offer'];
								}
							
							echo "<tr>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th>".$totalAmount."</th>";
							    echo "<th>".$totalRO."</th>";
							    echo "<th></th>";
							    echo "<th>".$totalRound500."</th>";
							    echo "<th>".$totalDiff."</th>";
							    echo "<th>".$totalRound1000."</th>";
							    echo "<th></th>";
							    echo "<th></th>";
							echo "</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>