<?php
/**
 * Provide a admin area view for the plugin
 *
 * This file is used to markup the admin-facing aspects of the plugin.
 *
 * @link       https://cdgraham.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 ( 'general' === $active_tab ) :
	global $wp_settings_sections, $wp_settings_fields;
	$general_sections = isset( $wp_settings_sections['card_oracle_option_general'] )
		? $wp_settings_sections['card_oracle_option_general']
		: array();
	?>
	<div class="co-dashboard">
		<header class="co-dashboard__hero co-dashboard__hero--subtle">
			<div class="co-dashboard__hero-text">
				<span class="co-dashboard__badge"><?php esc_html_e( 'Settings', 'card-oracle' ); ?></span>
				<h1 class="co-dashboard__hero-title"><?php esc_html_e( 'General Settings', 'card-oracle' ); ?></h1>
				<p class="co-dashboard__hero-description"><?php esc_html_e( 'Fine-tune global defaults, logging, and reading behaviour before you publish content to your audience.', '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=user-guide' ) ); ?>"><?php esc_html_e( 'View User Guide', 'card-oracle' ); ?></a>
				<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=card-oracle-doc-viewer&doc=help' ) ); ?>"><?php esc_html_e( 'Help Documentation', 'card-oracle' ); ?></a>
				<a class="button button-secondary" href="https://wordpress.com/support/feeds" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'RSS Feeds Guide', '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( 'Configuration', 'card-oracle' ); ?></h2>
				<p><?php esc_html_e( 'Update plugin defaults and helper utilities used across Card Oracle.', '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_general' ); ?>
					<div class="co-settings-grid">
						<?php foreach ( $general_sections as $section ) : ?>
							<?php
							$section_id = $section['id'];
							$fields     = isset( $wp_settings_fields['card_oracle_option_general'][ $section_id ] ) ? $wp_settings_fields['card_oracle_option_general'][ $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_general', $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; ?>
