<?php
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$user_type = $data['user_type'];
$mode = $data['mode'];
$member_id = $data['member_id'];

if ($user_type == '' || $member_id == '') {
	$returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went wrong try again !");
} else {
    // get all given
	if ($mode == 1) {
	    if($user_type == 2) {
	        $visitor_query = $con->query("select * from business_given_received where given_member_id = '$member_id' Order by id DESC");
	    } else {
	        $visitor_query = $con->query("select * from business_given_received where given_member_id = '$member_id' Order by id DESC");
	    }
		$visitor_details = array();
		$all_visitors = array();
		while ($row1 = $visitor_query->fetch_assoc()) {
			$visitor_details['id'] = $row1['id'];
			$visitor_details['given_member_id'] = $row1['given_member_id'];
			$g_member_id = $row1['given_member_id'];
// 			$visitor_details['given_name'] = $row1['given_name'];
// 			$visitor_details['given_mobile'] = $row1['given_mobile'];
			$member_query = $con->query("select * from app_users where member_id = '$g_member_id' Order by id DESC");
			if ($row2 = $member_query->fetch_assoc()) {
			    $visitor_details['given_name'] = $row2['name'];
			    $visitor_details['given_mobile'] = $row2['mobile'];
			    $visitor_details['given_image'] = $row2['logo'];
			}
			$visitor_details['received_member_id'] = $row1['received_member_id'];
			$r_member_id = $row1['received_member_id'];
// 			$visitor_details['received_name'] = $row1['received_name'];
// 			$visitor_details['received_mobile'] = $row1['received_mobile'];
			$member_query1 = $con->query("select * from app_users where member_id = '$r_member_id' Order by id DESC");
			if ($row3 = $member_query1->fetch_assoc()) {
			    $visitor_details['received_name'] = $row3['name'];
			    $visitor_details['received_mobile'] = $row3['mobile'];
			    $visitor_details['received_image'] = $row3['logo'];
			}
			$visitor_details['business_date'] = $row1['business_date'];
			$visitor_details['business_value'] = $row1['business_value'];
			$visitor_details['for_referral'] = $row1['for_referral'];
			$all_visitors [] = $visitor_details;
		}

		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"data" => $all_visitors
		);
	} else if ($mode == 2) {
	    if($user_type == 2) {
	        $visitor_query = $con->query("select * from business_given_received where received_member_id = '$member_id' Order by id DESC");
	    } else {
	        $visitor_query = $con->query("select * from business_given_received where received_member_id = '$member_id' Order by id DESC");
	    }
		$visitor_details = array();
		$all_visitors = array();
		while ($row1 = $visitor_query->fetch_assoc()) {
			$visitor_details['id'] = $row1['id'];
			$visitor_details['given_member_id'] = $row1['given_member_id'];
			$g_member_id = $row1['given_member_id'];
// 			$visitor_details['given_name'] = $row1['given_name'];
// 			$visitor_details['given_mobile'] = $row1['given_mobile'];
			$member_query = $con->query("select * from app_users where member_id = '$g_member_id' Order by id DESC");
			if ($row2 = $member_query->fetch_assoc()) {
			    $visitor_details['given_name'] = $row2['name'];
			    $visitor_details['given_mobile'] = $row2['mobile'];
			    $visitor_details['given_image'] = $row2['logo'];
			}
			$visitor_details['received_member_id'] = $row1['received_member_id'];
			$r_member_id = $row1['received_member_id'];
// 			$visitor_details['received_name'] = $row1['received_name'];
// 			$visitor_details['received_mobile'] = $row1['received_mobile'];
			$member_query1 = $con->query("select * from app_users where member_id = '$r_member_id' Order by id DESC");
			if ($row3 = $member_query1->fetch_assoc()) {
			    $visitor_details['received_name'] = $row3['name'];
			    $visitor_details['received_mobile'] = $row3['mobile'];
			    $visitor_details['received_image'] = $row3['logo'];
			}
			$visitor_details['business_date'] = $row1['business_date'];
			$visitor_details['business_value'] = $row1['business_value'];
			$visitor_details['for_referral'] = $row1['for_referral'];
			$all_visitors [] = $visitor_details;
		}

		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"data" => $all_visitors
		);
	}
}

echo json_encode($returnArr);
mysqli_close($con);
?>