<?php 
include("user_func.php");

function sendRequestEmail($userCode, $email)
{
	global $sableDB;
	$row 			= $sableDB->getRow('select * from user where user_code = '.$userCode);
	$coy 			= SableDBFunctions::getCompanyName($row['user_code']);
	$userName		= SableDBFunctions::getUserDisplayName($email);
	$emailContent 	= '<p>Request for assistance: '.$userName.' from '.$coy.':
		<ul>
			<li>Company: '.$coy.'</li>
			<li>Contact: '.$userName.'"</li>
			<li>Phone: <a href="tel:'.$row['user_cell'].'">'.$row['user_cell'].'"</a></li>
			<li>Email: <a href="tel:'.$row['user_email'].'">'.$row['user_email'].'"</a></li>
		</ul>
	</p>';
	SableSystem::sendEmail("info@jona.co.za", "Request For Assistance", $emailContent);			
} /* function sendConfirmationLink() */	

$loginLink = SableSystem::getSableLinkPath('login/index.php');		
$msg = "Your profile could not be found. ".
		"The link may possibly have been corrupted or your request for assistance has aleady been registered. ".
		"Please contact support for assistance.";
$hdr = '<h3 class="title"><i class="fa fa-frown-o" aria-hidden="true"></i>&nbsp;Oops...</h3>';
$userHash = base64_decode($_SESSION['caller']['args'][1]);
$sql = 'select user_request_id, user_code from user__request where hash = "'.$userHash.'" and expires is not null';
$row = $sableDB->getRow($sql);

if (is_array($row))
{
	
	$userCode 	= $row['user_code'];
	$requestId 	= $row['user_request_id'];
	$email		= SableDBFunctions::getUserEmailFromCode($userCode);
	$userName	= SableDBFunctions::getUserDisplayName($email);	
	
	$now = date('Y-m-d H:i:s');
	$sql = "update user set user_active = 1, activate_date = '".$now."' where user_code = '" . $userCode . "'";
	$result = $sableDB->updateQuery($sql);

	if ($result)
	{
		$hdr = '<h3 class="title"><i class="fa fa-ambulance" aria-hidden="true"></i>&nbsp;Request Received</h3>';
		$msg = "Thank you <strong>".$userName."</strong>.<p>".
		"We will be in touch with you soon so we can assist with your profile.</p>";
		$res = $sableDB->updateQuery('update user__request set expires = NULL where user_request_id = '.$requestId);
		sendRequestEmail($userCode, $email);
	}
}
?>
<!--// header -->
<form id="Login" name="Login">
	<div class="forms">
        <p></p>
        <div class="container forms">
            <div class="forms-grids">
                <div class="col-md-12 forms-grid">
                <?php echo($hdr); ?>
                </div> 
            </div>
        </div>
        <p></p>
        <div class="container">  
            <div class="forms-grids">
                <div class="col-md-12 forms-grid">
                    <? echo($msg); ?>
                    <p></p>
                </div>
            </div>
        </div>
	</div>
 </form>