<?php
session_start();
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("UTC");


require_once('API/database.php');

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


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

else if(!isset($_REQUEST['password']) || $_REQUEST['password']  == '') {
	$validationErr = "Please enter the password!";
} 


else
{


	$referID = $_REQUEST['memberID'];
	$password = md5($_REQUEST['password']);
    $decryptPassword = $_REQUEST['password'];

  try{
	$conn = new Database;
	$db = $conn->getConnection();
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	$stmt = $db->prepare("select * from my_registration where refer_id='$referID' and password = '$password'");
	$stmt->execute();
	$resultData = $stmt->fetch(PDO::FETCH_ASSOC);
	$db = null;
	$result = array();

	if($resultData['refer_id'] != NULL)
	{
		$result['success'] = true;
		$result['message'] = 'Login Successful!';
		$_SESSION['email_address'] = $resultData['email_address'];
	    $_SESSION['mobile_number'] = $resultData['mobile_number'];
	    $_SESSION['refer_id'] = $resultData['refer_id'];
	    $_SESSION['user_id'] = $resultData['id'];
	    $_SESSION['username'] = $resultData['name'] . " " . $resultData['last_name'];
		$_SESSION['customer_id'] = $resultData['customer_id'];

        $resultData['decryptPassword'] =  $decryptPassword;

		if($resultData['assigned_user_id'] == NULL || $resultData['assigned_user_id'] == 0)
		{
			addToMyBooster($resultData);
		
		}

			
	}


	else
	{
		session_unset();
		session_destroy();
		$result['success'] = false;
		$result['message'] = 'Invalid Login Credentials!';
	}

  }

  catch(Exception $err){
	  var_dump($err->getMessage());
  }

	print $return = json_encode($result);
	exit;
}

if($validationErr != '') {

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

} 



	function addToMyBooster($data)
	{
		$apiUrl = "https://5a3db12020244ee2e56274dc0addfecb:7dfd66436f07538a43c806e1364f2b39@marvel-mii.myshopify.com/admin/customers.json";
		$customerData = array();
		$customerData['customer']['email'] = $data['refer_id'] . "@mybooster.biz";
		$customerData['customer']['first_name'] = $data['name'];
		$customerData['customer']['last_name'] = $data['last_name'];
		$customerData['customer']['phone'] = $data['refer_id'] . "8";
		$customerData['customer']['verified_email'] = true;
		$customerData['customer']['password'] = $data['decryptPassword'];
		$customerData['customer']['password_confirmation'] = $data['decryptPassword'];
		$customerData['customer']['accepts_marketing'] = true;
		$customerData['customer']['addresses'][0]['address1'] = $data['address1'];
		$customerData['customer']['addresses'][0]['address2'] = $data['address2'];
		$customerData['customer']['addresses'][0]['city'] = $data['city'];
		$customerData['customer']['addresses'][0]['province'] = $data['state'];
		$customerData['customer']['addresses'][0]['zip'] = $data['zip_code'];
		$customerData['customer']['addresses'][0]['last_name'] = 'Home';
		$customerData['customer']['addresses'][0]['first_name'] = 'Residential';
		$customerData['customer']['addresses'][0]['country'] = $data['country'];
		curlRequest($apiUrl, $customerData, $data['refer_id']);

	}



	 function curlRequest($url, $data, $referID){

			$url = $url;

			$params = $data;
			//print_r($data_string);
			$session = curl_init($url);
		    //curl_setopt($session, CURLOPT_HEADER, true);
			curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($session, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: POST'));
			curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($params));
			curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
			curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
			curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

			$result = curl_exec($session);

			$response = json_decode($result);


			curl_close($session);
			
			if(isset($response->customer))
		    {
				updateassigned($referID, $response->customer->id);

			}

		   
		}


		function updateassigned($referID, $customer_id)
		{
			$conn = new Database;
			$db = $conn->getConnection();
			$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$stmt = $db->prepare("update my_registration set customer_id='$customer_id', assigned_user_id='1' where refer_id='$referID'");
			$stmt->execute();
			$db = null;

		}

?> 
