<?php
	include'connection.php';
	
	$usrInput = $_GET['usrInput'];
	function downline($parentId)
	{
		include'connection.php';
		
		$qu="SELECT * FROM MLM_Tree WHERE Userid='$parentId'";
		$re=mysqli_query($con,$qu);
		if(!empty($data=mysqli_fetch_array($re)))
		{
			$rightUsr = $data['RightSide'];
			$leftUsr = $data['LeftSide'];
			
			$parentUsr=["right" => $rightUsr, "left"=>$leftUsr];
		}
		else
		{
			$parentUsr="Blank";
		}
		
		return $parentUsr;
	}
	
	$user = $usrInput;
	
	if(empty($usrInput))
	{
		$user=$userid;
	}
	$usr = downline($user);
	if($usr=="Blank")
	{
		echo "<p style='color:red;'>Input Userid Not Available !</p>";
	}
	else
	{
		$leftDownline = $usr['left'];
		$rightDownline = $usr['right'];
		if(!empty($leftDownline))
		{	
			$nextLeftUsr = downline($leftDownline);
			$nextLeftDownloneR=$nextLeftUsr['right'];
			$nextLeftDownloneL=$nextLeftUsr['left'];
		}
		
		
		if(!empty($rightDownline))
		{
			$nextRightUsr = downline($rightDownline);
			$nextRightDownloneR=$nextRightUsr['right'];
			$nextRightDownloneL=$nextRightUsr['left'];
		}
		
		echo "<i class='fa fa-user'></i><span id='span1'>".$user."</span>";
		if(!empty($leftDownline) || !empty($rightDownline))
		{
			echo "<div class='vLine1'></div>";
			echo "<div class='hLine1'></div>";
		}
		if(!empty($leftDownline))
		{
			echo "<i class='fa fa-user' id='iconLeft1L'></i><span id='spanLeft1'>".$leftDownline."</span>";
		}
		if(!empty($rightDownline))
		{
			echo "<i class='fa fa-user' id='iconRight1R'></i><span id='spanRight1'>".$rightDownline."</span>";
		}
		if(!empty($nextLeftDownloneL) || !empty($nextLeftDownloneR))
		{
			echo "<div class='vLine2L'></div>";
			echo "<div class='vLine2R'></div>";
		}
		if(!empty($nextRightDownloneL) || !empty($nextRightDownloneR))
		{
			echo "<div class='hLine2L'></div>";
			echo "<div class='hLine2R'></div>";
		}
		if(!empty($nextLeftDownloneL))
		{
			echo "<i class='fa fa-user' id='iconLeft2L'></i><span id='spanLeft2L'>".$nextLeftDownloneL."</span>";
		}
		if(!empty($nextLeftDownloneR))
		{
			echo "<i class='fa fa-user' id='iconLeft2R'></i><span id='spanLeft2R'>".$nextLeftDownloneR."</span>";
		}
		if(!empty($nextRightDownloneL))
		{
			echo "<i class='fa fa-user' id='iconRight2L'></i><span id='spanRight2L'>".$nextRightDownloneL."</span>";
		}
		if(!empty($nextRightDownloneR))
		{
			echo "<i class='fa fa-user' id='iconRight2R'></i><span id='spanRight2R'>".$nextRightDownloneR."</span>";
		}
	}
?>