<?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;

	}


	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;

} 

?> 
