<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
?>
<html>
<head>
	<title>Recharge By Seller</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;
		}
		
		#inpNum{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" id="contentDiv">
			<div class="head">
				<div class="title"><i class="fa fa-bars" onclick="hideMenu()" style="float:left; margin:10px;"></i><p>Recharge By Seller :-</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 commType()
			        {
			            var commType = document.getElementById("commTypeInp").value;
			            
			            if(commType=="OTHER")
			            {
			                document.getElementById("inpNum").style.display= "inline";
			            }
			            else
			            {
			                document.getElementById("inpNum").style.display= "none";
			            }
			        }
			        
			        function commVal()
			        {
			            var SellerId = document.getElementById("Seller").value;
			            var xmlhttpSponser = new XMLHttpRequest();
			    	    xmlhttpSponser.open("GET","ajax_commValueBySeller.php?SellerId="+SellerId, false)
					    xmlhttpSponser.send(null);
					    document.getElementById("CommInp").value = xmlhttpSponser.responseText;
				    }
				        
			    </script>
			    <?php
			        $lapuNo=$_GET['lapuNo'];
			        $orderId=$_GET['rechId'];
			        $rechDate=$_GET['rechDate'];
			        
			        if(isset($_POST['submit']))
			        {
			            $fDateValue=$_POST['fdate'];
			            $lDateValue=$_POST['ldate'];
						$seller = $_POST['seller'];
			        }
			        else
			        {
			            $fDateValue=$date;
			            $lDateValue=$date;
			        }
			    ?>
				<div class="div1">
					<form method="post" action="export.php">
						<input type="date" name="date" id="date1" value="<?php echo $date; ?>">
						<select name="status" id="status">
						    <option>SUCCESS</option>
						    <option>FAILED</option>
						    <option>PENDING</option>
						    <option>ALL</option>
						</select>
						<select name="seller" onchange="commVal()" id="Seller">
		                    <option>--SELECT SELLER--</option>
						    <?php
						        $quSeller="SELECT * FROM Seller ORDER BY Name ASC";
						        $reSeller=mysqli_query($con,$quSeller);
						        while($dataSeller=mysqli_fetch_array($reSeller))
						        {
						            echo "<option>".$dataSeller['Name']."</option>";
						        }
						    ?>
						</select>
						<input type="text" name="commValue" id="CommInp" Placeholder="Enter Commission Value..">
						<input type="submit" name="export" value="Export To Excel">
					</form>
				</div>
				<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; ?>">
						<select name="seller">
		                    <option>--SELECT SELLER--</option>
						    <?php
						        $quSeller="SELECT * FROM Seller ORDER BY Name ASC";
						        $reSeller=mysqli_query($con,$quSeller);
						        while($dataSeller=mysqli_fetch_array($reSeller))
						        {
						            echo "<option>".$dataSeller['Name']."</option>";
						        }
						    ?>
						</select>
						<select name="opComm" id="commTypeInp" onclick="commType()">
		                    <option value="1029">AIRTEL(1029)</option>
		                    <option value="1033.15">JIO(1033.15)</option>
		                    <option value="1030" >VODA(1030)</option>
		                    <option >OTHER</option>
						</select>
						<input type="number" name="opCommOther" id="inpNum" placeholder="Enter Comm Value">
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
					</div>
				</form>
				<div class="div2">
					<table cellpadding="20" border="2"> 
						<tr>
							<th>From</th>
							<th>To</th>
							<th>Seller</th>
							<th>Lapu No.</th>
							<th>Payble Amount</th>
							<th>Real Amt.</th>
							<th>Extra.</th>
							<th>Status</th>
						</tr>
						<?PHP
							if(isset($_POST['submit']))
							{
								$fdate = $_POST['fdate'];
								$ldate = $_POST['ldate'];
								$seller = $_POST['seller'];
								$opCommOther = $_POST['opCommOther'];
								$opComm = $_POST['opComm'];
								if($opComm=="OTHER")
								{
								    $opCommValue = $opCommOther;
								}
								else
								{
								    $opCommValue = $opComm;
								}
								
								if($opComm==1029)
								{
								    $RefillValue=500;
								}
								else
								{
								    $RefillValue=1000;
								}
								$sl=0;
							    $AllTotalRound1000=0;
							    $totalAmount=0;
							    $totalAllExtra=0;
							    $quLapu="SELECT * FROM myapi WHERE Seller='$seller'";
							    $reLapu=mysqli_query($con,$quLapu);
							    while($dataLapu=mysqli_fetch_array($reLapu))
							    {
							        $lapuNo=$dataLapu['LapuNumber'];
							        $lapuAmount=0;
							        $totalRound1000=0;
							        $totalExtra=0;
							        
							        $quRech="SELECT * FROM recharge WHERE LapuNo='$lapuNo' AND Status='SUCCESS' AND Rechargedate>='$fdate' AND Rechargedate<='$ldate'";
							        $reRech=mysqli_query($con,$quRech);
							        while($dataRech=mysqli_fetch_array($reRech))
							        {   
								        $lapuAmount=$lapuAmount+$dataRech['Diff'];
								        
								        if($dataRech['Diff']>50)
									    {
									        if($dataRech['Diff']<$RefillValue)
									        {
									            $extra=floor($dataRech['Diff']);
									        }
									        else
									        {
									            
									            $realDiff=$dataRech['Diff']*1000/$opCommValue;
									            $round1000=floor($realDiff/$RefillValue)*$RefillValue;
									            $totalRound1000=$totalRound1000+$round1000;
									            $realBal=$round1000*$opCommValue/1000;
									            $extra=floor($dataRech['Diff']-$realBal);
									            
									        }
									        //echo $lapuNo." -> ".$round1000." | ".$dataRech['Diff']." - ".$realBal." = ".floor($extra)." | ".$totalExtra."<br>";
									        $totalExtra=$totalExtra+$extra;
									    }
							        }
							        
							        echo"<tr>";
    								    echo "<td>".$fdate."</td>";
    								    echo "<td>".$ldate."</td>";
    								    echo "<td>".$seller."</td>";
    								    echo "<td><a href='recharge_by_lapu.php?lapuNo=".$lapuNo."&fDate=".$fdate."&lDate=".$ldate."' target='_blank'>".$lapuNo."</a></td>";
    								    echo "<td>".$totalRound1000."</td>";
    								    echo "<td>".$lapuAmount."</td>";
    								    echo "<td>".floor($totalExtra)."</td>";
    								    echo "<td>".$status."</td>";
							        echo"</tr>";
							        
							        $AllTotalRound1000=$AllTotalRound1000+$totalRound1000;
							        $totalAmount=$totalAmount+$lapuAmount;
							        $totalAllExtra=$totalAllExtra+$totalExtra;
							    }   
					
							
							echo "<tr>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th></th>";
							    echo "<th>".$AllTotalRound1000."</th>";
							    echo "<th>".$totalAmount."</th>";
							    echo "<th>".$totalAllExtra."</th>";
							    echo "<th></th>";
							echo "</tr>";
							}
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>