<?php
include 'dbconn.php';
include 'func.php';

if(!empty($_REQUEST['type']))
{
	$type = mysqli_real_escape_string($conn, $_REQUEST['type']);
	$proceed = false;
	
	if(!empty($_REQUEST['password']) && !empty($_REQUEST['email']) ){
		$proceed = true;
	}
	
	if($proceed){
		$member_email = mysqli_real_escape_string($conn, $_REQUEST['email']);
		$member_password = mysqli_real_escape_string($conn, $_REQUEST['password']);
		$member_password = md5($member_password);
		
		$stmt = $conn->prepare("SELECT id, member_name, member_email, active_status FROM $table_member WHERE member_email = ? AND member_password = ?");
		$stmt->bind_param("ss", $member_email, $member_password);
		$stmt->execute();
		$stmt->store_result();
		$num_of_rows = $stmt->num_rows;
		$stmt -> bind_result($id, $member_name, $member_email, $active_status);
		
		$users = [];
		while ($stmt->fetch()) {
			$users[] = array(
				'id' => $id,
				'member_name' => $member_name,
				'member_email' => $member_email,
				'active_status' => $active_status
			);
		}
    	$stmt->free_result();
		$stmt->close();
		
		$userActive = false;
		
		//echo $users[0]['member_name'];
		if($num_of_rows > 0)
		 {
			 //existing user
			$member_id = $users[0]['id'];
			$member_name = $users[0]['member_name'];
			$member_email = $users[0]['member_email'];
			$active_status = $users[0]['active_status'];
			
			if((int)$active_status == 1){
				$userActive = true;
			}
			
			$_SESSION['Facebook_ID'] = '';
			$_SESSION['Member_ID'] = $member_id;
			$_SESSION['Name'] = $member_name;
			$_SESSION['Email'] = $member_email;
			$_SESSION['Type'] = 'default';
			$_SESSION['LoggedIn'.$session_app] = 1;
			$_SESSION['Token'] = getToken(30);
			
			$proceed = false;
			$check_member_points = checkExistPoints($member_id, $gameType);
			
			if(count($check_member_points) > 0){
				$proceed = true;
			}else{
				if(createMemberPoints($gameType, $member_id, $defaultPoint, $defaultChance, $defaultScore)){
					$proceed = true;
					$check_member_points = checkExistPoints($member_id, $gameType);
				}
			}

			if(!$userActive){
				echo '{"status":false, "message":"'.$lang['user-account-inactive'].'"}';
			}else if($proceed){
				if(count($check_member_points) > 0){
					$member_point = $check_member_points[0]['member_point'];
					$member_chance = $check_member_points[0]['member_chance'];
					$member_score = $check_member_points[0]['member_score'];

					echo '{"status":true, "member_id":'.$member_id.', "member_name":"'.$member_name.'", "member_point":'.$member_point.', "member_chance":'.$member_chance.', "member_score":'.$member_score.', "token":"'.$_SESSION['Token'].'", "game_type":"'.$gameType.'", "message":"'.$lang['user-login-successful'].'"}';	
				}else{
					echo '{"status":false, "message":"'.$lang['server-points-not-found'].'"}';	
				}
			}else{
				echo '{"status":false, "message":"'.$lang['server-points-not-found'].'"}';
			}
		 } else {
			echo '{"status":false, "message":"'.$lang['user-not-found'].'"}';	
		 }
	}else{
		echo '{"status":false, "message":"'.$lang['server-error'].'"}';
	}
}else{
	echo '{"status":false, "message":"'.$lang['server-error'].'"}';	
}

// Close connection
mysqli_close($conn);
?>