<?php
require_once('global.php');
require_once(ABSOLUTE_PATH.'/class/Mailer.php');
require_once(ABSOLUTE_PATH.'/class/Validator.php');

$template_body_class.= 'background-global';

$meta_title = 'Contact Us &ndash; LoLSkill';
$meta_description = 'You got feedback, want to report an issue or have questions? Contact us with your concerns!';
$template_og['url'] = DYNAMIC_PATH.'/contact';
$template_og['title'] = 'Contact Us &ndash; LoLSkill';
$template_og['description'] = 'You got feedback, want to report an issue or have questions? Contact us with your concerns!';

$showform = true;
if(isset($_POST['submit'])){
	$error = [];
	$username = htmlspecialchars(trim($_POST['username']));
	$contactRealm = htmlspecialchars(trim($_POST['contactRealm']));
	$email = htmlspecialchars(trim($_POST['email']));
	$text = htmlspecialchars(trim($_POST['text']));
	if($username==''){
		$error[] = 'Please enter your summoner name';
	}
	if(!Validator::checkEmail($email)){
		$error[] = 'Please enter a valid e-mail address';
	}
	if($text==''){
		$error[] = 'Please enter a message';
	}
	if(count($error)==0){
		$mailer = new Mailer();
		$mailer->addTo('contact@lolskill.net', 'LoLSkill Contact Form');
		$mailer->setFrom('noreply@lolskill.net', '['.$contactRealm.'] '.$username.' - LoLSkill Contact Form');
		$mailer->addReplyTo($email);
		$mailer->setSubject('Message from ['.$contactRealm.'] '.$username);
		$text_footer = '<br><br>Contact Form Submitted by IP: '.$_SERVER['HTTP_X_FORWARDED_FOR'];
		$mailer->setPlainText($text.$text_footer);
		$mailer->setHtmlText($text.$text_footer);
		$success = $mailer->send();
		if($success){
			$showform = false;
			$message = '			<div class="alert alert-success" role="alert">
				<p>We have received your message. Thank you!</p>
			</div>';
		}else{
			$error[] = 'An error occured while trying to send your message.';
		}
	}
	if(count($error) > 0){
		$message = '			<div class="alert alert-danger" role="alert">
				<p>The following error'.((count($error)==1) ? '' : 's').' occured:</p>';
		foreach($error as $err){
			$message.= '				&bull; '.$err.'<br>';
		}
		$message.= '			</div>';
	}
}else{
	$username = '';
	$email = ($um->isUser()) ? $thisUser['email'] : '';
	$text = '';
	$contactRealm = '';
}

if(!isset($message)){
	$message = '			<div class="alert alert-info" role="alert">
				<p>We recommend that you check whether your question has already been answered in the <a href="faq">FAQ</a>.</p>
				<p>Please keep your requests in English.</p>
			</div>';
}

$template_content = $am->getLeaderboard();
$template_content.= '		<section id="contact">
			<div class="container">
				<h2>Contact Us</h2>';
$template_content.= $message;

if($showform){
	$template_content.= '				<form action="contact" method="post">
					<div class="form-group">
						<label class="col-form-label" for="username">Summoner Name:</label>
						<input type="text" name="username" id="username" placeholder="Enter Summoner Name" autocomplete="off" maxlength="16" class="form-control" value="'.$username.'" required autofocus>
					</div>
					<div class="form-group">
						<label class="col-form-label" for="contactRealm">Region:</label>
						<select id="contactRealm" name="contactRealm" class="form-control">
							<option value="">Select Region</option>
							<option value="EUW" '.(($contactRealm=='EUW') ? 'selected' : '').'>EU West</option>
							<option value="EUNE" '.(($contactRealm=='EUNE') ? 'selected' : '').'>EU Nordic & East</option>
							<option value="NA" '.(($contactRealm=='NA') ? 'selected' : '').'>North America</option>
							<option value="BR" '.(($contactRealm=='BR') ? 'selected' : '').'>Brazil</option>
							<option value="KR" '.(($contactRealm=='KR') ? 'selected' : '').'>Korea</option>
							<option value="TR" '.(($contactRealm=='TR') ? 'selected' : '').'>Turkey</option>
							<option value="RU" '.(($contactRealm=='RU') ? 'selected' : '').'>Russia</option>
							<option value="LAN" '.(($contactRealm=='LAN') ? 'selected' : '').'>Latin America North</option>
							<option value="LAS" '.(($contactRealm=='LAS') ? 'selected' : '').'>Latin America South</option>
							<option value="OCE" '.(($contactRealm=='OCE') ? 'selected' : '').'>Oceania</option>
							<option value="JP" '.(($contactRealm=='JP') ? 'selected' : '').'>Japan</option>
						</select>
					</div>
					<div class="form-group">
						<label class="col-form-label" for="email">E-mail Address:</label>
						<input type="email" name="email" id="email" placeholder="Enter E-mail Address" autocomplete="off" maxlength="255" class="form-control" value="'.$email.'" required>
					</div>
					<div class="form-group">
						<label class="col-form-label" for="text">Message:</label>
						<textarea name="text" id="text" class="form-control" placeholder="Enter Your Message" required>'.$text.'</textarea>
					</div>
					<input type="hidden" name="submit" value="1">
					<button type="submit" class="btn btn-outline-primary">Send Message</button>
				</form>';
}
$template_content.= '			</div>
		</section>';
$template_content.= $am->getLeaderboard();

include_once(ABSOLUTE_PATH.'/template.php');
?>