<?php

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

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

// Get latest post URL (bio)
$preview_url    = author_kit_get_customizer_preview_url( null, 'bio' );
$customizer_url = admin_url( 'customize.php' ) .
	'?autofocus[section]=author_kit_custom_byline_section' .
	'&url=' . urlencode( $preview_url );
// Define templates
$byline_templates = array(
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-07.png',
		'title'       => 'Default',
		'btnText'     => 'Activate',
		'template_id' => 'byline-default',
		'category'    => 'custom_byline',
		'customizer'  => $customizer_url,
	),
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-08.png',
		'title'       => 'Elantra',
		'btnText'     => 'Activate',
		'template_id' => 'template-elantra',
		'category'    => 'custom_byline',
		'customizer'  => $customizer_url,
	),
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-09.png',
		'title'       => 'Advo',
		'btnText'     => 'Activate',
		'template_id' => 'template-advo',
		'category'    => 'custom_byline',
		'customizer'  => $customizer_url,
	),
);

$byline_activated_template = get_option( 'author_kit_custom_byline_template', 'byline-default' );
$byline_sorted_templates   = author_kit_sort_templates( $byline_templates, $byline_activated_template );
?>

<div id="enable-disable-option-header">
	<div>
		<h3><?php esc_html_e( 'Display Advanced Custom Bylines', 'author-kit' ); ?></h3>
		<?php
		$byline_post_display = get_option( 'author_kit_byline_display', false );

		$message = $byline_post_display
			? esc_html__( 'Automatically add a custom byline to posts! Style it live with the Customizer.', 'author-kit' )
			: esc_html__( 'Manually insert a custom byline into posts using a shortcode! Style it live with the Customizer.', 'author-kit' );

		echo '<p id="template-activation-message-byline">' . esc_html( $message ) . '</p>';
		?>
	</div>


	<div class="switch-container">
		<label class="switch">
			<input type="checkbox" id="awp-enable-template-byline-switch">
			<span class="slider round"></span>
		</label>
	</div>
</div>


<div class="grid-container" id="grid-container">
	<?php foreach ( $byline_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 === $byline_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 custom-byline-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; ?>
