<?php
$themename = "THEME ";

$shortname = "p2h";

$version = "1.1";

$option_group = $shortname.'_theme_option_group';
$option_name = $shortname.'_theme_options';

// Load stylesheet and jscript
add_action('admin_init', 'p2h_add_init');

function p2h_add_init() {
	$file_dir = get_template_directory_uri();

	wp_enqueue_style("p2hCss", $file_dir."/options/style.css", false, "1.0", "all");

	wp_enqueue_script("p2hScript", $file_dir."/options/custom.js", false, "1.0");

}

// Create custom settings menu
add_action('admin_menu', 'p2h_create_menu');

function p2h_create_menu() {
	global $themename;
	//create new top-level menu
	add_theme_page( __( $themename.' Homepage Options' ), __( 'Theme Options' ), 'edit_theme_options', basename(__FILE__), 'p2h_settings_page' );
}

// Register settings
add_action( 'admin_init', 'register_settings' );

function register_settings() {
   global $themename, $shortname, $version, $p2h_options, $option_group, $option_name;
  	//register our settings
	register_setting( $option_group, $option_name);
}

// Create theme options 

global $p2h_options;

$p2h_options = array (
	//  Contect Details
 array("name" => __(' Contect Details','ma'),
		"type" => "section"),
  array("name" => __('Your header Address and email id and phone no. add from here ','ma'),
		"type" => "section-desc"),
  array("type" =>"open"),
	      
			array("name" => __('Phone No ','ma'),
				"desc" => __('Current Phone No','ma'),
				"id" => "phone",
				"std" => "",
				"type" => "text"),

		   array("name" => __('E-mail','ma'),
				"desc" => __('Use business email only.', 'ma'),
				"id" => "email",
				"type" => "text",
				"std" => ""),
	
	array("name" => __('Address','ma'),
				"desc" => __('Address only.', 'ma'),
				"id" => "address",
				"type" => "text",
				"std" => ""),
	
	array("name" => __('Footer Text','ma'),
				"desc" => __('Footer Text only.', 'ma'),
				"id" => "f-text",
				"type" => "text",
				"std" => ""),

	array("type" => "close"),
	
	
		//  slider Details
 array("name" => __(' Slider Details','ma'),
		"type" => "section"),
  array("name" => __('Slider Details here ','ma'),
		"type" => "section-desc"),
  array("type" =>"open"),
	      
			array("name" => __('slider1 image ','ma'),
				"desc" => __('slider1 image','ma'),
				"id" => "slider1-img",
				"std" => "",
				"type" => "text"),

		   array("name" => __('Slider1 Heading','ma'),
				"desc" => __('Slider1 Heading.', 'ma'),
				"id" => "slider1-head",
				"type" => "text",
				"std" => ""),
	
	  array("name" => __('Slider1 Text','ma'),
				"desc" => __('Slider1 Text.', 'ma'),
				"id" => "slider1-text",
				"type" => "textarea",
				"std" => ""),
	
	
	    array("name" => __('slider2 image ','ma'),
				"desc" => __('slider2 image','ma'),
				"id" => "slider2-img",
				"std" => "",
				"type" => "text"),

		   array("name" => __('Slider2 Heading','ma'),
				"desc" => __('Slider2 Heading.', 'ma'),
				"id" => "slider2-head",
				"type" => "text",
				"std" => ""),
	
	  array("name" => __('Slider2 Text','ma'),
				"desc" => __('Slider2 Text.', 'ma'),
				"id" => "slider2-text",
				"type" => "textarea",
				"std" => ""),

	array("type" => "close"),
	
	
		//  About Details
 array("name" => __(' About Details','ma'),
		"type" => "section"),
  array("name" => __('About Details here ','ma'),
		"type" => "section-desc"),
  array("type" =>"open"),
	      
			array("name" => __('About Images ','ma'),
				"desc" => __('About Images','ma'),
				"id" => "about-img",
				"std" => "",
				"type" => "text"),

		  array("name" => __('About Heading ','ma'),
				"desc" => __('About Heading','ma'),
				"id" => "about-head",
				"std" => "",
				"type" => "text"),
	
	array("name" => __('About Text ','ma'),
				"desc" => __('About Text','ma'),
				"id" => "about-text",
				"std" => "",
				"type" => "textarea"),

	array("type" => "close"),
	
	//  OPD Timings Details
 array("name" => __(' OPD Timings Details','ma'),
		"type" => "section"),
  array("name" => __('OPD Timings here ','ma'),
		"type" => "section-desc"),
  array("type" =>"open"),
	      
			array("name" => __('Morning ','ma'),
				"desc" => __('Morning','ma'),
				"id" => "morning",
				"std" => "",
				"type" => "text"),

		   array("name" => __('Evening','ma'),
				"desc" => __('Evening.', 'ma'),
				"id" => "evening",
				"type" => "text",
				"std" => ""),
	
	  array("name" => __('Emergency Services','ma'),
				"desc" => __('Emergency Services.', 'ma'),
				"id" => "e-services",
				"type" => "text",
				"std" => ""),

	array("type" => "close"),

//health 24/7 Heading
array("name" => __('Health 24/7 Area','ma'),
		"type" => "section"),
     array("name" => __('Health 24/7 text  ','ma'),
		"type" => "section-desc"),
              array("type" =>"open"),
								array("name" => __('Health 24/7 Heading','ma'),
										"desc" => __('Health 24/7 Heading.', 'ma'),
										"id" => "health-head",
										"type" => "textarea",
										"std" => ""),

	       array("type" =>"open"),
								array("name" => __('Health 24/7 No','ma'),
										"desc" => __('Health 24/7 No.', 'ma'),
										"id" => "health-no",
										"type" => "text",
										"std" => ""),
	      
	
array("type" => "close"),

//SOCIAL MEDIA BUTTONS	

array("name" => __('Footer Social Media Links','ma'),
		"type" => "section"),
    array("name" => __('Set the links to the social media accounts you have and the appropriate icons will appear.','ma'),
		"type" => "section-desc"),
					array("type" => "open"),
										array("name" => __('Facebook','ma'),
												"desc" => __('Facebook Fontawesome class here','ma'),
												"id" => "facebookid",
												"type" => "text",
												"std" => ""),

										array("name" => __('Twitter','ma'),
												"desc" => __('Twitter Fontawesome class here','ma'),
												"id" => "twitterid",
												"type" => "text",
												"std" => ""),

										array("name" => __('Linkedin','ma'),
												"desc" => __('Linkedin Fontawesome class here','ma'),
												"id" => "linkedinid",
												"type" => "text",
												"std" => ""),		

										
	

					array("type" => "close"),
);


