<?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);
	$_REQUEST['referID'] = $data['referID'];
	$_REQUEST['referID'] = '171066868';
	//$_REQUEST['referID'] = '909378288';

	$validationErr = '';
	if(!isset($_REQUEST['referID'])){
		$validationErr = "Member ID Invalid!";
	}

	if($validationErr != ''){

		$result = array();
		$result['success'] = false;
		$result['message'] = $validationErr;
		print $return = json_encode($result);
		exit;

	}

	require_once("database.php");

	class NetworkExplorer
	{
		private $conn;
    	public  $memberID;
		

		function __Construct()
		{
			$dbconnect = new Database();
			$this->conn = $dbconnect->getConnection();
			
		}

		function Network_List()
		{

			try{

				$sql = "SELECT * from my_network where refer_id = '$this->memberID'";
				$stmt= $this->conn->prepare($sql);
				$stmt->execute();
				$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
				return $result;
			}

			catch(Exception $e)
			{
				$e->getMessage();
				return false;

			}

		}


		function Member_Info($referID)
		{

			try{

				$sql = "SELECT concat(name, ' ' , last_name) as name, refer_id from my_registration where refer_id = '$referID' AND deleted = 0";
				$stmt= $this->conn->prepare($sql);
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);
				//var_dump($result);
				return $result;
			}

			catch(Exception $e)
			{
				$e->getMessage();
				return false;
			}

		}


		function Package_Info($referID)
		{

			try{

				$sql = "SELECT date_format(date_entered, '%d-%m-%Y') as purchase_date, sum(total_PV) as purchase from my_member_package where refer_id = '$referID' AND deleted = 0 order by date_entered ASC";
				$stmt= $this->conn->prepare($sql);
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);
				//var_dump($result);
				return $result;
			}

			catch(Exception $e)
			{
				$e->getMessage();
				return false;
			}

		}

	}


	$obj = new NetworkExplorer();
	$obj->memberID = $_REQUEST['referID'];
	$result = $obj->Network_List();


	$leftMember  = explode(",", rtrim($result[0]["leftNetwork"], ','));
	$rightMember = explode(",", rtrim($result[0]["rightNetwork"], ','));
	$data = array();
	$leftMemberData  = array(); 
	$rightMemberData = array(); 
	$i=0;
	$j=0;
	

	foreach($leftMember as $row)
	{
		//echo $row;
		//echo "<br/>";
		$memberInfo  = $obj->Member_Info($row);
		$packageInfo = $obj->Package_Info($row);
		$leftMemberData[$i]['name']		= $memberInfo['name'];
		$leftMemberData[$i]['refer_id'] = $memberInfo['refer_id'];
		$leftMemberData[$i]['purchase'] = $memberInfo['purchase'];
		$leftMemberData[$i]['purchase_date'] = $memberInfo['purchase_date'];	
		$leftMemberData[$i]['leg'] = 0;
		$i++;
	}

	foreach($rightMember as $row)
	{
		$memberInfo  = $obj->Member_Info($row);
		$packageInfo = $obj->Package_Info($row);
		$rightMemberData[$j]['name']	 = $memberInfo['name'];
		$rightMemberData[$j]['refer_id'] = $memberInfo['refer_id'];
		$rightMemberData[$j]['purchase'] = $memberInfo['purchase'];
		$rightMemberData[$j]['purchase_date'] = $memberInfo['purchase_date'];
		$rightMemberData[$j]['leg'] = 1;
		$j++;
	}

	//var_dump($leftMemberData);


	$data['status']		= 1;
	$data['message']	= 'Success';
	$data['leftMember'] = $leftMemberData;
	$data['rightMember']= $rightMemberData;

	echo json_encode($data);





?>
