<?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'];*/
//$data['refer_id'] = '909378288';
//$data['user_id']='cce0df95-4ade-9824-bc54-5a888ec2957f';



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

}

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 getPVEarnings($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select sum(direct_incentive) as directIncentive, sum(tree_incentive) as treeIncentive from my_payout_main where refer_id='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);

			$stmt2 = $db->prepare("select payout_id FROM my_payout_main where refer_id='$refer_id' ORDER BY payout_id DESC limit 1");
			$stmt2->execute();
			$result2 = $stmt2->fetch(PDO::FETCH_ASSOC);
			$payoutID = $result2['payout_id'];

			$stmt3 = $db->prepare("select payout_run_date FROM my_payout_run where id='$payoutID'");
			$stmt3->execute();
			$result3 = $stmt3->fetch(PDO::FETCH_ASSOC);
			$updatedDate = $result3['payout_run_date'];
			
			$data = array();
			$data['directIncentive'] = $result['directIncentive'];
			$data['treeIncentive']   = $result['treeIncentive'];
			$data['updatedDate']     = $updatedDate;
			
			return $data;
	
		}

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


	function getPayoutDetails($refer_id, $db)
	{
		try
		{
			$stmt = $db->prepare("select * from my_payout_main where refer_id='$refer_id' && incentive > 0 order by id desc");
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

			$i=0;
			foreach($result as $row)
			{

				$result[$i]['start_date'] = getPayoutDate($row['payout_id'], $db);
				$i++;

			}

			return $result;

		}

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




	function getPayoutDate($payout_id, $db)
	{

		try
		{
            $stmt = $db->prepare("select DATE_FORMAT(payout_start_date, '%d-%m-%Y') as payout_start_date from my_payout_run where id='$payout_id' && status=1");
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);
			return $result['payout_start_date'];
		}

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

	}










echo json_encode($result);


?> 
