<?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");
    //$data = json_decode(trim(file_get_contents("php://input")),true);


	class ClaimReward
	{
		private $conn;
		public  $referID;
		public  $userID;
		private $regdDate;
		private $claimedLeftVolume;
		private $claimedRightVolume;
		public $rewardID;
		public $password;


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

		function verifyUser()
		{

		   try{

				$stmt  = $this->conn->prepare("SELECT refer_id from my_registration where refer_id='$this->referID' && transaction_password='$this->password' && id='$this->userID'");
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);
			 	if($result['refer_id'])
					return true;
				else
					return false;
			
			  }catch(Exception $err){
				return false;
		   } 

		}


		function addClaimRequest()
		{

		   try{

				$stmt = $this->conn->prepare("INSERT INTO my_member_reward_claim(refer_id, reward_id, date_entered, status) VALUES(?, ?, ?, ?)");
				$stmt->execute(array($this->referID, $this->rewardID,  date("Y-m-d H:i:s"), 0));
				$result = $stmt->fetch(PDO::FETCH_ASSOC);
			 	return true;
			
			  }catch(Exception $err){
				return $err->getMessage();
		   } 
		
		}


		function previousRequest()
		{

		   try{

				$stmt  = $this->conn->prepare("SELECT refer_id  from my_member_reward_claim where refer_id='$this->referID' && reward_id='$this->rewardID'");
				$stmt->execute();
				$result = $stmt->fetch(PDO::FETCH_ASSOC);
			 	if($result['refer_id'])
					return false;
				else
					return true;
			
			  }catch(Exception $err){
				return false;
		   } 

		}



		
	}

   	
    if(!isset($_REQUEST['rewardID']) || $_REQUEST['rewardID'] == '') {
		$validationErr = "Please enter reward name!";
	} else if(!isset($_REQUEST['rewardTransactionPassword']) || $_REQUEST['rewardTransactionPassword'] == '') {
		$validationErr = "Please enter transaction password!";
	}


	if($validationErr != '') {

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

	} 


	$result               = array();
	$rewardObj            = new ClaimReward();
	$rewardObj->referID   = $_REQUEST['referID'];
	$rewardObj->userID    = $_REQUEST['userID'];
	$rewardObj->rewardID  = $_REQUEST['rewardID'];
	$rewardObj->password  = md5($_REQUEST['rewardTransactionPassword']);


	if($rewardObj->verifyUser())
	{
		if($rewardObj->previousRequest())
		{
			if($rewardObj->addClaimRequest())
			{
				$result['success'] = 1;
				$result['message'] = "Your claim request is accepted. We would contact you soon!";
			}

			else
			{
				$result['success'] = 1;
			    $result['message'] = "Claim request failed. Please try later!";
			}

			
		}

		else
		{
			$result['success'] = 1;
			$result['message'] = "Your claim request for this reward is already in process. We would contact you soon!";

		}

	}


	else
	{

		$result['success'] = 0;
        $result['message'] = "Invalid Credentials! Your claim request cannot be accepted!";

	}


	echo json_encode($result);



?>

