<?php

// Create Options Menu Link
function alpcal_options_menu_link(){
	add_options_page(
		'AlpineCalendar Rev2 Options',
		'AlpineCalendar Rev2',
		'manage_options',
		'alpcal-options',
		'alpcal_options_content'
	);
}

	// Create Content
	function alpcal_options_content(){
	// Init Global Options
	global $alpcal_options;

	$default_alpcal_event_template = 'something will go here later';

// redir url for this site if req
//	$redirect_url = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
	

	$alpcal_options['cache_folder'] = str_replace("ALPCAL/Storage/", "", plugin_dir_path( __FILE__ )) . 'acCache/';
	try {
		$cal = new ALPCAL\Draw\Calendar($alpcal_options);
		$cal->setYearMonth(date("Y"),date("Y")); // needs to run to set the variables to retrieve
		$cal->getData(); // retrieves data and puts in in object
		}
	catch (Exception $exc)
		{
		echo "Exception caught: " . $exc->getMessage();
		die("<hr />Please report or correct this error and reload the page once fixed.");
		}
	
	?>
		<div class="wrap">
			<h2>AlpineCalendar Bootstrap Calendar Rev2 Settings</h2>
			<p>
            	Settings for the AlpineCalendar Bootstrap Calendar - cached revision plugin.
				<br />
				<strong>Shortcode: [alpinecalendarcached]</strong>
            </p>
            <p>
                To manage your events visit the Alpine Calendar Management Page: <a href="http://alpinecalendar.ca" target="_blank">alpinecalendar.ca</a>
            </p>
			<form method="post" action="options.php">
				<?php settings_fields('alpcal_settings_group'); ?>
				<table class="form-table">
					<tbody>
						<tr>
							<th scope="row"><label for="alpcal_settings[which_cal]"><?php _e('Which Calender', 'alpcal-domain'); ?></label></th>
							<td>
                            <select name="alpcal_settings[which_cal]" id="alpcal_settings[which_cal]">
                            	<option value="">Select Province</option>
                            	<option value="ALB" <?php if ($alpcal_options['which_cal']=="ALB") { echo " selected"; } ?>>Alberta</option>
                            	<option value="BCA" <?php if ($alpcal_options['which_cal']=="BCA") { echo " selected"; } ?>>British Columbia</option>
                            	<option value="AOA" <?php if ($alpcal_options['which_cal']=="AOA") { echo " selected"; } ?>>Ontario</option>
                            </select>
							<p class="description" id="alpcal_settings[which_cal]"><?php _e('Select the province\'s calendar you want showing.', 'alpcal-domain'); ?></p></td>
						</tr>
						<tr>
							<th scope="row"><label for="alpcal_settings[first_day]"><?php _e('Week Starts', 'first-day'); ?></label></th>
							<td>
                            <select name="alpcal_settings[first_day]" id="alpcal_settings[first_day]">
                            	<option value="MON" <?php if ($alpcal_options['first_day']=="MON") { echo " selected"; } ?>>Monday</option>
                            	<option value="SUN" <?php if ($alpcal_options['first_day']=="SUN") { echo " selected"; } ?>>Sunday</option>
                            </select>
							<p class="description" id="alpcal_settings[first_day]"><?php _e('Do you want the weeks to start on Sunday or Monday?', 'first-day'); ?></p></td>
						</tr>
						<tr>
							<th scope="row"><label for="alpcal_settings[show_eventTypes]"><?php _e('Show Event Types', 'alpcal-domain'); ?></label></th>
							<td>
                            <select name="alpcal_settings[show_eventTypes]" id="alpcal_settings[show_eventTypes]">
                            	<option value="Y" <?php if ($alpcal_options['show_eventTypes']=="Y") { echo " selected"; } ?>>Yes</option>
                            	<option value="N" <?php if ($alpcal_options['show_eventTypes']=="N") { echo " selected"; } ?>>No</option>
                            </select>
							<p class="description" id="alpcal_settings[show_eventTypes]"><?php _e('Event types may not work with all themes due to scripting conflicts.', 'alpcal-domain'); ?></p></td>
						</tr>
						<tr>
							<th scope="row"><label for="alpcal_settings[cache_length_months]"><?php _e('Month Cache Maximum Age (Seconds)', 'alpcal-domain'); ?></label></th>
							<td>
                            <input
															type="number"
															required
															name="alpcal_settings[cache_length_months]"
															id="alpcal_settings[cache_length_months]"
															value="<?php echo $alpcal_options['cache_length_months']>0 ? $alpcal_options['cache_length_months'] : 300;  ?>"
														/>
							<p class="description" id="alpcal_settings[cache_length_months]"><?php _e('Maximum age (in seconds) to cache months before updating from remote source. (<em class="small">Suggested default is 300</em>)', 'alpcal-domain'); ?></p></td>
						</tr>
						<tr>
							<th scope="row"><label for="alpcal_settings[cache_length_events]"><?php _e('Event Cache Maximum Age (Seconds)', 'alpcal-domain'); ?></label></th>
							<td>
                            <input
															type="number"
															required
															name="alpcal_settings[cache_length_events]"
															id="alpcal_settings[cache_length_events]"
															value="<?php echo $alpcal_options['cache_length_events']>0 ? $alpcal_options['cache_length_events'] : 300;  ?>"
														/>
							<p class="description" id="alpcal_settings[cache_length_events]"><?php _e('Maximum age (in seconds) to cache event details before updating from remote source. (<em class="small">Suggested default is 300</em>)', 'alpcal-domain'); ?></p></td>
						</tr>
						
						<tr>
							<th scope="row"><label for="alpcal_settings[calendars_additional]"><?php _e('Additional calendar feeds:', 'alpcal-domain'); ?></label></th>
							<td>
                            <input
															type="text"
															name="alpcal_settings[calendars_additional]"
															id="alpcal_settings[calendars_additional]"
															value="<?php echo $alpcal_options['calendars_additional'] ? $alpcal_options['calendars_additional'] : '';  ?>"
														/>
								- Category: 
								<select 
									type="text"
									name="alpcal_settings[calendars_additional_categ]"
									id="alpcal_settings[calendars_additional_categ]">
									<option value=""></option>
									<?
									foreach ($cal->EventTypes as $v)
									{
										echo '									<option';
										echo ' value="'.$v->EventTypeName.'~'.$v->EventTypeCLR.'"';
										echo ' style="background-color:#'.$v->EventTypeCLR.';"';
										if ( $alpcal_options['calendars_additional_categ'] == $v->EventTypeName.'~'.$v->EventTypeCLR) { echo ' selected '; }
										echo '>'.$v->EventTypeName.'</option>'."\n";
									}
									?>
								</select>
							<p class="description" id="alpcal_settings[calendars_additional]"><?php _e('Leave this blank by default - this is for sideloading additional calendars and requires the appropriate libraries to be in place. (Changes may not be immediate as cache times above will apply to previously fetched data.)', 'alpcal-domain'); ?></p></td>
						</tr>
						<tr>
							<th scope="row"><label for="alpcal_settings[disableUpdates]"><?php _e('Disable Updates', 'alpcal-domain'); ?></label></th>
							<td>
                            <select name="alpcal_settings[disableUpdates]" id="alpcal_settings[disableUpdates]">
                            	<option value="" <?php if ($alpcal_options['disableUpdates']=="") { echo " selected"; } ?>>No</option>
                            	<option value="Y" <?php if ($alpcal_options['disableUpdates']=="Y") { echo " selected"; } ?>>Yes</option>
                            </select>
							<p class="description" id="alpcal_settings[disableUpdates]"><?php _e('Allows you to lock the plugin and not allow updates to occur.', 'alpcal-domain'); ?></p></td>
						</tr>
						
					</tbody>
				</table>
							
				<p class="submit"><input type="submit" name="submit" id="submit" class="button" value="<?php _e('Save Changes', 'alpcal-domain'); ?>"></p>

<? 
if (1==2)
{
				?>	
				<label for="alpcal_settings[alpcal_event_template]">
					<?php _e('Template for Opened Events', 'alpcal_event_template'); ?>
					<br/>
					<small>
						This area allows you to edit the format of events that open in the calendar from AlpineCalendar.ca.
						Be careful making changes, refer to --- z asdf sdfa s df
					</small>
				</label>
				<br/>
				<textarea
					required
					class="large-text code" rows="8"
					name="alpcal_settings[alpcal_event_template]"
					id="alpcal_settings[alpcal_event_template]"
				><?php echo $alpcal_options['alpcal_event_template'] ? $alpcal_options['alpcal_event_template'] : $default_alpcal_event_template;  ?></textarea>
<?
} // if 1==2
				?>
			</form>
		</div>
	<?php
}

add_action('admin_menu', 'alpcal_options_menu_link');

// Register Settings
function alpcal_register_settings(){
	register_setting('alpcal_settings_group', 'alpcal_settings');
}

add_action('admin_init', 'alpcal_register_settings');
