<div id="custom-notices"></div>
<div class="wrap" id="iris-openwebsite-admin">
	<h1><?php echo PRODUCT_NAME; ?> Options</h1>
	<div id="dashboard-widgets-wrap">
		<div id="dashboard-widgets" class="metabox-holder">

<?php
global $wpdb; //global calls the wordpress database

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

				<?php
//FORM INPUTS
/* TO SANITIZE AND FILTER INPUTS USE:
 *
 * filter_var(htmlspecialchars($_POST['__INSERT_NAME_HERE__']), FILTER_SANITIZE_STRING);
 *
 * USE FILTER_SANITIZE_STRING, FILTER_SANITIZE_EMAIL or FILTER_SANITIZE_URL as necessary
 *
 */

// PREVENT EDITOR UPDATING ADMIN ONLY FIELDS
if ( current_user_can('administrator') ) {
	
	$in_production = htmlspecialchars( $_POST['admin-in-production'] );
	$website_live_request = 0;
	$website_package = htmlspecialchars( $_POST['admin-website-package'] );
	$IRISgoogleA = filter_var( htmlspecialchars( $_POST['admin-iga'] ), FILTER_SANITIZE_STRING );
	$map_api_key = filter_var( htmlspecialchars( $_POST['admin-map-key'] ), FILTER_SANITIZE_STRING );
	$iris_mtd = filter_var( htmlspecialchars( $_POST['admin-mtd'] ), FILTER_SANITIZE_STRING );
	$openspace = filter_var( htmlspecialchars( $_POST['admin-openspace'] ), FILTER_SANITIZE_STRING );
	$kashflow = filter_var( htmlspecialchars( $_POST['admin-kashflow'] ), FILTER_SANITIZE_STRING );
	$paypal_merchant_id = filter_var( htmlspecialchars( $_POST['admin-paypal-merchant-id'] ), FILTER_SANITIZE_STRING );
	$accountant_human_name = filter_var( htmlspecialchars( $_POST['admin-human'] ), FILTER_SANITIZE_STRING );
	$accountant_website = filter_var( htmlspecialchars( $_POST['admin-domain'] ), FILTER_SANITIZE_URL );
	$mailchimp_action = filter_var( htmlspecialchars( $_POST['admin-mailchimp-action'] ), FILTER_SANITIZE_URL );
	$mailchimp_u = filter_var( htmlspecialchars( $_POST['admin-mailchimp-u'] ), FILTER_SANITIZE_STRING );
	$mailchimp_id = filter_var( htmlspecialchars( $_POST['admin-mailchimp-id'] ), FILTER_SANITIZE_STRING );
	
} else {
	
	$in_production = ow_global_var( 'in_production' );
	$website_live_request = ow_global_var( 'website_live_request' );
	$website_package = ow_global_var( 'website_package' );
	$IRISgoogleA = ow_global_var( 'IRISgoogleA' );
	$map_api_key = ow_global_var( 'map_api_key' );
	$iris_mtd = ow_global_var( 'iris_mtd' );
	$openspace = ow_global_var( 'openspace' );
	$kashflow = ow_global_var( 'kashflow' );
	$paypal_merchant_id = ow_global_var( 'paypal_merchant_id' );
	$accountant_human_name = ow_global_var( 'accountant_human_name' );
	$accountant_website = ow_global_var( 'accountant_website' );
	$mailchimp_action = ow_global_var( '$mailchimp_action' );
	$mailchimp_u = ow_global_var( '$mailchimp_u' );
	$mailchimp_id = ow_global_var( '$mailchimp_id' );
}

$accountant_name = filter_var( htmlspecialchars( $_POST['admin-name'] ), FILTER_SANITIZE_STRING );
$accountant_full_name = filter_var( htmlspecialchars( $_POST['admin-full-name'] ), FILTER_SANITIZE_STRING );
$accountant_email = filter_var( htmlspecialchars( $_POST['admin-email'] ), FILTER_SANITIZE_EMAIL );
$form_email = filter_var( htmlspecialchars( $_POST['admin-form-email'] ), FILTER_SANITIZE_EMAIL );
	
if ( $form_email == '' ) {
	$form_email = $accountant_email;
}
	
