<?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");
	setlocale(LC_MONETARY, 'en_IN');

	date_default_timezone_set('Asia/Kolkata');

	require_once("database.php");

	$data = file_get_contents('php://input');

	$data = json_decode($data, TRUE ); //convert JSON into array


	class OrderList
	{
		private $conn;
		public  $typeRequest;

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

		}


		function getMemberInfo($referID)
		{
			try{

				$stmt  = $this->conn->prepare("select name, last_name from my_registration where refer_id='$referID'");
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);
			    $name   = ucwords($result['name'] . ' ' . $result['last_name']);
				return $name;
			
		  }catch(Exception $err){
			echo $err->getMessage();
			//return 0;
		  } 

    	}


		function getOrderList()
		{

		   try{

			  $stmt  = $this->conn->prepare("select * from my_order order by date_entered desc");
			  $stmt->execute();
			  $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			  $data = array();
			  $data = $result;
			  $i=0;
			  foreach($result as $row)
			   {
				   $data[$i]['name'] = $this->getMemberInfo($row['refer_id']);
				   $i++;
			   }
			   
			  return $data;
			
		  }catch(Exception $err){
			echo $err->getMessage();
			//return 0;
		  } 
	
		}


		function getOrderData($orderID)
		{

		   try{

				$stmt  = $this->conn->prepare("select * from my_order where id='$orderID'");
				$stmt->execute();
				$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			 	return $result;
			
			  }catch(Exception $err){
				return 0;

			  } 
		
		}

		
		function getPackageData($orderID)
		{

		   try{

				$stmt  = $this->conn->prepare("select * from my_member_package where order_id='$orderID'");
				$stmt->execute();
				$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			 	return $result;
			
			  }catch(Exception $err){
				return 0;

			  } 
		
		}
		
	}

	$result = array();

	$order = new OrderList();
	//$order->typeRequest = $_REQUEST['typeRequest'];

	$result['success'] = true;
	$result['message'] = "Success";
	

	if($data['typeRequest']==0)
	{
		$result['orderList']   = $order->getOrderData($data['order_id']);
		$result['packageList'] = $order->getPackageData($data['order_id']);
	}

	else
		$result['orderList']  = $order->getOrderList();


	echo json_encode($result);



?>

