<?php 

if(isset($_POST['submit'])){
	
	$_POST = help::sanitize($_POST);
	
	$_POST['email'] = strtolower($_POST['email']);
	
	$user = help::get_user($_POST['email']);
	
	## [ VALIDATE ] 
	
	if( $_POST['pwd'] != $_POST['cpwd'] ) $msg = "Password does not match";
	
	else if( $_POST['pin'] != $_POST['cpin'] ) $msg = "PIN does not match";
	
	else if( $user ) $msg = "The email already exists";
	
	else {
		
		unset($_POST['cpin']);
		unset($_POST['cpwd']);
		unset($_POST['submit']);
		
		$_POST['pwd'] = md5($_POST['pwd']);
		$_POST['pin'] = md5($_POST['pin']);
		
		$_POST['acctno'] = help::generate_number(10);
		
		$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;
		};
		
		## ----------
		
		$SQL = sQuery::insert( 'tbl_users', $_POST );
		
		$insert = $link->query( $SQL );
		
		if( !$insert ) $msg = "Request Failed. Something went wrong!";
		
		else {
			
			$msg = "Your account has been successfully created!";
			
			$mailer = new mailer();
			$mailer->addAddress($_POST['email']);
			$mailer->Subject = "Account Created";
			
			$mailer->Body = "
				<section>
					Hi {$_POST['fname']},
				</section>
				
				<section>
					Thank you for creating an account with {:bname}. It's a great honour to have you onboard. <br>
					Your account verification process is pending. You will be notified once your account is confirmed. 
				</section>
			";
			
			$msg .= $mailer->send() ? "<br> We've sent you an email" : null;
			
			$login_url = help::url( USERS_DIR );
			
			$msg .= "<br> <a href='{$login_url}'>Click Here to Login</a>";
			
		};
		
	};
	
}








