<?php
//if the form is posted, has all the mandatory fields and captcha matches
if ( ! empty( $_POST['support-submit'] ) ) {
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

//FORM INPUTS	
	$recipient = OW_SUPPORT_EMAIL;
	$fromEmail = filter_var($_POST['support-email'], FILTER_SANITIZE_EMAIL);
	$security = $_POST['support-security'];
	$theCategory = sanitize_text_field( stripslashes( $_POST['support-category'] ) );
	$theSubject = sanitize_text_field( stripslashes( $_POST['support-subject'] ) );
	

//EMAIL SUBJECT	
	$mailSubject = ( is_package_1() ) ? 'CHARGEABLE support: ' : 'FREE support: ';
	$mailSubject .= html_entity_decode( ow_global_var( 'accountant_name' ) ) . ' - ';
	$mailSubject .= $theCategory . ' - ';
	$mailSubject .= $theSubject;
	
//EMAIL BODY	
	$mailBody = '<html><body>';
	$mailBody .= 'This support ticket was raised via: ' . get_site_url() . '<br /><br />';
	$mailBody .= ( ow_global_var( 'in_production' ) == 1 ) ? '<b>Website status:</b> In production<br />' : '<b>Website status:</b> In development<br />';
	$mailBody .= '<b>Website package:</b> ' . ow_global_var( 'website_package' ) . '<br />';
	$mailBody .= '<b>Telephone:</b> ' . ow_global_var( 'accountant_phone' ) . '<br />';
	$mailBody .= '<b>Customer username:</b> ' . CLIENT_USERNAME . '<br /><br />';
	$mailBody .= '<b>Category</b> ' . $theCategory . '<br />';
	$mailBody .= '<b>Subject:</b> ' . $theSubject . '<br />';
	$mailBody .= '<b>Message:</b>' . wpautop( esc_textarea( stripslashes( $_POST['support-message'] ) ) );

//EMAIL FOOTER	
	$footer = get_email_footer();
	$mailBody .= $footer;
	$mailBody .= '</body></html>';

//EMAIL HEADERS
	function get_support_email_headers( $email ) {
		
		$from = html_entity_decode( ow_global_var( 'accountant_name' ) );
		$headers[] = 'From: ' . $from . ' <' . $email . '>';
		$headers[] = 'Reply-To: ' . $recipient;
		$headers[] = 'Content-Type: text/html; charset=UTF-8';	

		return $headers;
	}
	$headers = get_support_email_headers( $fromEmail );
	
//SECURITY CHECK	
	if ( $security != '' && $_SESSION['support-token'] != $_POST['support-token'] ) {
		//If the security check fails, don't send the email	
	} else {
		wp_mail( $recipient, $mailSubject, $mailBody, $headers );
	}
	unset( $_POST );
	echo '<p><strong>Thank you for contacting ' . PRODUCT_NAME . ' Support</strong></p><p>We aim to respond to support queries within ' . OW_SUPPORT_RESPONSE_TIME . ' hours and you will be contacted at ' . $fromEmail . '</p><p>If you don\'t recieve a confirmation email of this support request, please get in touch on ' . OW_SUPPORT_PHONE . '</p>';
	
} } else {
//if the form has not been posted	
?>
<form id="form-support" class="form" method="post" action="<?php menu_page_url( 'iris-admin-panel' ); ?>" role="form">
	<p><label>Your email address:</label><input type="email" name="support-email" placeholder="Email*" value="<?php echo ow_global_var( 'form_email' ); ?>" required/></p>
	<p>
		<label>What is your query regarding?</label><select name="support-category">
			<?php if ( ow_global_var( 'in_production' ) == 0 ) { echo '<option value="First Draft Changes">First Draft Changes</option>';} ?>
			<?php if ( ! is_package_4() ) { echo '<option value="Website Package Upgrade">Website Package Upgrade</option>';} ?>
			<?php if ( is_package_1() ) { echo '<option value="Making Tax Digital">Making Tax Digital</option>';} ?>
			<option value="Domain Name">Domain Name</option>
			<option value="Monthly Newsletter">Monthly Newsletter</option>
			<option value="MailChimp Email Marketing">MailChimp Email Marketing</option>
			<option value="Email Issues">Email issues</option>
			<option value="General Enquiry">General Enquiry</option>
		</select>
   </p>
    <p><label>Subject:</label><input type="text" name="support-subject" placeholder="Subject*" required/></p>
    <p><label>Description:</label><textarea name="support-message" placeholder="Message*" required ></textarea></p>
    <input type="text" name="support-security" class="security" value=""/>
    <input type="hidden" name="support-token" value="<?php session_token( 'support' ); ?>"/>
    <p><input type="submit" name="support-submit" value="Submit" class="button button-primary"/></p>
</form>
<?php } ?>