<?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");

date_default_timezone_set('Asia/Kolkata');

set_time_limit (30000000000000000000000000000000000000000);
ob_start();
//ini_set('memory_limit','2048444M');

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


//$data['refer_id'] 			 = '909378288';
//$data['tree_from_refer_id']  = '259824700';
//$data['tree_from_refer_id']  = '123123629';

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

$_REQUEST['referID'] = $data['refer_id'];
$_REQUEST['tree_from_refer_id'] = $data['tree_from_refer_id'];
$_REQUEST['member'] = '';




class member_sign_up {
    //global $sugar_config;
	public $db;
	public $pdo;	
	
	function __construct() {
		global $sugar_config;

		$host_name= "localhost";
		$db_name = "mybooster";
		$user_name = "root";
		$password = "root";


		$this->pdo = new PDO("mysql:host=$host_name;dbname=$db_name", $user_name, $password);
		// set the PDO error mode to exception
		$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

		$this->pdo->beginTransaction();
		
		$this->db = mysql_connect($host_name, $user_name, $password);
		mysql_select_db($db_name);		
		
		if(!$this->pdo) {
			$result = array();
			$result['status'] = '0';
			$result['message'] = "APP temporarily Unavailable!";
			print $return = json_encode($result);
			exit;
		}
    }

	function search_refer_id_tree_available( $referID, $i ) {

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

			while($sqlRow 	= mysql_fetch_assoc($sqlResult)) {

				$i=$i+1;

				$referID = $sqlRow['refer_id'];
		
				$_REQUEST['data'][] = $referID;

				if($referID == $_REQUEST['tree_from_refer_id']) {
					return $referID;					
				} else {
					$result2 = $this->search_refer_id_tree_available($referID, $i);
					////if( count($result2) != 0 ) {
					//} else {
					//}
				}			
			}
			//array_push($finalData1, $sponsoredTo1);
		}

