<?php
	include'session.php';
	include'../connection.php';
	include'../functions.php';
?>
<html>
<head>
	<title>Recharge Report</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:1%;
			padding:5px 10px;
		}
		.div1 input, .div1 select{
			height:40px;
			width:170px;
			font-size:15px;
			padding:5px 10px;
			border-radius:3px;
			border:solid #6b5127 1px;
			overflow:hidden;
			margin:5px 10px;
		}
		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%;
			max-height:400px;
			float:left;
			text-align:center;
			padding:10px;
			overflow:auto;
		}
		.div2 table{
			width:100%;
			text-align:center;
			font-size:15px;
			border-collapse:collapse;
			padding:2px 10px;
		}
		table th{
			background:#6b5127;
			color:white;
			height:40px;
			border:solid white 1px;
			padding:2px 5px;
		}
		table td{
			background:white;
			height:40px;
			border:solid black 1px;
			padding:2px 5px;
		}
		
		#account,#orderid,#partnerid,#exp{display:none;}
		
		@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>Recharge Report :-</p></div>
				<div class="msg"><marquee><?php include'../msg.php'; ?></marquee></msg>
			</div><hr>
			<div class="data-content">
			    <script>
			        function searchType()
			        {
			            var searchType = document.getElementById("sarchType").value;
			            
			            if(searchType=="DATEWISE")
			            {
			                document.getElementById("date1").style.display= "inline";
			                document.getElementById("date2").style.display= "inline";
			                document.getElementById("status").style.display= "inline";
			                document.getElementById("account").style.display= "none";
			                document.getElementById("orderid").style.display= "none";
			                document.getElementById("partnerid").style.display = "none";
			                document.getElementById("sbmt").style.display = "inline";
			                document.getElementById("exp").style.display = "none";
			            }
			            else if(searchType=="ACCOUNT_NO")
			            {
			                document.getElementById("date1").style.display= "none";
			                document.getElementById("date2").style.display= "none";
			                document.getElementById("status").style.display= "none";
			                document.getElementById("account").style.display= "inline";
			                document.getElementById("orderid").style.display= "none";
			                document.getElementById("partnerid").style.display = "none";
			                document.getElementById("sbmt").style.display = "inline";
			                document.getElementById("exp").style.display = "none";
			            }
			            else if(searchType=="ORDER_ID")
			            {
			                document.getElementById("date1").style.display = "none";
			                document.getElementById("date2").style.display = "none";
			                document.getElementById("status").style.display = "none";
			                document.getElementById("account").style.display = "none";
			                document.getElementById("orderid").style.display = "inline";
			                document.getElementById("partnerid").style.display = "none";
			                document.getElementById("sbmt").style.display = "inline";
			                document.getElementById("exp").style.display = "none";
			            }
			            else if(searchType=="PARTNER_ID")
			            {
			                document.getElementById("date1").style.display = "none";
			                document.getElementById("date2").style.display = "none";
			                document.getElementById("status").style.display = "none";
			                document.getElementById("account").style.display = "none";
			                document.getElementById("orderid").style.display = "none";
			                document.getElementById("partnerid").style.display = "inline";
			                document.getElementById("sbmt").style.display = "inline";
			                document.getElementById("exp").style.display = "none";
			            }
			            else if(searchType=="DOWNLOAD")
			            {
			                document.getElementById("date1").style.display= "inline";
			                document.getElementById("date2").style.display= "inline";
			                document.getElementById("status").style.display= "none";
			                document.getElementById("account").style.display= "none";
			                document.getElementById("orderid").style.display= "none";
			                document.getElementById("partnerid").style.display = "none";
			                document.getElementById("sbmt").style.display = "none";
			                document.getElementById("exp").style.display = "inline";
			            }
			        }
			        
			        function exportData()
			        {
			            var date1 = document.getElementById("date1").value;
			            var date2 = document.getElementById("date2").value;
			            
			            var xmlhttpSponser = new XMLHttpRequest();
						xmlhttpSponser.open("GET","export.php?date1="+date1+"&date2="+date2, false)
						xmlhttpSponser.send(null);
				        
			        }
			    </script>
			    <?php
			    if(isset($_POST['export']))
			    {
			        $action="action='export_recharge.php'";
			    }
			    ?>
				<form method="post" <?php echo $action; ?>>
					<div class="div1">
						<select name="sarchType" onchange="searchType()" id="sarchType">
						    <option>DATEWISE</option>
						    <option>ACCOUNT_NO</option>
						    <option>ORDER_ID</option>
						    <option>PARTNER_ID</option>
						    <option>DOWNLOAD</option>
						</select>
						<input type="date" name="fdate" id="date1" value="<?php echo $date; ?>">
						<input type="date" name="ldate" id="date2" value="<?php echo $date; ?>">
						<input type="number" name="account" id="account" placeholder="Mobile/DTH No....">
						<input type="number" name="orderid" id="orderid" placeholder="Order Id....">
						<input type="number" name="Partnerid" id="partnerid" placeholder="API Partner Id....">
						<select name="status" id="status">
						    <option>ALL</option>
						    <option>SUCCESS</option>
						    <option>FAILED</option>
						    <option>PENDING</option>
						</select>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()' id="sbmt">
						<input type="submit" name="export" value="Export To Excel"  id="exp">
				</form>
				</div>
				<div class="div2">
					<table cellpadding="20" border="2"> 
						<tr>
							<th>Order Id</th>
							<th>Partner Id</th>
							<th>Date</th>
							<th>Time</th>
							<th>Operator</th>
							<th>Mobile/DTH</th>
							<th>Amount</th>
							<th>Operator Id</th>
							<th>Status</th>
						</tr>
						<?PHP
							if(isset($_POST['submit']))
							{
								$sarchType = $_POST['sarchType'];
								$fdate     = $_POST['fdate'];
								$ldate     = $_POST['ldate'];
								$status    = $_POST['status'];
								$account   = $_POST['account'];
								$orderid   = $_POST['orderid'];
								$Partnerid = $_POST['Partnerid'];
								
								$sl=0;
								if($sarchType=="DOWNLOAD")
								{
								    $query = "SELECT * FROM recharge WHERE Userid='$userid' AND Date>='$fdate' AND Date<='$ldate'";
                                    $result = mysqli_query($con, $query);
                                    if(mysqli_num_rows($result) > 0)
                                    {
                                        $output .= '<table class="table" Borders="All Borders" cellpadding="10">  
                                        <tr>  
                                            <th>Date</th>  
                                            <th>Time</th> 
                                            <th>Recharge Id</th>  
                                            <th>API Partner Id</th>  
                                            <th>Mobile No.</th>  
                                            <th>Operator</th>  
                                            <th>Amount</th>    
                                            <th>Status</th>
                                            <th>Op Id</th>
                                        </tr>';
                                        while($row = mysqli_fetch_array($result))
                                        {
                                            $output .= ' <tr>  
                                            <td>'.$row["Date"].'</td>  
                                            <td>'.$row["Time"].'</td>
                                            <td>'.$row["Id"].'</td>  
                                            <td>'.$row["Partnerid"].'</td>  
                                            <td>'.$row["Mobile"].'</td>  
                                            <td>'.$row["OpName"].'</td>  
                                            <td>'.$row["Amount"].'</td>
                                            <td>'.$row["Status"].'</td>
                                            <td>'.$row["OpId"].'</td>
                                            </tr>';
                                        }
                                        $output .= '</table>';
                                        header('Content-Type: application/xls');
                                        header('Content-Disposition: attachment; filename=download.xls');
                                        echo $output;
                                    }
                                    else
                                    {
                                        echo "<h2 style='color:red;'>Data Not Found !</h2>";
                                    }
								}
								elseif($sarchType=="DATEWISE")
								{
								    if($status=="ALL")
								    {
								        $quPayment="SELECT * FROM recharge WHERE Userid='$userid' AND Date>='$fdate' AND Date<='$ldate'";
								    }
								    else
								    {
								        $quPayment="SELECT * FROM recharge WHERE Userid='$userid' AND Status='$status' AND Date>='$fdate' AND Date<='$ldate'";
								    }
								}
								elseif($sarchType=="ACCOUNT_NO")
								{
								    $quPayment="SELECT * FROM recharge WHERE Mobile='$account' ORDER BY Id DESC";
								}
								elseif($sarchType=="ORDER_ID")
								{
								    $quPayment="SELECT * FROM recharge WHERE Id='$orderid'";
								}
								elseif($sarchType=="PARTNER_ID")
								{
								    $quPayment="SELECT * FROM recharge WHERE Partnerid='$Partnerid'";
								}
								
								$rePayment=mysqli_query($con,$quPayment);
								while($dataPayment=mysqli_fetch_array($rePayment))
								{
									if($dataPayment['Status']=="FAILED")
									{
										$color="red";
									}
									elseif($dataPayment['Status']=="SUCCESS")
									{
										$color="green";
									}
									else
									{
										$color="blue";
									}
									echo"<tr>";
										echo "<td>".$dataPayment['Id']."</td>";
										echo "<td>".$dataPayment['Partnerid']."</td>";
										echo "<td>".$dataPayment['Date']."</td>";
										echo "<td>".$dataPayment['Time']."</td>";
										echo "<td>".$dataPayment['OpName']."</td>";
										echo "<td>".$dataPayment['Mobile']."</td>";
										echo "<td>".$dataPayment['Amount']."</td>";
										echo "<td>".$dataPayment['OpId']."</td>";
										echo "<td><p id='".$color."'>".$dataPayment['Status']."</p></td>";
									echo"</tr>";
								}
							}
							else
							{
								$sl=0;
								$quPayment="SELECT * FROM recharge WHERE Userid='$userid' ORDER BY Id DESC LIMIT 10";
								$rePayment=mysqli_query($con,$quPayment);
								while($dataPayment=mysqli_fetch_array($rePayment))
								{
									if($dataPayment['Status']=="FAILED")
									{
										$color="red";
									}
									elseif($dataPayment['Status']=="SUCCESS")
									{
										$color="green";
									}
									else
									{
										$color="blue";
									}
									echo"<tr>";
										echo "<td>".$dataPayment['Id']."</td>";
										echo "<td>".$dataPayment['Partnerid']."</td>";
										echo "<td>".$dataPayment['Date']."</td>";
										echo "<td>".$dataPayment['Time']."</td>";
										echo "<td>".$dataPayment['OpName']."</td>";
										echo "<td>".$dataPayment['Mobile']."</td>";
										echo "<td>".$dataPayment['Amount']."</td>";
										echo "<td>".$dataPayment['OpId']."</td>";
										echo "<td><p id='".$color."'>".$dataPayment['Status']."</p></td>";
									echo"</tr>";
								}
							}
							$count=mysqli_num_rows($rePayment);
							if($count==0)
							{
							    echo"<tr>";
									echo "<td colspan='10' style='color:red'><b>Data Not Found !</b></td>";
								echo"</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>