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

?>
<html>
<head>
	<title>Lapu List</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%;
			margin:0 1%;
			padding:10px;
		}
		.div1 input, .div1 select{
			height:35px;
			width:200px;
			font-size:15px;
			padding:2px 10px;
			border-radius:5px;
			border:solid #6b5127 1px;
			overflow:hidden;
			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%;
			float:left;
			text-align:center;
			padding:10px 20px;
			overflow:auto;
		}
		.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:white;
			height:35px;
			border:solid black 1px;
			padding:2px 5px;
		}
		#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>Lapu List :-</p></div>
				<div class="msg"><marquee><?php include'msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			    <script>
			        function searchAnyone()
			        {
			            var UsrType = document.getElementById("UsrType").value;
			            if(UsrType=="ANYONE LAPU")
			            {
			                document.getElementById("lapuList").style.display = "inline";
			                document.getElementById("oper").style.display = "none";
			                document.getElementById("seller").style.display = "none";
			            }
			            else
			            {
			                document.getElementById("lapuList").style.display = "none";
			                document.getElementById("oper").style.display = "inline";
			                document.getElementById("seller").style.display = "inline";
			            }
			        }
			        
			        function blockUsr(Arg)
			        {
			            var inpid = Arg;
			            var sellerId = document.getElementById(inpid).value;
			            var confrim_rslt = confirm("Are You Sure To Do Block This User ?");
			            
			            if(confrim_rslt==true)
			            {
			                var xmlhttpState = new XMLHttpRequest();
							xmlhttpState.open("GET","ajax_block_usr.php?sellerId="+sellerId+"&inpid="+inpid, false)
							xmlhttpState.send(null);
							document.getElementById("ip"+inpid).innerHTML = xmlhttpState.responseText;
			            }	
			        }
			        
			        function unblockUsr(Arg)
			        {
			            var inpid = Arg;
			            var sellerId = document.getElementById(inpid).value;
			            var confrim_rslt = confirm("Are You Sure To Do Unblock This User ?");
			            
			            if(confrim_rslt==true)
			            {
			                var xmlhttpState = new XMLHttpRequest();
							xmlhttpState.open("GET","ajax_unblock_usr.php?sellerId="+sellerId+"&inpid="+inpid, false)
							xmlhttpState.send(null);
							document.getElementById("ip"+inpid).innerHTML = xmlhttpState.responseText;
			            }
			        }
			    </script>
				<form method="post">
					<div class="div1">
					    <SELECT NAME="userType" id="UsrType" ONCHANGE="searchAnyone()" >
					    <?php
					        if(isset($_POST['submit']))
					        {
					            $userType = $_POST['userType'];
					                if($userType=="ALL")
								    {
								        $selected1 ="SELECTED";
								    }
								    elseif($userType=="ANYONE LAPU")
								    {
								        $selected2 ="SELECTED";
								    }
					        }
					        echo "<OPTION ".$selected1.">ALL</OPTION>
					        <OPTION ".$selected2.">ANYONE LAPU</OPTION>";
					   ?>
					    </SELECT>
						<select name="seller" id="seller">
						    <option>ALL</option>
						    <?php
						        $quGetRet="SELECT * FROM Seller";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            echo "<option value='".$dataGetRet['Name']."'>".$dataGetRet['Name']." - ".$dataGetRet['Firm']."</option>";
						        }
						        
						        
						    ?>
						</select>
					    <select name="op" id="oper">
					        <option>ALL</option>
					        <?php
					            $quOp="SELECT * FROM operator WHERE Active='0'";
					            $reOp=mysqli_query($con,$quOp);
					            while($dataOp=mysqli_fetch_array($reOp))
					            {
					                echo "<option>".$dataOp['Operator']."</option>";
					            }
					        ?>
					    </select>
					    <input type="text" list="inpDataList" name="lapuNo" id="lapuList">
						<datalist id="inpDataList" >
						    <?php
						        $quGetRet="SELECT * FROM myapi";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            echo "<option value='".$dataGetRet['LapuNumber']."'>".$dataGetRet['LapuNumber']." - ".$dataGetRet['Seller']."</option>";
						        }
						    ?>
						</datalist>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div2" id="downlinePlace">
					<table cellpadding="20" border="2"> 
						<tr>
							<th>Sl.</th>
							<th>ID</th>
							<th>Opearor</th>
							<th>Seller</th>
							<th>Address</th>
							<th>Lapu No.</th>
							<th>Last Rech Bal</th>
							<th>Lapu Bal</th>
							<th>Last Update</th>
							<th>Order Id</th>
							<th colspan='5'>Action</th>
						</tr>
						<?php
		                        $resultdate=getBackDate($date,7);
								$sl=0;
								if(isset($_POST['submit']))
								{
								    $userType = $_POST['userType'];
								    $seller = $_POST['seller'];
								    $op = $_POST['op'];
								    $lapuNo = $_POST['lapuNo'];
								    
								    if($userType=="ALL")
								    {
								        if($seller=="ALL")
								        {
								            if($op=="ALL")
								            {
								                $UsrQu="SELECT * FROM myapi ORDER BY Seller";
								            }
								            else
								            {
								                $UsrQu="SELECT * FROM myapi WHERE Operator='$op' ORDER BY Seller";
								            }
								        }
								        else
								        {
								            if($op=="ALL")
								            {
								                $UsrQu="SELECT * FROM myapi WHERE Seller='$seller' ORDER BY Seller";
								            }
								            else
								            {
								                $UsrQu="SELECT * FROM myapi WHERE Seller='$seller' AND Operator='$op' ORDER BY Seller";
								            }
								        }
								    }
								    elseif($userType=="ANYONE LAPU")
								    {
								        $UsrQu="SELECT * FROM myapi WHERE LapuNumber='$lapuNo'";
								    }
								}
								else
								{
								    $date1=date_create($date);
		                            date_sub($date1,date_interval_create_from_date_string("2 days"));
		                            $resultdate=date_format($date1,"Y-m-d");
		
								    $UsrQu="SELECT * FROM myapi WHERE LastUpdate<='$resultdate' AND LapuBal>10 ORDER BY floor(LapuBal) DESC LIMIT 10";
								    
								}
								$UsrResult=mysqli_query($con,$UsrQu);
								while($UsrData=mysqli_fetch_array($UsrResult))
								{
									$sl=$sl+1;
									$lapuNo=$UsrData['LapuNumber'];
									
									$quLastSuccess="SELECT ApiBal FROM recharge WHERE Status='SUCCESS' AND LapuNo='$lapuNo' ORDER BY Id DESC";
									$relastsuccessbal=mysqli_query($con,$quLastSuccess);
									$datalastsuccessbal=mysqli_fetch_array($relastsuccessbal);
									$lastRechargeBal=$datalastsuccessbal['ApiBal'];
									
									echo "<tr>";
										echo "<td>".$sl."<input style='display:none' type='hide' id='".$sl."' value='".$UsrData['Id']."'></td>";
										echo "<td>".$UsrData['Id']."</td>";
										echo "<td>".$UsrData['Seller']."</td>";
										echo "<td>".$UsrData['Operator']."</td>";
										echo "<td>".$UsrData['Address']."</td>";
										echo "<td>".$UsrData['LapuNumber']."</td>";
										echo "<td>".$lastRechargeBal."</td>";
										echo "<td>".floor($UsrData['LapuBal'])."</td>";
										echo "<td>".$UsrData['LastUpdate']."</td>";
										echo "<td>".$UsrData['OrderId']."</td>";
										echo "<td><a href='Action.php?lapuId=".$UsrData['Id']."&lapuNo=".$UsrData['LapuNumber']."'>Bal Nil</a></td>";
									echo "</tr>";
								}
							
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>