<?php
error_reporting(0);
include("../Header/Connection.php");

$ref_code = $_GET['ref_code'];
$from_page = $_GET['from_page'];
$activation_status = $_GET['activation_status'];

if ($from_page!= "")
{
	$success_wording = "Success. Account has been activated.";
	$fail_wording = "Fail to verify";
	$redirect_location = $from_page."?activation_status=".$activation_status; //list_of_user.php?activation_status=101
}
else
{
	$success_wording = "Thank You For Your Registration.Please Login To Access Your Dashboard";
	$fail_wording = "Fail to verify. Please contact admin.";
	$redirect_location = "https://mygoldsdnbhd.com/log-masuk/";
}

$New_ref_code_count = 1;
$New_ref_code = "MG10001";

$sql_last_user = "SELECT ref_code FROM user
	              Where status='1'
				  And (
					  ref_code Like 'MG1%'
					  Or ref_code Like 'MGP1%'
				  )
				  And ref_code != 'MGP10000'
		          Order by CAST(REPLACE(REPLACE(ref_code, 'MGP1000', ''), 'MG1000', '') AS UNSIGNED) Desc
				  LIMIT 1 ";
$result_last_user = $mysqli-> query($sql_last_user);
if ($result_last_user -> num_rows > 0)
{
	$row_last_user = $result_last_user ->fetch_assoc();
	$Last_ref_code = $row_last_user['ref_code'];
	$Last_ref_code = preg_replace('/^MGP?1000/', '', $Last_ref_code);
	$New_ref_code_count = (int)$Last_ref_code + 1;
	$New_ref_code = "MG1000".$New_ref_code_count;
}

//Update User Info
$Update_user_info = "UPDATE user SET
ref_code = '$New_ref_code',
username = '$New_ref_code',
trans_code = '$ref_code',
user_id = '$New_ref_code_count',
status = '1',
activation_status = '1',
verification_status = '1'
WHERE ref_code = '$ref_code' ";
$result_user_info = $mysqli->query($Update_user_info);

//Update Wallet info
$Update_wallet_info = "UPDATE wallet SET
ref_code = '$New_ref_code',
status = '1'
WHERE ref_code = '$ref_code' ";
$result_wallet_info = $mysqli->query($Update_wallet_info);

//Update Wallet info
$Update_wallet_info = "UPDATE payment SET
status = '1',
payment_status = '1'
WHERE trans_code = '$ref_code' ";
$result_wallet_info = $mysqli->query($Update_wallet_info);

if ($result_user_info && $result_wallet_info) 
{
    echo "<script type='text/javascript'>alert('" . addslashes($success_wording) . "');</script>";
    echo "<script type='text/javascript'>window.location.href='" . addslashes($redirect_location) . "';</script>";
    exit();
} 
else 
{
    echo "<script type='text/javascript'>alert('" . addslashes($fail_wording) . "');</script>";
    echo "<script type='text/javascript'>window.location.href='" . addslashes($redirect_location) . "';</script>";
    exit();
}
