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

//require_once('database.php');
/*
$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>";
*/

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


/*$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 = '';


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($data = getSponsorData($refer_id, $user_id, $db)))
	{
        $result['success'] = true;
		$result['message'] = "Sponsor Member exist!";
		$result['data'] = $data;


	}

	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 getSponsorData($refer_id, $user_id, $db)
	{
		$directBonusArray = array();
		$directBonusArrayMain = array();

		try
		{
			$stmt = $db->prepare("select name, last_name, refer_id, date_entered from my_registration where deleted=0 and sponsor_by='$refer_id' order by date_entered ASC");
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

			foreach($result as $resultData) {

    			$directBonusArray['refer_id']	 	= 	$resultData['refer_id'];	
				$directBonusArray['date_entered']	=	$resultData['date_entered'];
				$directBonusArray['name']			=	$resultData['name'];
				$directBonusArray['last_name']		=	$result['last_name'];
				$packageDetails = 	getPackageDetails($resultData['refer_id'], $db);

				//$directBonusArray['activation_date']=	$packageDetails['date_entered'];
				$directBonusArray['purchase']		=	$packageDetails['totalPurchase'];
				$directBonusArray['totalPV']		=	$packageDetails['totalPurchasePV'];

				//$directBonusArray['position']       =	$this->findChildPosition($rowSelect1['refer_id'], $sponsor_id);
				
				if($directBonusArray['totalPV'] != 0) {
					$directPayout = (($directBonusArray['totalPV']/100)*5);
					$totalDirectPayout = $totalDirectPayout+$directPayout;
				} else {
					$directPayout = 0;
				}
				$directBonusArray['incentive'] 		= $directPayout;
				array_push($directBonusArrayMain, $directBonusArray);

			}

			$directBonusArrayMain['total_incentive'] = $totalDirectPayout;
			return $directBonusArrayMain;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Change Password request failed! Please try after sometime.";
		}

	}



		

	function getPackageDetails($refer_id, $db)
	{
		try
		{
		
			$stmt = $db->prepare("select sum(purchase) as totalPurchase, sum(purchase_PV) as totalPurchasePV  from my_direct_payout where sponsored_id='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);
			return $result;
	
		}

		catch(PDOException $ex){
		   $result['success'] = false;
		   $result['message'] = "Change Password request failed! Please try after sometime.";
		}


	}




echo json_encode($result);


?> 
