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

$data = json_decode(trim(file_get_contents("php://input")),true);
/*
$requestParameter['mobile_number'] = $_REQUEST['mobile_number'];
$requestParameter['first_name']   = $_REQUEST['first_name'];
$requestParameter['device_type']  = $_REQUEST['device_type'];
$requestParameter['source'] 	  = $_REQUEST['source'];
print "<pre>";
print_r($_REQUEST);
print "</pre>";
*/

/*$file = fopen("API_logs/API_updateProfile.txt","a");
$today = date("Y-m-d H:i:s");
$parameter = "-------------------".$today."----------------------------"."\n";
foreach($_REQUEST as $key=>$value) {
	$parameter = $parameter."$key=>$value"."\n";
}
$parameter = $parameter."-----------------------------------------------"."\n";
fwrite($file, $parameter);
fclose($file);*/

$validationErr = '';


/*$_REQUEST['refer_id'] = '123123629';
$_REQUEST['user_id'] = '7cb3c901-ffc3-e12b-9d6b-5a3918fb456d';

$data['refer_id'] = $_REQUEST['refer_id'];
$data['user_id'] = $_REQUEST['user_id'];*/

$validationErr = '';
if(!isset($data['refer_id'])){
	$validationErr = "Member ID Invalid!";
}else if(!isset($data['user_id'])){
	$validationErr = "Member ID Invalid!";
}

if($validationErr != '') {

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

}

$db = new PDO('mysql:host=localhost;dbname=mybooster;', 'root', 'root');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$refer_id = $data['refer_id'];
$user_id = $data['user_id'];
$result = array();
$data = array();

if(verifyUser($refer_id, $user_id, $db))
{
	if(count($personaldata = getUserData($refer_id, $db)))
	{
        $result['success'] = true;
		$result['message'] = "Sponsor Member exist!";
		$result['data']['personal_info'] = $personaldata;

    	$result['data']['package_info'] = getPackageDetails($refer_id, $db);

		$totalPV = 0;
		$totalPurchase = 0;

		foreach($result['data']['package_info'] as $row)
		{
			$totalPurchase += $row['product_price'];
			$totalPV += $row['total_PV'];
		}

		
		$result['data']['package_info']['totalPurchase'] = $totalPurchase;
		$result['data']['package_info']['totalPV'] = $totalPV;
		$result['data']['recently_joined'] = getRecentlyJoined($refer_id, $db);
		$result['data']['sales'] = getSalesData($refer_id, $db);
		$result['data']['secure'] = getTransactionPassword($refer_id, $db);
		$result['data']['pv_earnings'] = getPVEarnings($refer_id, $db);
		$result['data']['sponsor_count'] = getSponsorCount($refer_id, $db);

	}

	else
	{
		$result['success'] = false;
		$result['message'] = "No Sponsored Member exist!";
		$result['data'] = NULL;
	}

}

else
{
	$result['success'] = false;
	$result['message'] = "Invalid Credentials!";
}


   function verifyUser($refer_id, $user_id, $db)
	{
		try
		{
            $stmt = $db->prepare("select refer_id from my_registration where refer_id='$refer_id' && id='$user_id'");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);
			$db = null;

			if($result['refer_id'] != NULL)
				return 1;

			else
				return 0;
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}

		
	}


	function getUserData($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select * from my_registration where refer_id='$refer_id' order by date_entered ASC");
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			return $result;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}


	}

	function getPackageDetails($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select name, refer_id, date_entered, customer_id, product_id, product_price, total_PV, paid_by_online, online_TransactionID, paid_by_voucher, voucher_code, total_tax, total_paid  from my_member_package where refer_id='$refer_id' && payment_status='1'");
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			return $result;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}

	}

	
	/*function getRecentPackageDetails($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select name, refer_id, date_entered, customer_id, product_id, product_price, total_PV, paid_by_online, online_TransactionID, paid_by_voucher, voucher_code, total_tax, total_paid  from my_member_package where refer_id='$refer_id' && payment_status='1' order by date_entered desc limit 1");
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			return $result;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}

	}*/


	function getRecentJoineeSponsor($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select sponsor_by from my_registration where refer_id='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);
			return $result['sponsor_by'];
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}

	}






	function getRecentlyJoined($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select distinct refer_id, node from my_payout_child where sponsored_id='$refer_id' order by date_time desc limit 5");
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			$data = array();
			$i = 0;

			foreach($result as $row)
			{
				/*$getName = getUserData($row['refer_id'], $db);
			    $result[$i]['name'] = $getName[0]['name'] . " " .  $getName[0]['last_name'];
				$getPurchase = getRecentPackageDetails($row['refer_id'], $db);
    			$result[$i]['product'] = $getPurchase[0]['name'];
				$result[$i]['purchased_date'] = $getPurchase[0]['date_entered'];
				$result[$i]['price'] = $getPurchase[0]['product_price'];
				$result[$i]['pv'] = $getPurchase[0]['total_PV'];*/

				$getSponsor = getRecentJoineeSponsor($row['refer_id'], $db);
				$getName = getUserData($getSponsor, $db);
			    $result[$i]['name'] = $getName[0]['name'] . " " .  $getName[0]['last_name'];
			    $result[$i]['refer_id'] = $getSponsor;
				$i++;
			}

			return $result;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}

	}


	function getSalesData($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select * from my_member_volume where refer_id='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			if($result == Null)
				$result = 0;
			return $result;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}

	}

	function getTransactionPassword($refer_id, $db)
	{

		try
		{
			$stmt = $db->prepare("select transaction_password from my_registration where refer_id='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);
			if($result)
			{
				if($result['transaction_password']=='')
					return 0;
				else 
					return 1;

			}

			else
				return 0;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}


	}


	function getPVEarnings($refer_id, $db)
	{

		try
		{
			$stmt = $db->prepare("select * from my_pv_balance where refer_id='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);
			return $result;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}


	}


	function getSponsorCount($refer_id, $db)
	{

		try
		{
			$stmt = $db->prepare("select count(refer_id) as sponsor_count from my_registration where sponsor_by='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);
			return $result['sponsor_count'];
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Invalid Credentials!";
		}


	}









echo json_encode($result);


?> 
