<?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 = getPackageDetails($refer_id, $db)))
	{
        $result['success'] = true;
		$result['message'] = "Successful!";
		$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 getPackageDetails($refer_id, $db)
	{
		try
		{
		
			$stmt = $db->prepare("select * from my_member_package where refer_id='$refer_id'");
			$stmt->execute();
			$result = $stmt->fetchAll(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);


?> 
