<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') {
	
	$status = false;
	
	$_POST = help::sanitize($_POST);
	$_POST['acctno'] = help::generate_number(10);
	
	// encrypt;
	
	$_POST['pwd'] = md5($_POST['pwd']);
	$_POST['pin'] = md5($_POST['pin']);
	
	// Copied image code from /for-register.php;
	
	$image = help::process_image( $_FILES['image'] );
		
	if( empty($image['error']) ) {
		$img_name = md5(basename($image['name'])) . ".{$image['extension']}";
		$target = UPLOADS_DIR . "/profile/{$img_name}";
		$uploaded = move_uploaded_file($image['tmp_name'], $target);
		if( $uploaded ) $_POST['pics'] = $img_name;
	};
	
	// End image code;
	
	$SQL = sQuery::insert( 'tbl_users', $_POST );
	
	$status = $link->query( $SQL );
	
	if( $status ) {
		
		$msg = "User account successfully created! <br>";
		
		$mailer = new mailer();
		$mailer->addAddress($_POST['email']);
		$mailer->Subject = "New Account";
		
		$TABLE = $mailer->table(array(
			array("First Name", $_POST['fname']),
			array("Last Name", $_POST['lname']),
			array("Login", $_POST['email']),
			array("Password", $_POST['pwd']),
			array("PIN", $_POST['pin']),
			array("Account Type", $_POST['utype']),
			array("Phone", $_POST['phone']),
		));
		
		$login_url = help::url( USERS_DIR );
		
		$mailer->Body = "
			<section>
				Dear {$_POST['fname']}, <br>
				A new account has been created on your behalf and it is currently inactive. You will be notified once it gets activated. <br>
				Please review your account information below:
			</section>
			<section>
				{$TABLE}
			</section>
			<section>
				Please <a href='{$login_url}'>login</a> to your account to update your information
			</section>
		";
		
		$msg .= $mailer->send() ? "Email Notification sent" : "Email notification could not be sent";
		
	} else $msg = "The account could not be created";
	
}