<?php

/**
 * Admin Page Template for Author Kit
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}


$preview_url = author_kit_get_customizer_preview_url( null, 'bio' );

$customizer_url = admin_url( 'customize.php' ) .
	'?autofocus[section]=author_kit_bio_box_section' .
	'&url=' . urlencode( $preview_url );

// Define available templates
$templates = array(
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-01.png',
		'title'       => 'Default',
		'template_id' => 'template-default',
		'category'    => 'author_bio',
		'customizer'  => $customizer_url,
	),
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-02.png',
		'title'       => 'Memoir',
		'template_id' => 'template-memoir',
		'category'    => 'author_bio',
		'customizer'  => $customizer_url,
	),
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-03.png',
		'title'       => 'Monarch',
		'template_id' => 'template-monarch',
		'category'    => 'author_bio',
		'customizer'  => $customizer_url,
	),
);

// Get the currently activated template
$activated_template = get_option( 'author_kit_author_bio_template', 'template-default' );
// Function to sort templates with the activated one first

// Sort templates so the activated one is first
$sorted_templates = author_kit_sort_templates( $templates, $activated_template );

// Fetch display option
$auto_display    = get_option( 'author_kit_bio_display', true );
$display_message = $auto_display ?
	__( 'Automatically add an Author Bio box to posts! Style it live with the Customizer.', 'author-kit' ) :
	__( 'Manually insert an Author Bio box to Posts/Pages using a shortcode! Style it live with the Customizer.', 'author-kit' );
?>

<div id="enable-disable-option-header">
	<div>
		<h3><?php esc_html_e( 'Display Author Bio Box', 'author-kit' ); ?></h3>
		<p id="template-activation-message-bio"><?php echo esc_html( $display_message ); ?></p>
	</div>
	<div class="switch-container">
		<label class="switch">
			<input type="checkbox" id="awp-enable-template-bio-box-switch" <?php echo $auto_display ? 'checked' : ''; ?>>
			<span class="slider round"></span>
		</label>
	</div>
</div>

<div class="grid-container" id="grid-container">
	<?php foreach ( $sorted_templates as $template ) :
		$template_id     = isset( $template['template_id'] ) ? sanitize_key( $template['template_id'] ) : '';
		$title           = isset( $template['title'] ) ? sanitize_text_field( $template['title'] ) : '';
		$img_src         = isset( $template['imgSrc'] ) ? esc_url( $template['imgSrc'] ) : '';
		$category        = isset( $template['category'] ) ? sanitize_text_field( $template['category'] ) : '';
		$customizer_url  = isset( $template['customizer'] ) ? esc_url( $template['customizer'] ) : '';

		$is_active       = ( $template_id === $activated_template );
		$button_class    = $is_active ? 'ct-button-success active-template' : 'ct-button-primary';
		$is_disabled     = $is_active ? 'disabled' : '';
		$is_customizable = author_kit_kirki_active() && ! empty( $customizer_url );
		?>
		<div class="ct-single-demo">
			<figure>
				<img 
				src="<?php // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
				echo esc_url($img_src); ?>" 
				alt="<?php echo esc_attr( $title ); ?>" 
				loading="lazy">
			</figure>

			<div class="ct-demo-actions">
				<h4><?php echo esc_html( $title ); ?></h4>
				<div class="ct-demo-buttons">
					<?php if ( $is_active ) : ?>
						<a class="ct-button <?php echo esc_attr($is_customizable) ? 'ct-button-link' : 'disabled'; ?>"
						   target="_blank"
						   href="<?php echo esc_attr($is_customizable) ? esc_attr($customizer_url): '#'; ?>"
						   rel="noopener noreferrer">
							<?php esc_html_e( 'Customize', 'author-kit' ); ?>
						</a>
					<?php else : ?>
						<button type="button"
								class="ct-button preview-button">
							<?php esc_html_e( 'Free', 'author-kit' ); ?>
						</button>
					<?php endif; ?>

					<button type="button"
							class="ct-button-activate <?php echo esc_attr( $button_class ); ?>"
							data-template-id="<?php echo esc_attr( $template_id ); ?>"
							data-template-title="<?php echo esc_attr( $title ); ?>"
							data-template-category="<?php echo esc_attr( $category ); ?>"
							data-nonce="<?php echo esc_attr( wp_create_nonce( 'author_kit_template_activation' ) ); ?>"
							onclick="activateTemplate(this)"
							<?php disabled( $is_active ); ?>>
						<?php echo $is_active ? esc_html__( 'Activated', 'author-kit' ) : esc_html__( 'Activate', 'author-kit' ); ?>
					</button>
				</div>
			</div>
		</div>
	<?php endforeach; ?>
</div>

<?php if ( function_exists( 'author_kit_render_error' ) ) : ?>
	<div class="customizer-error-message">
		<?php echo wp_kses_post( author_kit_render_error() ); ?>
	</div>
<?php endif; ?>