$accountant_phone = filter_var( htmlspecialchars( $_POST['admin-tel'] ), FILTER_SANITIZE_STRING );
$accountant_phone2 = filter_var( htmlspecialchars( $_POST['admin-tel2'] ), FILTER_SANITIZE_STRING );
$accountant_fax = filter_var( htmlspecialchars( $_POST['admin-fax'] ), FILTER_SANITIZE_STRING );
$accountant_memberships = filter_var( htmlspecialchars( $_POST['admin-memberships'] ), FILTER_SANITIZE_STRING );
$accountant_reg = filter_var( htmlspecialchars( $_POST['admin-company-reg'] ), FILTER_SANITIZE_STRING );
$accountant_reg_country = filter_var( htmlspecialchars( $_POST['admin-company-reg-country'] ), FILTER_SANITIZE_STRING );
$accountant_vat = filter_var( htmlspecialchars( $_POST['admin-company-vat'] ), FILTER_SANITIZE_STRING );
$facebook = filter_var( htmlspecialchars( $_POST['admin-facebook'] ), FILTER_SANITIZE_URL );
$googleplus = filter_var( htmlspecialchars( $_POST['admin-googleplus'] ), FILTER_SANITIZE_URL );
$linkedin = filter_var( htmlspecialchars( $_POST['admin-linkedin'] ), FILTER_SANITIZE_URL );
$twitter = filter_var( htmlspecialchars( $_POST['admin-twitter'] ), FILTER_SANITIZE_URL );
$accountant_address = filter_var( htmlspecialchars( $_POST['admin-address'] ), FILTER_SANITIZE_STRING );
$accountant_main_area = filter_var( htmlspecialchars( $_POST['admin-main-area'] ), FILTER_SANITIZE_STRING );
$googleA = filter_var( htmlspecialchars( $_POST['admin-ga'] ), FILTER_SANITIZE_STRING );
$gpsLocation = filter_var( htmlspecialchars( $_POST['admin-map-gps'] ), FILTER_SANITIZE_STRING );

$username = CLIENT_USERNAME;

if ( $username ) {
	$result = $wpdb->query( "REPLACE INTO ow_global_configuration (gc_option,gc_value) VALUES
		('accountant_id', '$username'),
		('in_production', '$in_production'),
		('website_live_request', $website_live_request),
		('website_package', '$website_package'),
		('accountant_name', '$accountant_name'),
		('accountant_full_name', '$accountant_full_name'),
		('accountant_email', '$accountant_email'),
		('form_email', '$form_email'),
		('accountant_phone', '$accountant_phone'),
		('accountant_phone2', '$accountant_phone2'),
		('accountant_fax', '$accountant_fax'),
		('accountant_memberships', '$accountant_memberships'),
		('company_reg', '$accountant_reg'),
		('company_reg_country', '$accountant_reg_country'),
		('accountant_vat', '$accountant_vat'),
		('accountant_address', '$accountant_address'),
		('accountant_main_area', '$accountant_main_area'),
		('accountant_website', '$accountant_website'),
		('googleA', '$googleA'),
		('IRISgoogleA', '$IRISgoogleA'),
		('map_api_key', '$map_api_key'),
		('gpsLocation', '$gpsLocation'),
		('accountant_human_name', '$accountant_human_name'),
		('facebook', '$facebook'),
		('googleplus', '$googleplus'),
		('linkedin', '$linkedin'),		
		('twitter', '$twitter'),
		('iris_mtd', '$iris_mtd'),
		('openspace', '$openspace'),
		('kashflow', '$kashflow'),
		('paypal_merchant_id', '$paypal_merchant_id'),
		('mailchimp_action', '$mailchimp_action'),
		('mailchimp_u', '$mailchimp_u'),
		('mailchimp_id', '$mailchimp_id')
	");
	if ( $result === false ) { // If this returns false, there is incorrect code. Check all the database column names are correct. 
		echo '<div class="notice notice-error custom-notice"><p><strong>There was an error updating the database. Please contact the administrator.</strong></p></div>';
	} else if ( $result == 0 ) { // This isn't an error
		echo '<div class="notice notice-warning is-dismissible custom-notice"><p><strong>The database hasn\'t been updated as the values haven\'t changed. Please make sure you enter the correct details.</strong></p></div>';		
	} else { // Success message
		update_newsletter_status( $website_package );
		update_client_area_status( $website_package );
		update_tax_tips_status( $website_package );
		echo '<div class="notice notice-success is-dismissible custom-notice"><p><strong>The database has been successfully updated.</strong></p></div>';
	}
} else {
	echo '<div class="notice notice-error custom-notice"><p><strong>Unable to update the database - You haven\'t specified a username.</strong></p></div>';
}
unset( $_POST );
query_db();	
?>
	
<?php } }
//if the form has not been posted	
?>

