<?php
/**
 * Provide a admin area view for the plugin
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://chillichalli.com
 * @since      0.7.0
 *
 * @package    Card_Oracle
 * @subpackage Card_Oracle/admin/partials
 */

?>

<!-- This file should primarily consist of HTML with a little bit of PHP. -->
<?php
if ( 'email' === $active_tab ) :
	global $wp_settings_sections, $wp_settings_fields;

	$email_sections = isset( $wp_settings_sections['card_oracle_option_email'] )
		? $wp_settings_sections['card_oracle_option_email']
		: array();

	?>
	<?php $is_premium = card_oracle_fs()->can_use_premium_code__premium_only(); ?>
	<div class="co-dashboard">
		<header class="co-dashboard__hero co-dashboard__hero--subtle">
			<div class="co-dashboard__hero-text">
				<?php if ( $is_premium ) : ?>
					<span class="co-dashboard__badge"><?php esc_html_e( 'Premium', 'card-oracle' ); ?></span>
				<?php endif; ?>
				<h1 class="co-dashboard__hero-title"><?php esc_html_e( 'Email Settings', 'card-oracle' ); ?></h1>
				<p class="co-dashboard__hero-description">
					<?php
					if ( $is_premium ) {
						esc_html_e( 'Control automated delivery, sender branding, and follow-up sequences for every reading.', 'card-oracle' );
					} else {
						esc_html_e( 'Configure email sharing options to let users receive their reading results by email.', 'card-oracle' );
					}
					?>
				</p>
			</div>
			<div class="co-dashboard__hero-actions">
				<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=card-oracle-doc-viewer&doc=help#email-integration' ) ); ?>">
					<?php esc_html_e( 'Email Documentation', 'card-oracle' ); ?>
				</a>
				<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=card-oracle-doc-viewer&doc=troubleshooting#email-integration-issues' ) ); ?>">
					<?php esc_html_e( 'Troubleshooting', 'card-oracle' ); ?>
				</a>
			</div>
		</header>

		<section class="co-dashboard__section co-dashboard__section--panel">
			<div class="co-dashboard__section-heading">
				<h2><?php esc_html_e( 'Delivery configuration', 'card-oracle' ); ?></h2>
				<p><?php esc_html_e( 'Customise templates, timing, and handoff details for automated outreach.', 'card-oracle' ); ?></p>
			</div>
			<div class="co-dashboard__panel-body">
				<form method="post" action="<?php echo esc_url( add_query_arg( 'tab', $active_tab, admin_url( 'options.php' ) ) ); ?>">
					<?php settings_fields( 'card_oracle_option_email' ); ?>
					<div class="co-settings-grid">
						<?php foreach ( $email_sections as $section ) : ?>
							<?php
							$section_id = $section['id'];
							$fields     = isset( $wp_settings_fields['card_oracle_option_email'][ $section_id ] )
								? $wp_settings_fields['card_oracle_option_email'][ $section_id ]
								: array();
							?>
							<div class="co-settings-card">
								<h3><?php echo esc_html( $section['title'] ); ?></h3>
								<?php
								if ( ! empty( $section['callback'] ) ) {
									call_user_func( $section['callback'], $section );
								}

								if ( ! empty( $fields ) ) :
									ob_start();
									do_settings_fields( 'card_oracle_option_email', $section_id );
									$fields_markup = trim( ob_get_clean() );
									if ( $fields_markup ) :
										?>
										<table class="form-table co-settings-table">
											<?php echo $fields_markup; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
										</table>
										<?php
									endif;
								endif;
								?>
							</div>
						<?php endforeach; ?>
					</div>
					<div class="co-settings-actions">
						<?php submit_button( esc_html__( 'Save Changes', 'card-oracle' ), 'primary', 'submit', false ); ?>
					</div>
				</form>
			</div>
		</section>
	</div>
<?php endif; ?>
