<?php
	header('Content-Type: application/json');
	ini_set('display_startup_errors',1);
	ini_set('display_errors',1);
	error_reporting(-1);
	date_default_timezone_set('Asia/Kolkata');
	require_once('database.php');

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

	$_REQUEST['memberID'] = $data['memberID'];
   
	if(!isset($_REQUEST['memberID']) || $_REQUEST['memberID'] == '') {
		$validationErr = "Please enter Member ID!";
	}


	if(!empty($validationErr)){

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

	}

	
	$memberID  = $_REQUEST['memberID'];


	
	class Member
	{

		private $conn;
		public  $memberID;
		


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


		function checkUserExist()
		{

			$sql = "SELECT id FROM my_registration WHERE refer_id='$this->memberID'";
			$stmt = $this->conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetch(PDO::FETCH_ASSOC);

			if(empty($result['id']))
				return false;

			else
				return true;

		}

		function getProfile()
		{

			try{

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

			catch(Exception $ex){
				//echo $ex->getMessage();
			    return false;
			}

		}


		function getPurchase()
		{

			try{

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

			catch(Exception $ex){
				//echo $ex->getMessage();
			    return false;
			}

		}


		
		function getPayout()
		{

			try{

				$sql = "Select a.tree_incentive, a.direct_incentive, a.incentive, b.payout_start_date from my_payout_main as a left join my_payout_run as b on a.payout_id = b.id where a.refer_id='$this->memberID' AND a.incentive > 0";
				$stmt = $this->conn->prepare($sql);
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);
				return $result;					
			}

			catch(Exception $ex){
				//echo $ex->getMessage();
			    return false;
			}

		}




	}

	
	$member = new Member();
	$member->memberID = $_REQUEST['memberID']; 


	
	if($member->checkUserExist())
	{
		$data = array();
		$data['status']  = '1';
		$data['message'] = 'Successfull';
		$data['profile'] = $member->getProfile();
		$data['purchase']= $member->getPurchase();
		$data['payout']  = $member->getPayout();
		echo json_encode($data);
		exit;
	}

	else
	{
		$data = array();
		$data['status'] = '0';
		$data['message'] = "MemberID doesn't Exist.";
		echo json_encode($data);
		exit;
	}
?>