<?php

require_once 'rover-templates.php';

class Rover_IDX_ADMINCUSTOMIZE
	{
	private Rover_IDX $rover_idx;

	public function __construct(Rover_IDX $rover_idx)
		{
		$this->rover_idx						= $rover_idx;
		}

	public function roveridx_panel_styling_form() {

		?>
		<div id="wp_defaults" class="wrap">
		<?php

		$this->rover_idx->COMMON->error_log(__FILE__, __FUNCTION__, __LINE__, 'Starting');

		if (!is_array($theme_options = get_option(roverOPTIONS::THEME)))
			$theme_options						= array();

		$upload_dir								= wp_upload_dir();
		$all_templates							= $this->roveridx_get_all_templates();

		$rover_content							= $this->rover_idx->CONTENT->rover_content(	'ROVER_COMPONENT_STYLING_PANEL',
															array_merge(
																$theme_options,
																array(
																	'region'				=> $this->rover_idx->get_first_region(),
																	'regions'				=> implode(',', array_keys($this->rover_idx->all_selected_regions)),
	//																'settings'				=> $theme_options,
																	'rover_css'				=> null,
																	'wp_menus'				=> $this->roveridx_get_menus($theme_options),
																	'plugin_url'			=> ROVER_IDX_PLUGIN_URL,
																	'upload_url'			=> $upload_dir['baseurl'],
																	'page_templates'		=> implode(",", $all_templates),
																	'all_templates'			=> implode(",", $all_templates),
																	'property_template'		=> (isset($theme_options['property_template'])
																										? $theme_options['property_template']
																										: null),
																	'mc_template'			=> (isset($theme_options['mc_template'])
																										? $theme_options['mc_template']
																										: null),
																	'agent_detail_template'	=> (isset($theme_options['agent_detail_template'])
																										? $theme_options['agent_detail_template']
																										: null),
																	'rep_template'			=> (isset($theme_options['rep_template'])
																										? $theme_options['rep_template']
																										: null),
																	'template'				=> (isset($theme_options['template'])
																										? $theme_options['template']
																										: null)
																	)
																)
															);

		$TEMPLATES								= new Rover_IDX_ADMINTEMPLATES($this->rover_idx);
		?>

			<div id="rover-styling-panel" class="">

				<?php echo $TEMPLATES->roveridx_panel_header();	?>

				<?php echo $rover_content['the_html'];	?>

				<?php echo $TEMPLATES->roveridx_panel_footer($panel = 'styling');	?>

			</div>
		</div><!-- wrap -->


	<?php
		}



	private function roveridx_get_menus($theme_options)	{

		$menu_names								= array();
		if (false)
			{
			foreach(get_registered_nav_menus() as $k => $v)
				$menu_names[]					= wp_get_nav_menu_name($k);
			}
		else if (false)
			{
			foreach ( get_registered_nav_menus() as $location => $description ) {
				$menu_names[]					= $location;
				}
			}
		else
			{
			foreach (wp_get_nav_menus() as $menu) {
				$menu_names[]					= $menu->name;
				}
			}

		return implode(',', $menu_names);
		}

	private function roveridx_get_all_templates()	{

		$all_templates			= array();

		foreach ( get_page_templates() as $template_name => $template_filename ) {
			$all_templates[]	= $template_filename;
			}

		return $all_templates;
		}

	private function roveridx_get_templates($all_templates, $options, $key)	{

		$val					= ($options && isset($options[$key]))
										? $options[$key]
										: null;
		$one_selected			= false;

		$the_html				= array();
		foreach ($all_templates as $one_template)
			{
			$selected			= null;

			if ($one_template == $val)
				{
				$selected 		= 'selected=selected';
				$one_selected 	= true;
				}

			$the_html[]			= "<option value='".$one_template."' ".$selected."> ".$one_template."</option>";
			}

		#	Add as first entry
		array_unshift($the_html , "<option value='".(($one_selected) ? '' : 'selected')."'> Use default page template</option>");

		return implode("", $the_html);
		}
	}


?>