<?php

	print "<table border='1' width='50%' align='center'>";

	print "<tr>
				<td>Sr No</td>
				<td>Member Name</td>
				<td>Member ID</td>
				<td>Direct Incentive</td>
				<td>Tree Incentive</td>
				<td>Total</td>
				<td>Payout Date</td>
				<td>View Members</td>
			</tr>";

	$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);

	
$memberIDString ='[{"MemberID":909378288}]';

$memberIDString ='[{"MemberID":214018779}]';

   
    $memberIDIndex = 1;
	$memberIDArray = json_decode($memberIDString);
	//$memberIDArray = $memberIDString;
	
	foreach($memberIDArray as $data)
	{

		$result1 = getProfile($data, $db);
		$result2 = getPayoutInfo($data, $db);

		foreach($result2 as $row)
		{
			echo "<tr>";
			echo "<td>" . $memberIDIndex++  . "</td>";
			echo "<td>" . $result1['name'] . ' ' . $result1['last_name']  . "</td>";
			echo "<td>" . $result1['refer_id']   . "</td>";
			echo "<td>" . $row['direct_incentive']   . "</td>";
			echo "<td>" . $row['tree_incentive']   . "</td>";
			echo "<td>" . $row['incentive']   . "</td>";
			echo "<td>" . $row['payout_start_date']   . "</td>";
			echo "<td><a href='viewpayout.php?refer_id=" . $result1['refer_id'] ."&payout_id=" . $row['payout_id'] . "' target='_blank'>View Report</td>";
			echo "</tr>";
		}


	
	}


	function getProfile($data, $db)
	{
		try{

			$sql = "SELECT * from my_registration where refer_id='$data->MemberID'";
			//echo $sql . "<br/>";
			$stmt= $db->prepare($sql);
			$stmt->execute();
    		$result = $stmt->fetch(PDO::FETCH_ASSOC);
			return $result;
		

		}


		catch(Exception $e)
		{
			$e->getMessage();
			return false;

		}

	}

	
	function getPayoutInfo($data, $db)
	{
		try{

			$sql = "SELECT a.payout_id, a.refer_id, a.direct_incentive, a.tree_incentive, a.incentive, b.id, DATE_FORMAT(b.payout_start_date,'%d/%m/%Y') as 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='$data->MemberID' AND a.incentive>0";
			//echo $sql . "<br/>";
			$stmt= $db->prepare($sql);
			$stmt->execute();
    		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			return $result;
	
		}


		catch(Exception $e)
		{
			$e->getMessage();
			return false;

		}

	}


	
	
?>