<?php

/**
 * Provide a admin area view for the Templates page
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://www.cspgeeks.com
 * @since      1.1.0
 *
 * @package    Basic_Seo_Helper
 * @subpackage Basic_Seo_Helper/admin/partials
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variables are scoped to this included file, not global scope

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

$options = get_option( 'cspgbase_options', array() );

// Handle form submission
if ( isset( $_POST['submit'] ) && isset( $_POST['basic_seo_helper_templates_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['basic_seo_helper_templates_nonce'] ) ), 'basic_seo_helper_templates_settings' ) ) {
	// Template settings
	$template_options = array(
		'cspgbase_default_title_template'          => isset( $_POST['cspgbase_default_title_template'] ) ? sanitize_text_field( wp_unslash( $_POST['cspgbase_default_title_template'] ) ) : '',
		'cspgbase_default_description_template'    => isset( $_POST['cspgbase_default_description_template'] ) ? sanitize_textarea_field( wp_unslash( $_POST['cspgbase_default_description_template'] ) ) : '',
		'cspgbase_default_og_title_template'       => isset( $_POST['cspgbase_default_og_title_template'] ) ? sanitize_text_field( wp_unslash( $_POST['cspgbase_default_og_title_template'] ) ) : '',
		'cspgbase_default_og_description_template' => isset( $_POST['cspgbase_default_og_description_template'] ) ? sanitize_textarea_field( wp_unslash( $_POST['cspgbase_default_og_description_template'] ) ) : '',
		'cspgbase_fallback_text'                   => isset( $_POST['cspgbase_fallback_text'] ) ? sanitize_text_field( wp_unslash( $_POST['cspgbase_fallback_text'] ) ) : '',
		'cspgbase_title_separator'                 => isset( $_POST['cspgbase_title_separator'] ) ? sanitize_text_field( wp_unslash( $_POST['cspgbase_title_separator'] ) ) : '',
	);

	// Update template options
	foreach ( $template_options as $option_name => $option_value ) {
		update_option( $option_name, $option_value );
	}

	echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Template settings saved successfully!', 'cspg-basic-seo-helper' ) . '</p></div>';

	// Add flag to indicate form was submitted successfully
	wp_add_inline_script( 'cspg-basic-seo-helper-templates', 'bshTemplates.templatesSaved = true;' );
}
?>

<div class="wrap">
	<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
	
	<div class="basic-seo-helper-admin-wrapper">
		<!-- Dynamic Template Settings -->
		<div class="postbox">
			<div class="postbox-header">
				<h2><?php esc_html_e( 'Dynamic Template Settings', 'cspg-basic-seo-helper' ); ?></h2>
			</div>
			<div class="inside">
				<form method="post" action="">
					<?php wp_nonce_field( 'basic_seo_helper_templates_settings', 'basic_seo_helper_templates_nonce' ); ?>
					
					<p><?php esc_html_e( 'Use dynamic variables to create consistent meta titles and descriptions across your site. Variables will be automatically replaced with actual content when pages are rendered.', 'cspg-basic-seo-helper' ); ?></p>
				
				<table class="form-table" role="presentation">
					<tbody>
						<tr>
							<th scope="row">
								<label for="cspgbase_default_title_template"><?php esc_html_e( 'Default Meta Title Template', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<input type="text" 
										id="cspgbase_default_title_template" 
										name="cspgbase_default_title_template" 
										value="<?php echo esc_attr( get_option( 'cspgbase_default_title_template', '{post_title} {separator} {site_name}' ) ); ?>" 
										class="regular-text bsh-template-field" />
								<div class="bsh-tag-buttons" data-target="cspgbase_default_title_template">
									<button type="button" class="bsh-tag-btn" data-tag="{post_title}">{post_title}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{site_name}">{site_name}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{separator}">{separator}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{category}">{category}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{author}">{author}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{date}">{date}</button>
								</div>
								<p class="description"><?php esc_html_e( 'Default template for page titles when no custom title is set.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<label for="cspgbase_default_description_template"><?php esc_html_e( 'Default Meta Description Template', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<textarea id="cspgbase_default_description_template" 
											name="cspgbase_default_description_template" 
											rows="3" 
											class="regular-text bsh-template-field"><?php echo esc_textarea( get_option( 'cspgbase_default_description_template', '{excerpt}' ) ); ?></textarea>
								<div class="bsh-tag-buttons" data-target="cspgbase_default_description_template">
									<button type="button" class="bsh-tag-btn" data-tag="{excerpt}">{excerpt}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{post_title}">{post_title}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{category}">{category}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{author}">{author}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{site_name}">{site_name}</button>
									<button type="button" class="bsh-tag-btn" data-tag="{date}">{date}</button>
								</div>
								<p class="description"><?php esc_html_e( 'Default template for meta descriptions when no custom description is set.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<label for="cspgbase_default_og_title_template"><?php esc_html_e( 'Default Open Graph Title Template', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<input type="text" 
										id="cspgbase_default_og_title_template" 
										name="cspgbase_default_og_title_template" 
										value="<?php echo esc_attr( get_option( 'cspgbase_default_og_title_template', '{post_title}' ) ); ?>" 
										class="regular-text" />
								<p class="description"><?php esc_html_e( 'Template for Open Graph titles used in social media sharing.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<label for="cspgbase_default_og_description_template"><?php esc_html_e( 'Default Open Graph Description Template', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<textarea id="cspgbase_default_og_description_template" 
											name="cspgbase_default_og_description_template" 
											rows="3" 
											class="regular-text"><?php echo esc_textarea( get_option( 'cspgbase_default_og_description_template', '{excerpt}' ) ); ?></textarea>
								<p class="description"><?php esc_html_e( 'Template for Open Graph descriptions used in social media sharing.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<label for="cspgbase_fallback_text"><?php esc_html_e( 'Fallback Text', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<input type="text" 
										id="cspgbase_fallback_text" 
										name="cspgbase_fallback_text" 
										value="<?php echo esc_attr( get_option( 'cspgbase_fallback_text', 'Visit {site_name} for the latest updates.' ) ); ?>" 
										class="regular-text" />
								<p class="description"><?php esc_html_e( 'Text to use when template variables cannot be populated.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
						
						<tr>
							<th scope="row">
								<label for="cspgbase_title_separator"><?php esc_html_e( 'Title Separator', 'cspg-basic-seo-helper' ); ?></label>
							</th>
							<td>
								<select id="cspgbase_title_separator" name="cspgbase_title_separator">
									<?php
									$current_separator = get_option( 'cspgbase_title_separator', '|' );
									$separators        = array(
										'|' => '| (Pipe)',
										'-' => '- (Dash)',
										'–' => '– (En Dash)',
										'—' => '— (Em Dash)',
										'•' => '• (Bullet)',
										'/' => '/ (Slash)',
										':' => ': (Colon)',
										'>' => '> (Greater Than)',
									);
									foreach ( $separators as $value => $label ) {
										echo '<option value="' . esc_attr( $value ) . '"' . selected( $current_separator, $value, false ) . '>' . esc_html( $label ) . '</option>';
									}
									?>
								</select>
								<p class="description"><?php esc_html_e( 'Separator to use in titles between elements.', 'cspg-basic-seo-helper' ); ?></p>
							</td>
						</tr>
					</tbody>
				</table>
				
				<h3><?php esc_html_e( 'Preview', 'cspg-basic-seo-helper' ); ?></h3>
				<div id="bsh-template-preview" class="bsh-preview-box">
					<p><strong><?php esc_html_e( 'Title Preview:', 'cspg-basic-seo-helper' ); ?></strong> <span id="bsh-title-preview"><?php esc_html_e( 'Loading...', 'cspg-basic-seo-helper' ); ?></span></p>
					<p><strong><?php esc_html_e( 'Description Preview:', 'cspg-basic-seo-helper' ); ?></strong> <span id="bsh-description-preview"><?php esc_html_e( 'Loading...', 'cspg-basic-seo-helper' ); ?></span></p>
				</div>
				
				<?php submit_button(); ?>
				</form>
			</div>
		</div>
		
		<!-- Template Variables Guide -->
		<div class="bsh-seo-elements-guide">
			<div class="bsh-guide-wrapper">
				<div class="bsh-guide-header">
					<h2><span class="bsh-guide-icon">📝</span><?php esc_html_e( 'Template Variables Guide', 'cspg-basic-seo-helper' ); ?></h2>
					<p><?php esc_html_e( 'Template variables are dynamic placeholders that automatically get replaced with actual content from your posts, pages, and site settings. Think of them as smart shortcuts that help you create consistent, professional meta tags across your entire website.', 'cspg-basic-seo-helper' ); ?></p>
				</div>
				
				<div class="bsh-guide-columns">
					<div class="bsh-guide-column">
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">📄</span><?php esc_html_e( 'Content Variables', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'What They Do:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'These variables pull content directly from your posts and pages, ensuring your meta tags always match your actual content.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Available Variables:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <code>{post_title}</code> - <?php esc_html_e( 'The title of the current post or page', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{excerpt}</code> - <?php esc_html_e( 'Post excerpt or auto-generated content summary', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{post_type}</code> - <?php esc_html_e( 'Type of content (Post, Page, Product, etc.)', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{category}</code> - <?php esc_html_e( 'Primary category of the post', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{author}</code> - <?php esc_html_e( 'Name of the content author', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Best Use Cases:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Perfect for ensuring your meta titles and descriptions always reflect your actual content. Especially useful for blogs, news sites, and any site with regularly updated content.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🌐</span><?php esc_html_e( 'Site Variables', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'What They Do:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'These variables pull information from your WordPress site settings, providing consistent branding across all your meta tags.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Available Variables:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <code>{site_name}</code> - <?php esc_html_e( 'Your site title from WordPress settings', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{tagline}</code> - <?php esc_html_e( 'Your site tagline/description', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{separator}</code> - <?php esc_html_e( 'The separator you choose (|, -, etc.)', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{date}</code> - <?php esc_html_e( 'Current date or post publication date', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Best Use Cases:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Essential for building consistent brand recognition. Use {site_name} in most titles and {tagline} for homepage descriptions to reinforce your brand identity.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">⚙️</span><?php esc_html_e( 'How Templates Work', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Default Templates:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'These templates are used when you haven\'t set custom meta tags for specific posts or pages. They provide automatic, professional SEO for all your content without manual work.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Individual Override:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'On any post or page, you can set custom meta titles and descriptions that will override these templates. The templates serve as smart defaults for everything else.', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Live Processing:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Variables are replaced with actual content when pages load', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Search engines see the final, processed meta tags', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Templates update automatically when content changes', 'cspg-basic-seo-helper' ); ?></p>
						</div>
					</div>
					
					<div class="bsh-guide-column">
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">💡</span><?php esc_html_e( 'Template Examples & Best Practices', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Recommended Title Templates:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <code>{post_title} | {site_name}</code> - <?php esc_html_e( 'Classic, works for most sites', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{post_title} - {category} | {site_name}</code> - <?php esc_html_e( 'Good for blogs', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{site_name}: {post_title}</code> - <?php esc_html_e( 'Brand-first approach', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'Recommended Description Templates:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <code>{excerpt}</code> - <?php esc_html_e( 'Simple, uses post content', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{excerpt} | {site_name}</code> - <?php esc_html_e( 'Adds brand reinforcement', 'cspg-basic-seo-helper' ); ?><br>
							• <code>{category}: {excerpt}</code> - <?php esc_html_e( 'Categorized content', 'cspg-basic-seo-helper' ); ?></p>
							
							<p><strong><?php esc_html_e( 'SEO Impact:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Well-crafted templates ensure every page has optimized meta tags, improve search engine rankings, and create consistent branding across your entire site.', 'cspg-basic-seo-helper' ); ?></p>
						</div>
						
						<div class="bsh-guide-item">
							<h4><span class="bsh-guide-icon">🔧</span><?php esc_html_e( 'Complete Variables Reference', 'cspg-basic-seo-helper' ); ?></h4>
							<p><strong><?php esc_html_e( 'Quick Reference:', 'cspg-basic-seo-helper' ); ?></strong><br>
							<?php esc_html_e( 'Click any variable below to learn more about it. All variables can be used in any template field and will be automatically replaced with real content.', 'cspg-basic-seo-helper' ); ?></p>
							
							<div class="bsh-variables-reference">
								<div class="bsh-variable-item">
									<code>{site_name}</code>
									<span><?php esc_html_e( 'Your website name from WordPress Settings → General', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{tagline}</code>
									<span><?php esc_html_e( 'Your site tagline/description from WordPress settings', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{post_title}</code>
									<span><?php esc_html_e( 'The title of the current post, page, or content', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{excerpt}</code>
									<span><?php esc_html_e( 'Post excerpt or auto-generated content summary (first 150 words)', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{category}</code>
									<span><?php esc_html_e( 'Primary category assigned to the post (first category if multiple)', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{author}</code>
									<span><?php esc_html_e( 'Display name of the content author', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{date}</code>
									<span><?php esc_html_e( 'Publication date of the post (formatted as Month Day, Year)', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{post_type}</code>
									<span><?php esc_html_e( 'Type of content: Post, Page, Product, etc.', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{separator}</code>
									<span><?php esc_html_e( 'The separator you choose above (|, -, •, etc.)', 'cspg-basic-seo-helper' ); ?></span>
								</div>
								<div class="bsh-variable-item">
									<code>{custom_field:key}</code>
									<span><?php esc_html_e( 'Any custom field value (replace "key" with your custom field name)', 'cspg-basic-seo-helper' ); ?></span>
								</div>
							</div>
							
							<p><strong><?php esc_html_e( 'Usage Tips:', 'cspg-basic-seo-helper' ); ?></strong><br>
							• <?php esc_html_e( 'Always include {site_name} for brand consistency', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Use {post_title} as the first element in most title templates', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Keep descriptions under 160 characters - {excerpt} usually handles this', 'cspg-basic-seo-helper' ); ?><br>
							• <?php esc_html_e( 'Test your templates with different content types to ensure they work well', 'cspg-basic-seo-helper' ); ?></p>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

