<?php
$medium_allowed_frontend_templates = $this->medium->getFrontendTemplates();
?>

<div id="anchor-usage-section" class="toolset-user-editors-beaver-backend">
	<h3><?php echo sprintf( __( '%1$s is active.', 'wpv-views' ), $this->editor->getName() ); ?></h3>

	<?php
	/*
	 * NO allowed frontend template
	 * happens e.g. for Content Templates when no item under 'Usage' is selected
	 */
	if( empty( $medium_allowed_frontend_templates ) ): ?>

		<p>
			<?php
				echo sprintf( 
					__( 'Before you can edit with %1$s, you need to <a href="#anchor-usage-section">choose what type this Content Template will display</a>.', 'wpv-views' ),
					$this->editor->getName()
				);
			?>
		</p>

	<?php
	/*
	 * ONE allowed frontend template
	 * in this case we don't create a selection
	 * and place the single template in a hidden input
	 */
	elseif( count( $medium_allowed_frontend_templates ) == 1 ):
		$template = array_pop( $medium_allowed_frontend_templates ); ?>

		<input id="toolset-beaver-builder-single-template"
		       type="hidden" value="<?php echo $template['path']; ?>"
		       data-preview-domain="<?php echo $template['domain']; ?>"
		       data-preview-slug="<?php echo $template['slug']; ?>" />

		<div class="toolset-user-editors-beaver-spinner">
			<img src="<?php echo site_url(); ?>/wp-admin/images/spinner.gif" />
		</div>

		<?php // Button "Start Beaver Frontend Editor"
		FLBuilderAdminPosts::render(); ?>

	<?php
	/*
	 * MULTIPLE allowed frontend templates
	 * user gets an select of all available templates
	 */
	else:
		$stored_template_path = get_post_meta( $this->medium->getId(), $this->editor->getOptionName(), true ); ?>

		<p>
			<?php
				_e( 'This Content Template is used for several types. '
				. 'Choose which type you want to see in the editor:', 'wpv-views' );
			?>
		</p>


		<p>
			<select id="toolset-user-editors-beaver-template-file">
				<?php foreach( $medium_allowed_frontend_templates as $template ):
						$selected = ( $template['path'] == $stored_template_path['template_path'] ) 
							? ' selected="selected"'
							: ''; ?>

					<option value="<?php echo $template['path'] ?>"<?php echo $selected; ?>
							data-preview-domain="<?php echo $template['domain']; ?>"
					        data-preview-slug="<?php echo $template['slug']; ?>">
						<?php echo $template['form-option-label']; ?>
					</option>
				<?php endforeach; ?>
			</select>
		</p>

		<div class="toolset-user-editors-beaver-spinner">
			<img src="<?php echo site_url(); ?>/wp-admin/images/spinner.gif" />
		</div>

		<?php // Button "Start Beaver Frontend Editor"
		FLBuilderAdminPosts::render();

	endif; ?>

</div>