		return $sponsoredTo1;
	}

	function search_refer_id_available( $referID ) {

		$sql 				= "select id, name, last_name, mobile_number, email_address, refer_id, left_node, right_node, date_entered from my_registration where sponsor_id = '$referID' order by date_entered";
	
		$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)) {
			
				$result1 						= array();
				$sponsoredTo 					= array();
				$sponsoredTo['refer_id'] 		= $sqlRow['refer_id'];
				//$_REQUEST['data']['node'] 		= $_REQUEST['node'];

				$_REQUEST['data'][] 	= $sponsoredTo['refer_id'];

				$referID = $sponsoredTo['refer_id'];
				$result1 = $this->search_refer_id_tree_available($referID, $i);
			}
		}
		return $result1;
	}

	function get_package_details($refer_id) {

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

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

		$sqlPayout 			= "SELECT * FROM my_member_volume where refer_id='$refer_id'";
		$resPayout 			= mysql_query($sqlPayout);
		$rowPayout 			= mysql_fetch_assoc($resPayout);

		$packageArr['left_node_pv'] =  $rowPayout['left_member_pv'];
		$packageArr['right_node_pv'] =  $rowPayout['right_member_pv'];

	
		if($packageArr['left_node_pv']=="") {
			$packageArr['left_node_pv'] = 0;		
		}

		if($packageArr['right_node_pv']=="") {
			$packageArr['right_node_pv'] = 0;		
		}
	

		$packageArr['total_token']		= $totalTokens;
		$packageArr['total_purchase']	= $totalPurchase;
		$packageArr['total_pv']			= $totalPV;

		return $packageArr;
	}

	function get_booster_status($refer_id) {

		return 0;

	}

	function get_my_info( $referID ) {

		//$sql 		= "SELECT id, name, last_name, mobile_number, email_address, refer_id, left_node, right_node FROM my_registration WHERE deleted=0 AND refer_id = '$referID'";
		$sql 		= "SELECT id, name, last_name, mobile_number, email_address, refer_id, left_node, right_node FROM my_registration WHERE refer_id = '$referID'";
		$sqlResult 	= mysql_query($sql);
		$sqlRow 	= mysql_fetch_assoc($sqlResult);
		return $sqlRow;
	}

	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;
				$totalToken = '';
				$referID = $sqlRow['refer_id'];
				$totalPV =  $this->get_package_details($sqlRow['refer_id']);
				
				$sqlRow['left_node_token']  = $totalPV['left_node_pv'];
				$sqlRow['right_node_token'] = $totalPV['right_node_pv'];
				$sqlRow['total_bb_token'] 	= $totalPV['total_purchase'];
				$sqlRow['total_pv'] 		= $totalPV['total_pv'];

				
				if($i >= 4) { //nothing
					array_push( $sponsoredTo1, $sqlRow );
				} else {
					$result2 = $this->get_tree1($referID, $i);
					if( count($result2) != 0 ) {
					
	
						//print "<br>refer_id1: ".$referID;
						$sqlRow['d'] =  $result2;
						array_push( $sponsoredTo1, $sqlRow );
						//$sponsoredTo1[] = $result2;	
						//array_push( $sponsoredTo1, $sponsoredTo2 );
				
					} else {
						//print "<br>refer_id2: ".$referID;
						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'];

				$totalToken 	= $this->get_package_details($sponsoredTo['refer_id']);
				$boosterStatus 	= $this->get_booster_status($sponsoredTo['refer_id']);				
				$sponsoredTo['booster'] = $boosterStatus;

				$sponsoredTo['left_node_token'] = $totalToken['left_node_pv'];
				$sponsoredTo['right_node_token'] = $totalToken['right_node_pv'];
				$sponsoredTo['total_bb_token'] = $totalToken['total_purchase'];
				$sponsoredTo['total_pv'] = $totalToken['total_pv'];

				$referID = $sponsoredTo['refer_id'];
				if($sponsoredTo['refer_id']!=0)
					$result1 = $this->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;
	}

	function findMember($downlineReferID, $uplineReferID)
	{

		$sql 			= "select sponsor_id from my_registration where refer_id = '$downlineReferID'";
		$sqlResult 		= mysql_query($sql);
		$sqlRow 		= mysql_fetch_assoc($sqlResult);

		//echo $sqlRow['sponsor_id'] .   "     "  .   $uplineReferID . "\n";

		if($sqlRow['sponsor_id'] == $uplineReferID)
			$_REQUEST['member'] = 1;
		
		else if($sqlRow['sponsor_id'] == '123123629' || $sqlRow['sponsor_id'] == '233791797')
			$_REQUEST['member'] = 0;

		else if($sqlRow['sponsor_id'] == 0 || $sqlRow['sponsor_id'] == '')
			$_REQUEST['member'] = 0;

		else
			$this->findMember($sqlRow['sponsor_id'], $uplineReferID);


		return $downline;
		

	}


}

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

	   $conn 			= new member_sign_up();

	   if(!empty($_REQUEST['tree_from_refer_id']) && ($_REQUEST['tree_from_refer_id'] != $_REQUEST['referID']))
		{
		   
		   $conn->findMember($_REQUEST['tree_from_refer_id'], $_REQUEST['referID']);
   
 		   if(!($_REQUEST['member']))
			{
				$result['status'] = 0;
				$result['message'] = 'Member not found in your network!';
				print $result1 = json_encode($result);
				exit;
			}

			else
			{

				$_REQUEST['referID'] = $_REQUEST['tree_from_refer_id'];

			}
		}

		
		unset($_REQUEST['data']);

		$yourLegDetails = $conn->get_package_details($_REQUEST['referID']);
		$yourInfo = $conn->get_my_info($_REQUEST['referID']);

		$boosterStatus 		= $conn->get_booster_status($_REQUEST['referID']);				
		$yourInfo['booster'] = $boosterStatus;

		$yourInfo['right_node_token'] = $yourLegDetails['right_node_pv'];
		$yourInfo['left_node_token'] = $yourLegDetails['left_node_pv'];
		$yourInfo['total_token'] = $yourLegDetails['total_purchase'];
		$yourInfo['total_pv'] = $yourLegDetails['total_pv'];

		$result['status'] = 1;
		$result['message'] = 'Success';
		$result['personal_info'] = $yourInfo;
		$resultAll = $conn->get_tree($_REQUEST['referID']);
		$result['data'] = $resultAll;

	 
}

print $result1 = json_encode($result);

?>
