<?php

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

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

// Get author archive page
$archive_url    = author_kit_get_customizer_preview_url( null, 'archive_page' );
$customizer_url = admin_url( 'customize.php' ) .
	'?autofocus[section]=author_kit_archive_section' .
	'&url=' . urlencode( $archive_url );

$archive_activated_template = get_option( 'author_kit_author_archive_template', 'archive-template-default' );
$archive_page_display       = get_option( 'author_kit_archive_display', false );

// Template list
$archive_templates = array(
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-04.png',
		'title'       => 'Default',
		'template_id' => 'archive-template-default',
		'category'    => 'author_archive',
		'customizer'  => $customizer_url,
	),
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-05.png',
		'title'       => 'Scribe',
		'template_id' => 'archive-template-scribe',
		'category'    => 'author_archive',
		'customizer'  => $customizer_url,
	),
	array(
		'imgSrc'      => AUTHOR_KIT_PLUGIN_URL . 'admin/assets/files/images/author-kit-previews-06.png',
		'title'       => 'Quill',
		'template_id' => 'archive-template-quill',
		'category'    => 'author_archive',
		'customizer'  => $customizer_url,
	),
);

// Sort templates based on activation status
$archive_sorted_templates = author_kit_sort_templates( $archive_templates, $archive_activated_template );
?>

<div id="enable-disable-option-header">
	<div>
		<h3><?php esc_html_e( 'Display Author Archive Page', 'author-kit' ); ?></h3>
		<p id="template-activation-message-archive">
			<?php
			echo $archive_page_display ?
				esc_html__( 'Automatically add an Author Bio box to posts! Style it live with the Customizer.', 'author-kit' ) :
				esc_html__( 'Manually insert an Author Bio box to Posts/Pages using a shortcode! Style it live with the Customizer.', 'author-kit' );
			?>
		</p>
	</div>
	<div class="switch-container">
		<label class="switch">
			<input type="checkbox" id="awp-enable-template-bio-archive-switch" <?php echo $archive_page_display ? 'checked' : ''; ?>>
			<span class="slider round"></span>
		</label>
	</div>
</div>
<div class="grid-container" id="grid-container">
	<?php foreach ( $archive_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 === $archive_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 snipcss-tvGig">
			<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 archive-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; ?>