<?php $access = ( ! current_user_can( 'administrator' ) ) ? 'disabled' : ''; ?>

<form id="form-admin" method="post" action="<?php echo get_permalink(); ?>">
	<div id="postbox-container-1" class="postbox-container">
	<div id="main-sortables" class="meta-box-sortables">
		<div id="website-package" class="postbox">
			<h2 class="hndle"><span class="dashicons dashicons-dashboard"></span> Website Package</h2>
			<div class="inside">
				<p>
					<label>Is website live?</label>
					<span class="help-toggle">i</span>
					<span class="help-content">When you want to make your website live, please get in touch.</span>
					<select <?php echo $access; ?> name="admin-in-production">
						<option value="1" <?php echo ( ow_global_var( 'in_production' ) == 1 ) ? 'selected' : ''; ?>>True</option>
						<option value="0" <?php echo ( ow_global_var( 'in_production' ) == 0 ) ? 'selected' : ''; ?>>False</option>
					</select>
				</p>
				<p>
					<label>Website package:</label>
					<span class="help-toggle">i</span>
					<span class="help-content">This is the website package that you purchased. For upgrades, please get in touch.</span>
					<select <?php echo $access; ?> name="admin-website-package">
						<option value="<?php echo PACKAGE_1; ?>" <?php echo ( ow_global_var( 'website_package' ) == PACKAGE_1 ) ? 'selected' : ''; ?>><?php echo PACKAGE_1; ?></option>
						<option value="<?php echo PACKAGE_2; ?>" <?php echo ( ow_global_var( 'website_package' ) == PACKAGE_2 ) ? 'selected' : ''; ?>><?php echo PACKAGE_2; ?></option>
						<option value="<?php echo PACKAGE_3; ?>" <?php echo ( ow_global_var( 'website_package' ) == PACKAGE_3 ) ? 'selected' : ''; ?>><?php echo PACKAGE_3; ?></option>
						<option value="<?php echo PACKAGE_4; ?>" <?php echo ( ow_global_var( 'website_package' ) == PACKAGE_4 ) ? 'selected' : ''; ?>><?php echo PACKAGE_4; ?></option>
					</select>
				</p>
				<p>
					<label>Has Monthly Newsletter:</label>
					<span class="help-toggle">i</span>
					<span class="help-content">Not all of our website packages come with our Tax Tips &amp; News monthly newsletter included.</span>
					<input type="text" disabled value="<?php echo ( has_newsletter() ) ? 'True' : 'False'; ?>">
				</p>
			</div>
		</div>
		<div id="website-details" class="postbox">
			<h2 class="hndle"><span class="dashicons dashicons-admin-site"></span> Website Details</h2>
			<div class="inside">
        		<p>
					<label class="url">Domain Name: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">If you need to change your domain name, please get in touch.</span>
					<input type="text" <?php echo $access; ?> name="admin-domain" value="<?php echo ow_global_var( 'accountant_website' ); ?>"/>
        		</p>
        		<p>
					<label>Google Analytics Code: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">Google Analytics enables you to track your website usage. For more information about Google Analytics, please see our knowledge base.</span>
					<input type="text" name="admin-ga" value="<?php echo ow_global_var( 'googleA' ); ?>"/>
        		</p>
				<?php if ( current_user_can( 'administrator' ) ) : ?>
					<p><label>IRIS Google Analytics Code: </label><input type="text" name="admin-iga" value="<?php echo ow_global_var( 'IRISgoogleA' ); ?>"/></p>
				<?php endif; ?>
			</div>
		</div>
		<?php if ( current_user_can( 'administrator' ) ) : ?>
			<div id="mailchimp-details" class="postbox">
				<h2 class="hndle"><span class="dashicons dashicons-email"></span> MailChimp Details</h2>
				<div class="inside">
					<p>
						<label class="url">MailChimp URL:</label>
						<input type="text" <?php echo $access; ?> name="admin-mailchimp-action" value="<?php echo ow_global_var( 'mailchimp_action' ); ?>"/>
					</p>
					<p>
						<label class="url">MailChimp User ID (u):</label>
						<input type="text" <?php echo $access; ?> name="admin-mailchimp-u" value="<?php echo ow_global_var( 'mailchimp_u' ); ?>"/>
					</p>
					<p>
						<label class="url">MailChimp Form ID (id):</label>
						<input type="text" <?php echo $access; ?> name="admin-mailchimp-id" value="<?php echo ow_global_var( 'mailchimp_id' ); ?>"/>
					</p>
				</div>
			</div>
   		<?php endif; ?>
    </div>
	</div>
	<div id="postbox-container-2" class="postbox-container">
	<div id="side-sortables" class="meta-box-sortables">
		<div id="contact-details" class="postbox">
			<h2 class="hndle"><span class="dashicons dashicons-location"></span> Contact Details</h2>
			<div class="inside">
				<p>
					<label>Contact Name: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The name of the contact with <?php echo PRODUCT_NAME; ?>. This name is not published on your website.</span>				
					<input type="text" <?php echo $access; ?> name="admin-human" value="<?php echo ow_global_var( 'accountant_human_name' ); ?>"/>
				</p>
				<p>
					<label>Address: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">Your main address. To add multiple address to your site, please get in touch with <?php echo PRODUCT_NAME; ?> Support.</span>
					<textarea name="admin-address"><?php echo ow_global_var( 'accountant_address' ); ?></textarea>
				</p>
				<p>
					<label>Main Area: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The main geographical area you operate. This is used for search engine optimisation.</span>
					<input type="text" name="admin-main-area" value="<?php echo ow_global_var( 'accountant_main_area' ); ?>"/>
				</p>
				<p>
					<label>Published Email: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The email address that will appear on your website.</span>
					<input type="email" name="admin-email" value="<?php echo ow_global_var( 'accountant_email' ); ?>"/>
				</p>
				<p>
				<label>Form Email: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The email address that your website contact forms will send to. This can be different from the published email address.</span>
					<input type="email" name="admin-form-email" value="<?php echo ow_global_var( 'form_email' ); ?>"/>
				</p>
				<p>
					<label>Main Tel: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The published telephone number.</span>
					<input type="tel" name="admin-tel" value="<?php echo ow_global_var( 'accountant_phone' ); ?>"/>
				</p>
				<p>
					<label>Alt Tel: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">An alternative published phone number if applicable.</span>
					<input type="tel" name="admin-tel2" value="<?php echo ow_global_var( 'accountant_phone2' ); ?>"/>
				</p>
				<p>
					<label>Fax No: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The published fax number if applicable.</span>
					<input type="tel" name="admin-fax" value="<?php echo ow_global_var( 'accountant_fax' ); ?>"/>
				</p>
			</div>
		</div>
    </div>
	</div>
	<div id="postbox-container-3" class="postbox-container">
		<div id="column3-sortables" class="meta-box-sortables">
		<div id="company-details" class="postbox">
			<h2 class="hndle"><span class="dashicons dashicons-building"></span> Company Details</h2>
			<div class="inside">
				<?php if ( current_user_can( 'administrator' ) ) : ?>
					<p><label>Client Username: </label><input type="text" disabled value="<?php echo CLIENT_USERNAME; ?>"/></p>
				<?php endif; ?>
				<p>
					<label>Practice Name: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The name you use for your company e.g. Andrew &amp; Brown.</span>
					<input type="text" name="admin-name" value="<?php echo ow_global_var( 'accountant_name' ); ?>"/>
				</p>
				<p>
					<label>Practice Full Name: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The full name you use for your company e.g. Andrews &amp; Brown Chartered Accountants. This can be the same as your Practice Name.</span>
					<input type="text" name="admin-full-name" value="<?php echo ow_global_var( 'accountant_full_name' ); ?>"/>
				</p>
				<p>
					<label>Company Reg Number: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">Your company registration number if applicable.</span>				
					<input type="text" name="admin-company-reg" value="<?php echo ow_global_var( 'company_reg' ); ?>"/>
				</p>
				<p>
					<label>Company Registered in: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The country/countries your company is registered if applicable.</span>	
					<input type="text" list="company-reg-country" name="admin-company-reg-country" value="<?php echo ow_global_var( 'company_reg_country' ); ?>"/>	
					<datalist id="company-reg-country">
						<option value="England">
						<option value="England & Wales">
						<option value="Ireland">
						<option value="Northern Ireland">
						<option value="Scotland">
						<option value="United Kingdom">
					</datalist>
				</p>
				<p>
					<label>Company VAT Number: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">Your company VAT number if applicable.</span>				
					<input type="text" name="admin-company-vat" value="<?php echo ow_global_var( 'accountant_vat' ); ?>"/>
				</p>
				<p>
					<label>Accountant Memberships: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">If you have any memberships such as ICAEW, ACCA, please enter them here. Put each membership onto a new line.</span>
					<textarea name="admin-memberships"><?php echo ow_global_var( 'accountant_memberships' ); ?></textarea>
   				</p>
    		</div>
    	</div>
		<?php if ( current_user_can( 'administrator' ) ) : ?>
			<div id="iris-products" class="postbox">
				<h2 class="hndle"><span class="dashicons dashicons-iris"></span> IRIS Making Tax Digital</h2>
				<div class="inside">
					<p><label>Has customer purchased this add-on? </label>
					<select <?php echo $access; ?> name="admin-mtd" id="mtd-ctrl">
						<option value="1" <?php echo ( ow_global_var( 'iris_mtd' ) == 1 || ! is_package_1() ) ? 'selected' : ''; ?>>True</option>
						<option value="0" <?php echo ( ow_global_var( 'iris_mtd' ) == 0 && is_package_1() ) ? 'selected' : ''; ?>>False</option>
					</select>
					</p>
					<div id="mtd-inputs">
						<p><label>OpenSpace Subdomain: </label><input type="text" <?php echo $access; ?> name="admin-openspace" value="<?php echo ow_global_var( 'openspace' ); ?>"/></p>
						<p><label>KashFlow Unique ID: </label><input type="text" <?php echo $access; ?> name="admin-kashflow" value="<?php echo ow_global_var( 'kashflow' ); ?>"/></p>
						<p><label>PayPal Merchant ID: </label><input type="text" <?php echo $access; ?> name="admin-paypal-merchant-id" value="<?php echo ow_global_var( 'paypal_merchant_id' ); ?>"/></p>
					</div>
				</div>
			</div>
		 <?php endif; ?>
    </div>
	</div>
	<div id="postbox-container-4" class="postbox-container">
		<div id="column4-sortables" class="meta-box-sortables">

        <div id="social-details" class="postbox">
			<h2 class="hndle"><span class="dashicons dashicons-share"></span> Social Media Details</h2>
			<div class="inside">
				<p>
					<label class="url">Facebook URL: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The full URL of your Facebook page. Leave blank if you don't have a Facebook page.</span>
					<input type="text" name="admin-facebook" value="<?php echo ow_global_var( 'facebook' ); ?>"/>
				</p>
				<p>
					<label class="url">Google+ URL: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The full URL of your Google+ page. Leave blank if you don't have a Google+ page.</span>
					<input type="text" name="admin-googleplus" value="<?php echo ow_global_var( 'googleplus' ); ?>"/>
				</p>
				<p>
					<label class="url">LinkedIn URL: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The full URL of your LinkedIn page. Leave blank if you don't have a LinkedIn page.</span>
					<input type="text" name="admin-linkedin" value="<?php echo ow_global_var( 'linkedin' ); ?>"/>
				</p>
				<p>
					<label class="url">Twitter URL: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The full URL of your Twitter page. Leave blank if you don't have a Twitter page.</span>
					<input type="text" name="admin-twitter" value="<?php echo ow_global_var( 'twitter' ); ?>"/>
				</p>
			</div>
		</div>
        <div id="social-details" class="postbox">
			<h2 class="hndle"><span class="dashicons dashicons-location-alt"></span> Map Details</h2>
			<div class="inside">
				<?php if ( current_user_can( 'administrator' ) ) : ?>
					<p><label>Google Map API Key: </label><input type="text" <?php echo $access; ?> name="admin-map-key" value="<?php echo ow_global_var( 'map_api_key' ); ?>"/></p>
				<?php endif; ?>
				<p>
					<label>Google Map Latitude &amp; Longitude: </label>
					<span class="help-toggle">i</span>
					<span class="help-content">The latitude and longitude co-ordinates of your practice e.g. 51.4869709, -0.5722636. To find out your latitude &amp; longitude, please see our knowledge base.</span>
					<input type="text" name="admin-map-gps" value="<?php echo ow_global_var( 'gpsLocation' ); ?>"/>
				</p>
			</div>
		</div>
        <div id="update-details" class="postbox">
			<h2 class="hndle"><span class="dashicons dashicons-upload"></span> Update Options</h2>
			<div class="inside">
				<p class="alert"><strong>Changing the above options will take effect immediately. Please ensure all options are correctly filled out before clicking update.</strong></p>
				<p><input type="checkbox" id="confirm-update" required/><label for="confirm-update">Are you sure you want to update your options</label></p>
				<p><input type="submit" name="admin-submit" value="Update Options" class="button button-primary"/></p>
			</div>
		</div>
    </div>
	</div>
</form>

</div>
</div>
</div>