function p2h_settings_page() {
   global $themename, $shortname, $version, $p2h_options, $option_group, $option_name;
?>

<div class="wrap">
<div class="options_wrap">
<?php screen_icon(); ?><h2><?php echo $themename; ?> <?php _e(' Options','ma'); ?></h2>
<p class="top-notice"><?php _e('Fully Customize Website Powerby Om Soft Solution. ','ma'); ?></p>
<?php if ( isset ( $_POST['reset'] ) ): ?>
<?php // Delete Settings
global $wpdb, $themename, $shortname, $version, $p2h_options, $option_group, $option_name;
delete_option('p2h_theme_options');
wp_cache_flush(); ?>
<div class="updated fade"><p><strong><?php _e( $themename. ' options reset.' ); ?></strong></p></div>

<?php elseif ( isset ( $_REQUEST['updated'] ) ): ?>
<div class="updated fade"><p><strong><?php _e( $themename. ' options saved.' ); ?></strong></p></div>
<?php endif; ?>

<form method="post" action="options.php">

<?php settings_fields( $option_group ); ?>

<?php $options = get_option( $option_name ); ?>

<?php foreach ($p2h_options as $value) {
if ( isset($value['id']) ) { $valueid = $value['id'];}

switch ( $value['type'] ) {

case "section":

?>

	<div class="section_wrap">
	<h3 class="section_title"><?php echo $value['name']; ?>
<?php break;

case "section-desc":

?>

	<span><?php echo $value['name']; ?></span></h3>

	<div class="section_body">

<?php

break;

case 'text':

?>
		
	<div class="options_input options_text">

		<div class="options_desc"><?php echo $value['desc']; ?></div>

		<span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

		<input name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( isset( $options[$valueid]) ){ esc_attr_e($options[$valueid]); } else { esc_attr_e($value['std']); } ?>" />

	</div>

<?php

break;

case 'textarea':

?>
	<div class="options_input options_textarea">

		<div class="options_desc"><?php echo $value['desc']; ?></div>

		<span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

		<textarea name="<?php echo $option_name.'['.$valueid.']'; ?>" type="<?php echo $option_name.'['.$valueid.']'; ?>" cols="" rows=""><?php if ( isset( $options[$valueid]) ){ esc_attr_e($options[$valueid]); } else { esc_attr_e($value['std']); } ?></textarea>

	</div>

<?php

break;
		
case 'select':

?>

	<div class="options_input options_select">

		<div class="options_desc"><?php echo $value['desc']; ?></div>

		<span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

		<select name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>">

		<?php foreach ($value['options'] as $option) { ?>

				<option <?php if ($options[$valueid] == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>

		</select>

	</div>

<?php

break;

case "radio":

?>

	<div class="options_input options_select">

		<div class="options_desc"><?php echo $value['desc']; ?></div>

		<span class="labels"><label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label></span>

		  <?php foreach ($value['options'] as $key=>$option) { 
			if ( isset ( $options[$valueid] ) ) {
				if ($key == $options[$valueid] ) {
					$checked = "checked=\"checked\"";
					} else {
						$checked = "";
					}
			}else{
				if($key == $value['std']){
					$checked = "checked=\"checked\"";
				}else{
					$checked = "";
				}
			}?>

			<input type="radio" id="<?php echo $option_name.'['.$valueid.']'; ?>" name="<?php echo $option_name.'['.$valueid.']'; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />

			<?php } ?>

	</div>

<?php

break;

case "checkbox":

?>

	<div class="options_input options_checkbox">

		<div class="options_desc"><?php echo $value['desc']; ?></div>

		<?php if( isset( $options[$valueid] ) ){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>

		<input type="checkbox" name="<?php echo $option_name.'['.$valueid.']'; ?>" id="<?php echo $option_name.'['.$valueid.']'; ?>" value="true" <?php echo $checked; ?> />

		<label for="<?php echo $option_name.'['.$valueid.']'; ?>"><?php echo $value['name']; ?></label>

	 </div>

<?php

break;

case "close":

?>

</div><!--#section_body-->

</div><!--#section_wrap-->

<?php
break;

}

}

?>

<span class="submit">
<input class="button button-primary" type="submit" name="save" value="<?php _e('Save All Changes', 'ma') ?>" />
</span>
</form>

<form method="post" action="">

<span class="button-right" class="submit">

<input class="button button-secondary" type="submit" name="reset" value="<?php _e('Reset/Delete Settings', 'ma') ?>" />
<input type="hidden" name="action" value="reset" />
<span><?php _e('Caution: All entries will be deleted from database. Press when starting afresh or completely removing the theme.','ma') ?></span>

</span>

</form>
</div><!--#options-wrap-->
</div>

<?php } ?>