<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<!DOCTYPE html>
<html lang="en">
<?php include ("../Header/head.php"); ?>
<body class="skin-default fixed-layout">

<!-- Main wrapper - style you can find in pages.scss -->
<div id="main-wrapper">    
<?php include ("../Header/header.php"); ?>
<?php include ("../Header/left_menu.php"); ?>
<?php include ("../Header/right_sidebar.php"); ?>



        
<!-- Page wrapper  -->
<div class="page-wrapper">
<!-- Container fluid  -->
<div class="container-fluid">
<!-- Bread crumb and right sidebar toggle -->
<!--?php include ("../Header/breadcumb.php"); ?-->
<br><br>
<!-- End Bread crumb and right sidebar toggle -->
<!-- Start Page Content -->
<!-- ============================================================== -->
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title">My Network</h4>  <!--a href="team_geonology_view.php"> View In Desktop Mode></a-->
<div class="container">	
	<?php
	global $mysqli;

	$agent_id = $_GET['agent_id'];

	if ($agent_id != "")
	{
	    $user_id = $agent_id;
	}

	//Check user info
	$sql_user = "SELECT *
				 FROM user
				 Where id = '$user_id'
				 And status = '1'";
	$result_user = $mysqli-> query($sql_user);
	if ($result_user -> num_rows > 0)
	{
		$row_user = $result_user ->fetch_assoc();
		$user_id = $row_user['user_id'];
	}


	$sql = "SELECT U.*, WOI.order_full_total as package_price, WOI.order_items as order_items
	FROM user U
	Inner Join woocommerce_order_info WOI
	On U.ref_code = WOI.new_ref_code
	WHERE U.status = '1'
	AND WOI.status = '1'
    group by U.ref_code
	ORDER BY U.user_id";
	$result = $mysqli->query($sql);
	// Create an array to conatin a list of items and parents
	$menus = array
	(
		'items' => array(),
		'sponsor_ids' => array()
	);
	// Builds the array lists with data from the SQL result
	while($items = $result -> fetch_assoc())
	{
		// Create current menus item id into array
		$menus['items'][$items['user_id']] = $items;
		// Creates list of all items with children
		$menus['sponsor_ids'][$items['sponsor_id']][] = $items['user_id'];
	}
	?>
	
	<?php
	$sql_user = "SELECT * FROM user 
	WHERE user_id = '$user_id'
	And status='1'";
	$result_user = $mysqli-> query($sql_user);
	if ($result_user -> num_rows > 0)
	{
		$row_user = $result_user ->fetch_assoc();
		$ref_code = $row_user['ref_code'];
		$type_of_user = $row_user['type_of_user'];
		$name = $row_user['name'];
	}
	?>
	<br>
	<span style='background-color:#212529;color:white;padding-left:10px;padding-right:10px;padding-top:10px;padding-bottom:10px;border-radius:25px;'>
	<?php echo $ref_code; ?> | <?php echo $name; ?>
	<!--a href="add_new_user.php?sponsor_code=<?php echo $ref_code; ?>"><font color='white'>Register</font></a--></span><br><br>
	<?php 
	// Print all tree view menus 
	echo createTreeView($user_id, $menus);
	?>			
</div>


<?php
// function to create dynamic treeview menus
function createTreeView($sponsor_id, $menu) 
{
   global $mysqli;

   $html = "";
   if (isset($menu['sponsor_ids'][$sponsor_id])) 
   {
      $html .= "
      <ol class='tree'>";
       foreach ($menu['sponsor_ids'][$sponsor_id] as $itemId) 
	   {
          
          if(isset($menu['sponsor_ids'][$itemId]))
		  {
             $html .= "
             <li style='background-color:#212529;color:white;padding-left:25px;padding-right:10px;padding-top:10px;padding-bottom:10px;border-radius:25px;' >
			 <label for='subfolder2'><font size='2px'>
			 &nbsp; Member ID: ".$menu['items'][$itemId]['ref_code']."
			 | Name: ".$menu['items'][$itemId]['name']."
			 | Package: ".$menu['items'][$itemId]['order_items']."
			  <!--a href='add_new_user.php?sponsor_code=".$menu['items'][$itemId]['ref_code']."'><font color='white'>Register</font></a-->
			 | <a href='team_geonology.php?agent_id=".$menu['items'][$itemId]['id']."'><font color='white'>View More</font></a>
			 </font></label>
			 <input type='checkbox' name='subfolder2'/>";
             $html .= createTreeView($itemId, $menu);
             $html .= "</li>";
          }
		  if(!isset($menu['sponsor_ids'][$itemId])) 
		  {
             $html .= "<li style='background-color:#212529;color:white;padding-left:10px;padding-right:10px;padding-top:10px;padding-bottom:5px;border-radius:25px;' >
			 <label for='subfolder2'><font size='2px'>
			  &nbsp; Member ID: ".$menu['items'][$itemId]['ref_code']."
			 | Name: ".$menu['items'][$itemId]['name']."
			 | Package: ".$menu['items'][$itemId]['order_items']."
			  <!--a href='add_new_user.php?sponsor_code=".$menu['items'][$itemId]['ref_code']."'><font color='white'>Register</font></a-->
			 | <a href='team_geonology.php?agent_id=".$menu['items'][$itemId]['id']."'><font color='white'>View More</font></a>
			 </font></label>
			 <input type='checkbox' name='subfolder2'/>
			 </li>";
          }
       }
       $html .= "</ol>";
   }
   return $html;
}
?>



</div>
</div>                      
</div>
</div>
<!-- ============================================================== -->
<!-- End PAge Content -->

</div>
<!-- End Container fluid  -->
</div>
<!-- End Page wrapper  -->

<!-- Footer Start-->
<?php include ("../Footer/footer.php"); ?>
<!-- Footer End-->

</div>
<!-- End Wrapper -->
<!-- All Jquery -->
    

<!-- All Jquery Start-->
<?php include ("../Header/all_jquery.php"); ?>
<!-- All Jquery End-->

</body>

</html>