<?php
	include'session.php';
	include'connection.php';
	include'../Retailers/functions.php';
    
    $quDownline="SELECT * FROM MLM_Tree WHERE Userid='$userid'";
    $reDownline=mysqli_query($con,$quDownline);
    $dataDownline=mysqli_fetch_array($reDownline);
    $rightUsr=$dataDownline['RightSide'];
    $leftUsr=$dataDownline['LeftSide'];
    $rightCount=$dataDownline['RightCount'];
    $leftCount=$dataDownline['LeftCount'];
    
    $totalUsr=$rightCount+$leftCount
?>
<html>
<head>
	<title>Downline</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:100%;
			height:auto;
			margin:0;
			padding:5px 10px;
			float:left;
		}
		.div1 input, .div1 select{
			height:35px;
			width:auto;
			font-size:14px;
			padding:2px 10px;
			border-radius:5px;
			border:solid #6b5127 1px;
			overflow:hidden;
			margin:10px 10px 0 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:;
			width:100%;
			height:450px;
			float:left;
			text-align:center;
			padding:0px;
			overflow:auto;
		}
		table{
			width:98%;
			text-align:center;
			font-size:13px;
			border-collapse:collapse;
			padding:2px 10px;
			margin:1%;
		}
		table th{
			background:#6b5127;
			color:white;
			height:30px;
			border:solid white 1px;
			padding:2px 5px;
		}
		table td{
			background:white;
			height:30px;
			border:solid black 1px;
			padding:2px 5px;
		}
		#inpDataList{display:none;}
		.fa-check, .fa-ban{font-size:20px;}
		#tbl1{width:40%; float:left; }
		@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;
			}
			
		#tbl1{width:90%; margin:0 5%; }
		}
	</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>Downline :-</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 USER")
			            {
			                document.getElementById("inpDataList").style.display = "inline";
			            }
			            else
			            {
			                document.getElementById("inpDataList").style.display = "none";
			            }
			        }
			        
			        function blockUsr(Arg)
			        {
			            var inpid = Arg;
			            var usrId = 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?usrId="+usrId+"&inpid="+inpid, false)
							xmlhttpState.send(null);
							document.getElementById("ip"+inpid).innerHTML = xmlhttpState.responseText;
			            }	
			        }
			        
			        function unblockUsr(Arg)
			        {
			            var inpid = Arg;
			            var usrId = 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?usrId="+usrId+"&inpid="+inpid, false)
							xmlhttpState.send(null);
							document.getElementById("ip"+inpid).innerHTML = xmlhttpState.responseText;
			            }
			        }
			    </script>
				<div class="div1" id="downlinePlace">
				    <form method="post">
					    <table cellpadding="20" border="2" id="tbl1">
					        <tr><th>Total Downline</th><th>Left Side</th><th>Right Side</th></tr>
					        <tr><td><?php echo $totalUsr; ?></td><td><?php echo $leftCount; ?></td><td><?php echo $rightCount; ?></td></tr>
					    </table>
					    <SELECT NAME="userType" id="UsrType" ONCHANGE="searchAnyone()" >
					    <?php
					        if(isset($_POST['submit']))
					        {
					            $userType = $_POST['userType'];
					                if($userType=="ALL")
								    {
								        $selected1 ="SELECTED";
								    }
								    elseif($userType=="ANYONE USER")
								    {
								        $selected3 ="SELECTED";
								    }
								    elseif($userType=="RIGHT SIDE")
								    {
								        $selected4 ="SELECTED";
								    }
								    elseif($userType=="LEFT SIDE")
								    {
								        $selected5 ="SELECTED";
								    }
					        }
					        echo "<OPTION ".$selected1.">ALL</OPTION>
					        <OPTION ".$selected3.">ANYONE USER</OPTION>
					        <OPTION ".$selected4.">RIGHT SIDE</OPTION>
					        <OPTION ".$selected5.">LEFT SIDE</OPTION>";
					   ?>
					    </SELECT>
						<select id="inpDataList" name="retailer">
						    <option>--Select Retailer--</option>
						    <?php
						        $quGetRet="SELECT * FROM MLM_Users ORDER BY Name ASC";
						        $reGetRet=mysqli_query($con,$quGetRet);
						        while($dataGetRet=mysqli_fetch_array($reGetRet))
						        {
						            if("true"==checkChild($userid,$dataGetRet['Userid']))
						            {
						                echo "<option value='".$dataGetRet['Userid']."'>".$dataGetRet['Name']." - ".$dataGetRet['Mobile']."</option>";
						            }
						        }
						    ?>
						</select>
						<input type="submit" name="submit" value="Submit"  onclick='showLoader()'>
				    </form>
				</div>
				<div class="div2" id="downlinePlace">
					<table cellpadding="20" border="2"> 
						<tr>
							<th>Sl.</th>
							<th>Join Date</th>
							<th>User</th>
							<th>Mobile</th>
							<th>Rank</th>
							<th>Txn</th>
							<th>Left</th>
							<th>Right</th>
							<th>Parent User</th>
							<th>Sponsor User</th>
						</tr>
						<?php	
						    
								$sl=0;
								$limit=10000000;
								if(isset($_POST['submit']))
								{
								    $userType = $_POST['userType'];
								    $retailer = $_POST['retailer'];
								    
								    if($userType=="ALL" || $userType=="RIGHT SIDE" || $userType=="LEFT SIDE")
								    {
								        $UsrQu="SELECT * FROM MLM_Users ORDER BY Id ASC";
								    }
								    elseif($userType=="ANYONE USER")
								    {
								        $UsrQu="SELECT * FROM MLM_Users WHERE Userid='$retailer'";
								    }
								}
								else
								{
								    $UsrQu="SELECT * FROM MLM_Users ORDER BY Id DESC";
								    $limit=10;
								}
								$uplineId=$userid;
								$UsrResult=mysqli_query($con,$UsrQu);
								while($UsrData=mysqli_fetch_array($UsrResult))
								{
								    
								    if($userType=="RIGHT SIDE")
								    {
								        if($rightUsr==$UsrData['Userid'])
								        {
								            $flag=1;
								        }
								        if($sl>0)
								        {
								            $uplineId=$rightUsr;
								            $flag=1;
								        }
								    }
								    elseif($userType=="LEFT SIDE")
								    {
								        if($leftUsr==$UsrData['Userid'])
								        {
								            $flag=1;
								        }
								        if($sl>0)
								        {
								            $uplineId=$leftUsr;
								            $flag=1;
								        }
								    }
								    else
								    {
								        
								            $flag=1;
								    }
								    
								    if("true"==checkChild($uplineId,$UsrData['Userid']) && $sl<$limit && $flag==1)
								    {
								        $usr=$UsrData['Userid'];
								        $quSide="SELECT * FROM MLM_Tree WHERE Userid=$usr";
								        $reSide=mysqli_query($con,$quSide);
								        $dataSide=mysqli_fetch_array($reSide);
								        
									    $sl=$sl+1;
    									echo "<tr>";
	    									echo "<td>".$sl."<input style='display:none' type='hide' id='".$sl."' value='".$UsrData['Userid']."'></td>";
		    								echo "<td>".$UsrData['Date']."</td>";
			    							echo "<td>".$UsrData['Name']." [".$usr."]</td>";
			    							echo "<td>".$UsrData['Mobile']."</td>";
		    								echo "<td>".$UsrData['Id_Status']."</td>";
		    								echo "<td>".$UsrData['TotalTxn']."</td>";
		    								echo "<td>".$dataSide['LeftCount']."</td>";
		    								echo "<td>".$dataSide['RightCount']."</td>";
		    								echo "<td>".getName($UsrData['Parent_Id'])." [".$UsrData['Parent_Id']."]</td>";
		    								echo "<td>".getName($UsrData['SponsorId'])." [".$UsrData['SponsorId']."]</td>";
							    		echo "</tr>";
								    }
								}
								
								    if($sl<1)
								    {   
    									echo "<tr>";
	    									echo "<td colspan='10'><h3 style='color:red'>Data Not Found !</h3></td>";
							    		echo "</tr>";
								    }
							
						?>
					</table>
				</div>
			</div>
		</div>
	</div>
	<?php
		require'footer.php';
	?>
</body>
</html>