<?php

header('Content-Type: application/json');

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(1);
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");
ini_set('set_time_limit', 0);
ini_set('memory_limit','1000000000000M');
date_default_timezone_set('Asia/Kolkata');


$data = json_decode(trim(file_get_contents("php://input")),true);


//var_dump($data['refer_id']);

$_REQUEST['referID'] = $data['referID'];
//$_REQUEST['referID'] = '205033984';


set_time_limit (30000);
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("mybooster");


function get_package_details($refer_id) {
	$sqlPackage 		= "SELECT sum(total_PV) as total_PV	 FROM `my_member_package` WHERE deleted=0 and `refer_id` = '".$refer_id."' and payment_status=1";
	$resPackage 		= mysql_query($sqlPackage);
	$rowPackage 		= mysql_fetch_assoc($resPackage);
	$totalPV		    = $rowPackage['total_PV'];


	$sqlPackage2 		= "SELECT date_entered FROM `my_member_package` WHERE `refer_id` = '" . $refer_id . "' and deleted=0 and payment_status=1 order by date_entered ASC Limit 1";
	$resPackage2 		= mysql_query($sqlPackage2);
	$rowPackage2		= mysql_fetch_assoc($resPackage2);
	$date_entered		= $rowPackage2['date_entered'];


	if(empty($date_entered) || $date_entered=="NULL")
		$date_entered = "N.A";



	if($totalPV == "" || $totalPV=="NULL") {
		$totalPV = 0;
	} else {
		$totalPV = sprintf("%0.2f", $totalPV);
	}

	$data = array();
	$data['totalPV'] = $totalPV;
	$data['date_entered'] = $date_entered;

	return $data;

	return 0;
}

function get_tree1( $referID, $i ) {

	$sql 		= "select id, name, last_name, mobile_number, email_address, refer_id, left_node, right_node from my_registration where sponsor_id = '$referID' order by date_entered limit 0, 10";
	$sqlResult 		= mysql_query($sql);
	$rowCnt 		= mysql_num_rows($sqlResult);
	
	if($rowCnt != 0) {
		//$finalData1['refer_id'] = $referID;
		$finalData1 = array();
		$sponsoredTo1 = array();
		$sponsoredTo2 = array();
		
		while($sqlRow 	= mysql_fetch_assoc($sqlResult)) {

			$i=$i+1;

			$referID = $sqlRow['refer_id'];
			if($i >= 160) { //nothing
				array_push( $sponsoredTo1, $sqlRow );
			} else {
				

				$result2 = get_tree1($referID, $i);
				if( count($result2) != 0 ) {
					/*
					$sponsoredTo1['id'] 			= $sqlRow['id'];
					$sponsoredTo1['name'] 			= $sqlRow['name'];
					$sponsoredTo1['mobile_number'] 	= $sqlRow['mobile_number'];
					$sponsoredTo1['email_address'] 	= $sqlRow['email_address'];
					$sponsoredTo1['refer_id'] 		= $sqlRow['refer_id'];
					$sponsoredTo1[] = $result2;
					*/
					$data = get_package_details($referID);
					$total_PV = $data['totalPV'];
					$date_entered = $data['date_entered'];
				    $sqlRow['total_PV']=$total_PV;
					$sqlRow['date_entered'] = $date_entered;
					$sqlRow['d'] =  $result2;
					array_push( $sponsoredTo1, $sqlRow);
					//$sponsoredTo1[] = $result2;	
					//array_push( $sponsoredTo1, $sponsoredTo2 );
				
				} else {
					$data = get_package_details($referID);
					$total_PV = $data['totalPV'];
					$date_entered = $data['date_entered'];
				    $sqlRow['total_PV']=$total_PV;
					$sqlRow['date_entered'] = $date_entered;
					array_push( $sponsoredTo1, $sqlRow);
				}
			}
		}
		//array_push($finalData1, $sponsoredTo1);
	}

	return $sponsoredTo1;
}

function get_tree( $referID ) {

	$sql 			= "select id, name, last_name, mobile_number, email_address, refer_id, left_node, right_node from my_registration where sponsor_id = '$referID' order by date_entered limit 0, 10";
	$sqlResult 		= mysql_query($sql);
	$rowCnt 		= mysql_num_rows($sqlResult);
	
	$i = 1;
	if($rowCnt != 0) {
		$finalData['refer_id'] 	= $referID;
		$sponsoredTo 			= array();
		$sponsoredFull			= array();
		$sponsoredTo['d'] = array();
		while($sqlRow 			= mysql_fetch_assoc($sqlResult)) {
			
			//array_push( $sponsoredTo, $sqlRow );
			$result1 = array();
			$sponsoredTo = array();
			$sponsoredTo['id'] 				= $sqlRow['id'];
			$sponsoredTo['name'] 			= $sqlRow['name'];
			$sponsoredTo['last_name'] 		= $sqlRow['last_name'];
			$sponsoredTo['mobile_number'] 	= $sqlRow['mobile_number'];
			$sponsoredTo['email_address'] 	= $sqlRow['email_address'];
			$sponsoredTo['refer_id'] 		= $sqlRow['refer_id'];
			$sponsoredTo['left_node'] 		= $sqlRow['left_node'];
			$sponsoredTo['right_node'] 		= $sqlRow['right_node'];
			//$sponsoredTo['date_entered'] 	= $sqlRow['date_entered'];


			$data = get_package_details($sqlRow['refer_id']);
			$total_PV = $data['totalPV'];
			$date_entered = $data['date_entered'];
			$sponsoredTo['total_PV']=$total_PV;
			$sponsoredTo['date_entered']=$date_entered;

			$referID = $sponsoredTo['refer_id'];
			$result1 = get_tree1($referID, $i);
			if( count($result1) != 0 ) {
				//print "<pre>result1: ".count($result1);
				//print_r($result1);
				//print "</pre>";
				$sponsoredTo['d'] = $result1;
				//array_push( $sponsoredTo['d'], $result1 );	
			}
			array_push($sponsoredFull, $sponsoredTo);
		}
	}
	return $sponsoredFull;
}

if($_REQUEST['referID'] == "") {
	$result['status'] = 0;
	$result['message'] = 'Please enter refer id';
	$result['data'] = array();

} else {

	$result['status'] = 1;
	$result['message'] = 'Success';

	//$referID = '59101719';
	$resultAll = get_tree($_REQUEST['referID']);

	//print "<pre>";
	//print_r($resultAll);
	//print "</pre>";	
	//exit;

	$result['data'] = $resultAll;
}

//print "<pre>";
//print_r($result);
//print "</pre>";
//exit;
$result1 = json_encode($result);

if ($result1)
    echo $result1;
else
    echo json_last_error_msg();

exit;

$result2 = json_decode($result1);



?>
