<?php
error_reporting(0);
session_start();
date_default_timezone_set("Asia/Kolkata");

require_once '../connection/db.php';

if (!empty($_GET['userId']) && !empty($_GET['mID']) && !empty($_GET['codeGenerate'])) {

	$db = getDB();

	$user_id = base64_decode($_GET['userId']);
	$wallet = base64_decode($_GET['mID']);
	$memberId = base64_decode($_GET['codeGenerate']);

	/* VERIFY USER EXISTS */
	$stmt = $db->prepare("SELECT member_id,user_id,walletAddress 
                          FROM meddolic_user_details 
                          WHERE member_id=? AND user_id=? 
                          LIMIT 1");
	$stmt->execute([$memberId, $user_id]);
	$user = $stmt->fetch();

	if ($user) {

		/* CLEAR OLD COOKIES */
		setcookie('memberUserId', '', time() - 3600, '/');
		setcookie('memberPassKey', '', time() - 3600, '/');

		/* SET SESSION */
		$_SESSION['user_member_id'] = $user['member_id'];
		$_SESSION['member_user_id'] = $user['user_id'];
		$_SESSION['member_walletAddress'] = $user['walletAddress'];

		/* SET COOKIE */
		setcookie("memberUserId", $user['user_id'], time() + 3600, "/");
		setcookie("memberPassKey", $user['walletAddress'], time() + 3600, "/");

		echo "<script>window.top.location.href='Dashboard.php';</script>";
		exit;
	} else {

		echo "<script>alert('Invalid User');window.history.back();</script>";
		exit;
	}
}
?>