<?php
/**
 * Settings page — tabbed, mirrors the Setup Wizard sections (since 2.3.1).
 *
 * Inner tabs: Organization | People | Contact | Pages | Content | Features | Advanced.
 * Every setting the wizard touches is also evergreen-editable here. The "Advanced"
 * tab houses fields the wizard doesn't expose (vatID / leiCode / iso6523Code /
 * geo coordinates / openingHoursSpecification / extra identifiers).
 *
 * All tabs submit to the same form; the save handler in
 * BW_Schema_Admin::save_settings() picks up whichever fields are in $_POST.
 *
 * @package BW_AI_Schema_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$org_data       = BW_Schema_Core::get_organization_schema();
$business_type  = get_option( 'bw_schema_business_type', '' );
$schema_org_typ = get_option( 'bw_schema_schema_org_type', '' );
$founding_date  = get_option( 'bw_schema_founding_date', '' );
if ( is_string( $founding_date ) && preg_match( '/^\d{4}$/', $founding_date ) ) {
	$founding_date .= '-01-01';
}
$employee_count = get_option( 'bw_schema_employee_count', 0 );
$org_descr      = get_option( 'bw_schema_org_description', '' );
$awards_opt     = get_option( 'bw_schema_awards', array() );
$certs_opt      = get_option( 'bw_schema_certifications', array() );
$address        = get_option( 'bw_schema_address', array() );
$contact_points = get_option( 'bw_schema_contact_points', array() );
$page_mappings  = get_option( 'bw_schema_page_mappings', array() );
$post_defaults  = BW_Schema_Core::get_post_type_defaults();
$enable_schema  = get_option( 'bw_schema_enable_schema', 'yes' );
$enable_breadcr = get_option( 'bw_schema_enable_breadcrumbs', 'yes' );
$enable_search  = get_option( 'bw_schema_enable_sitelinks_search', 'yes' );
$enable_cache   = get_option( 'bw_schema_enable_cache', 'yes' );
$output_org_hp  = get_option( 'bw_schema_output_org_homepage', 'no' );
$publish_princ  = get_option( 'bw_schema_publishing_principles', '' );
$fact_policy    = get_option( 'bw_schema_fact_checking_policy', '' );
$disabled_srcs  = get_option( 'bw_schema_disable_sources', array() );
$detected_srcs  = method_exists( 'BW_Schema_Core', 'detect_schema_sources' )
	? BW_Schema_Core::detect_schema_sources() : array();

$enable_price_range = get_option( 'bw_schema_enable_price_range', 'no' );
$price_range        = get_option( 'bw_schema_price_range', '' );

$team_post_type = get_option( 'bw_schema_team_post_type', '' );

settings_errors( 'bw_schema_settings' );

$tabs = array(
	'organization' => array( 'label' => __( 'Organization', 'bw-ai-schema-pro' ), 'icon' => 'dashicons-building' ),
	'people'       => array( 'label' => __( 'People', 'bw-ai-schema-pro' ),       'icon' => 'dashicons-admin-users' ),
	'contact'      => array( 'label' => __( 'Contact', 'bw-ai-schema-pro' ),      'icon' => 'dashicons-email-alt' ),
	'pages'        => array( 'label' => __( 'Pages', 'bw-ai-schema-pro' ),        'icon' => 'dashicons-admin-page' ),
	'content'      => array( 'label' => __( 'Content', 'bw-ai-schema-pro' ),      'icon' => 'dashicons-admin-post' ),
	'features'     => array( 'label' => __( 'Features', 'bw-ai-schema-pro' ),     'icon' => 'dashicons-admin-generic' ),
	'advanced'     => array( 'label' => __( 'Advanced', 'bw-ai-schema-pro' ),     'icon' => 'dashicons-admin-tools' ),
);
?>
<div class="wrap bw-schema-admin-wrap">
	<h1><?php _e( 'BW AI Schema Pro', 'bw-ai-schema-pro' ); ?></h1>

	<!-- Outer page nav (unchanged) -->
	<nav class="nav-tab-wrapper bw-schema-nav-tabs">
		<a href="<?php echo admin_url( 'options-general.php?page=bw-ai-schema' ); ?>" class="nav-tab">
			<span class="dashicons dashicons-dashboard"></span> <?php _e( 'Dashboard', 'bw-ai-schema-pro' ); ?>
		</a>
		<a href="<?php echo admin_url( 'options-general.php?page=bw-ai-schema-settings' ); ?>" class="nav-tab nav-tab-active">
			<span class="dashicons dashicons-admin-generic"></span> <?php _e( 'Settings', 'bw-ai-schema-pro' ); ?>
		</a>
		<a href="<?php echo admin_url( 'options-general.php?page=bw-ai-schema-authors' ); ?>" class="nav-tab">
			<span class="dashicons dashicons-admin-users"></span> <?php _e( 'Author Profiles', 'bw-ai-schema-pro' ); ?>
		</a>
		<a href="<?php echo admin_url( 'options-general.php?page=bw-ai-schema-setup' ); ?>" class="nav-tab">
			<span class="dashicons dashicons-welcome-learn-more"></span> <?php _e( 'Setup Wizard', 'bw-ai-schema-pro' ); ?>
		</a>
	</nav>

	<!-- Inner section nav (new in 2.3.1) -->
	<div class="bw-schema-section-tabs" style="margin: 20px 0 0; border-bottom: 1px solid #c3c4c7;">
		<?php foreach ( $tabs as $key => $tab ) : ?>
			<button type="button"
				class="bw-section-tab"
				data-tab="<?php echo esc_attr( $key ); ?>"
				style="padding: 10px 16px; background: transparent; border: 0; border-bottom: 3px solid transparent; cursor: pointer; font-weight: 500;">
				<span class="dashicons <?php echo esc_attr( $tab['icon'] ); ?>"></span>
				<?php echo esc_html( $tab['label'] ); ?>
			</button>
		<?php endforeach; ?>
	</div>

	<form method="post" action="">
		<?php wp_nonce_field( 'bw_schema_settings', 'bw_schema_settings_nonce' ); ?>
		<input type="hidden" name="bw_schema_active_tab" id="bw_schema_active_tab" value="organization" />

		<!-- ========== ORGANIZATION TAB ========== -->
		<div class="bw-section-panel" data-panel="organization">
			<h2><?php _e( 'Organization profile', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description"><?php _e( 'The basic identity that anchors your @graph. These fields populate the Organization (or EmploymentAgency / Local Business subtype) node referenced from every page.', 'bw-ai-schema-pro' ); ?></p>

			<table class="form-table">
				<tr>
					<th><label for="org_name"><?php _e( 'Name', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<input type="text" id="org_name" name="org_name" value="<?php echo esc_attr( $org_data['name'] ?? '' ); ?>" class="regular-text" />
						<p class="description"><?php _e( 'Your official organization name as it should appear in schema.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="org_alternate_name"><?php _e( 'Alternate / brand name', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<input type="text" id="org_alternate_name" name="org_alternate_name" value="<?php echo esc_attr( $org_data['alternateName'] ?? '' ); ?>" class="regular-text" />
						<p class="description"><?php _e( 'A.k.a., DBA, or brand name (e.g., legal name "Acme Corp" but commonly known as "Acme").', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="org_legal_name"><?php _e( 'Legal name', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<input type="text" id="org_legal_name" name="org_legal_name" value="<?php echo esc_attr( $org_data['legalName'] ?? '' ); ?>" class="regular-text" />
					</td>
				</tr>
				<tr>
					<th><label for="org_slogan"><?php _e( 'Slogan', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_slogan" name="org_slogan" value="<?php echo esc_attr( $org_data['slogan'] ?? '' ); ?>" class="large-text" /></td>
				</tr>
				<tr>
					<th><label for="org_description"><?php _e( 'Description', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<textarea id="org_description" name="org_description" rows="4" class="large-text"><?php echo esc_textarea( $org_descr ); ?></textarea>
						<p class="description"><?php _e( 'Appears as <code>description</code> on the Organization node.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="org_url"><?php _e( 'Website URL', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="url" id="org_url" name="org_url" value="<?php echo esc_attr( $org_data['url'] ?? home_url( '/' ) ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_logo"><?php _e( 'Logo URL', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<input type="url" id="org_logo" name="org_logo" value="<?php echo esc_attr( $org_data['logo'] ?? '' ); ?>" class="regular-text" />
						<p class="description"><?php _e( 'Renders as a rich ImageObject (width / height auto-detected if the file is in your media library).', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="org_telephone"><?php _e( 'Phone', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="tel" id="org_telephone" name="org_telephone" value="<?php echo esc_attr( $org_data['telephone'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_email"><?php _e( 'Email', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="email" id="org_email" name="org_email" value="<?php echo esc_attr( $org_data['email'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="bw_schema_business_type"><?php _e( 'Business type', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="bw_schema_business_type" name="bw_schema_business_type">
							<option value=""><?php _e( '— Select Business Type —', 'bw-ai-schema-pro' ); ?></option>
							<?php
							$bt_choices = array(
								'professional' => __( 'Professional Services (Consulting, Agency, Recruiting, etc.)', 'bw-ai-schema-pro' ),
								'medical'      => __( 'Medical / Healthcare', 'bw-ai-schema-pro' ),
								'hospitality'  => __( 'Hospitality (Hotel, Restaurant)', 'bw-ai-schema-pro' ),
								'education'    => __( 'Educational Institution', 'bw-ai-schema-pro' ),
								'ecommerce'    => __( 'E-commerce / Online Store', 'bw-ai-schema-pro' ),
								'technology'   => __( 'Technology / Software', 'bw-ai-schema-pro' ),
								'realestate'   => __( 'Real Estate', 'bw-ai-schema-pro' ),
								'financial'    => __( 'Financial Services', 'bw-ai-schema-pro' ),
								'media'        => __( 'Media / Publishing', 'bw-ai-schema-pro' ),
								'nonprofit'    => __( 'Non-Profit Organization', 'bw-ai-schema-pro' ),
								'government'   => __( 'Government Agency', 'bw-ai-schema-pro' ),
								'personal'     => __( 'Personal / Individual', 'bw-ai-schema-pro' ),
							);
							foreach ( $bt_choices as $k => $label ) :
								?>
								<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $business_type, $k ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
						<p class="description"><?php _e( 'High-level category. Filters the specific Schema.org type below.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="bw_schema_schema_org_type"><?php _e( 'Schema.org type', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="bw_schema_schema_org_type" name="bw_schema_schema_org_type">
							<option value=""><?php _e( '— Use default (Organization) —', 'bw-ai-schema-pro' ); ?></option>
							<?php
							$known_types = class_exists( 'BW_Schema_Helpers' )
								? BW_Schema_Helpers::get_schema_org_types() : array();
							$flat        = array();
							foreach ( $known_types as $cat => $types ) {
								foreach ( $types as $type_key => $type_label ) {
									$flat[ $type_key ] = $type_label;
								}
							}
							// Preserve a saved value not in flat list.
							if ( $schema_org_typ && ! isset( $flat[ $schema_org_typ ] ) ) {
								echo '<option value="' . esc_attr( $schema_org_typ ) . '" selected>' . esc_html( $schema_org_typ ) . ' ' . esc_html__( '(current)', 'bw-ai-schema-pro' ) . '</option>';
							}
							foreach ( $flat as $type_key => $type_label ) :
								?>
								<option value="<?php echo esc_attr( $type_key ); ?>" <?php selected( $schema_org_typ, $type_key ); ?>><?php echo esc_html( $type_label ); ?></option>
							<?php endforeach; ?>
						</select>
						<p class="description"><?php _e( 'Specific schema.org @type. More specific is better for rich results.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="bw_schema_founding_date"><?php _e( 'Founding date', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="date" id="bw_schema_founding_date" name="bw_schema_founding_date" value="<?php echo esc_attr( $founding_date ); ?>" /></td>
				</tr>
				<tr>
					<th><label for="bw_schema_employee_count"><?php _e( 'Number of employees', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="number" min="0" id="bw_schema_employee_count" name="bw_schema_employee_count" value="<?php echo (int) $employee_count; ?>" /></td>
				</tr>
				<tr>
					<th><label for="org_areas_served"><?php _e( 'Areas served', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<?php $areas = is_array( $org_data['areaServed'] ?? null ) ? implode( ', ', $org_data['areaServed'] ) : ( $org_data['areaServed'] ?? '' ); ?>
						<textarea id="org_areas_served" name="org_areas_served" rows="2" class="large-text"><?php echo esc_textarea( $areas ); ?></textarea>
						<p class="description"><?php _e( 'Comma-separated. Emitted as <code>areaServed</code> on the Organization.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="org_services"><?php _e( 'Services / products', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<?php $svcs = is_array( $org_data['makesOffer'] ?? null ) ? implode( ', ', $org_data['makesOffer'] ) : ( $org_data['makesOffer'] ?? '' ); ?>
						<textarea id="org_services" name="org_services" rows="3" class="large-text"><?php echo esc_textarea( $svcs ); ?></textarea>
						<p class="description"><?php _e( 'Comma-separated. Emitted as <code>makesOffer</code> with each entry wrapped in <code>Offer → itemOffered → Service</code>.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="org_awards"><?php _e( 'Awards', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<?php $awards_str = is_array( $awards_opt ) ? implode( "\n", $awards_opt ) : ( $awards_opt ); ?>
						<textarea id="org_awards" name="org_awards" rows="3" class="large-text" placeholder="<?php esc_attr_e( 'One per line', 'bw-ai-schema-pro' ); ?>"><?php echo esc_textarea( $awards_str ); ?></textarea>
					</td>
				</tr>
				<tr>
					<th><label for="org_certifications"><?php _e( 'Certifications / credentials', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<?php $certs_str = is_array( $certs_opt ) ? implode( "\n", $certs_opt ) : ( $certs_opt ); ?>
						<textarea id="org_certifications" name="org_certifications" rows="3" class="large-text" placeholder="<?php esc_attr_e( 'One per line', 'bw-ai-schema-pro' ); ?>"><?php echo esc_textarea( $certs_str ); ?></textarea>
						<p class="description"><?php _e( 'Emitted as <code>hasCredential</code> on the Organization.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="org_sameas"><?php _e( 'Social profiles (sameAs)', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<?php $sameas_str = is_array( $org_data['sameAs'] ?? null ) ? implode( "\n", $org_data['sameAs'] ) : ''; ?>
						<textarea id="org_sameas" name="org_sameas" rows="4" class="large-text" placeholder="<?php esc_attr_e( 'One URL per line', 'bw-ai-schema-pro' ); ?>"><?php echo esc_textarea( $sameas_str ); ?></textarea>
					</td>
				</tr>
				<tr>
					<th><?php _e( 'Price range', 'bw-ai-schema-pro' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="enable_price_range" value="yes" <?php checked( $enable_price_range, 'yes' ); ?> />
							<?php _e( 'Display in schema', 'bw-ai-schema-pro' ); ?>
						</label>
						<select name="org_price_range" style="margin-left: 12px;">
							<?php foreach ( array( '$', '$$', '$$$', '$$$$' ) as $pr ) : ?>
								<option value="<?php echo esc_attr( $pr ); ?>" <?php selected( $price_range, $pr ); ?>><?php echo esc_html( $pr ); ?></option>
							<?php endforeach; ?>
						</select>
						<p class="description"><?php _e( 'Only meaningful for Local Business / EmploymentAgency subtypes.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
			</table>
		</div>

		<!-- ========== PEOPLE TAB ========== -->
		<div class="bw-section-panel" data-panel="people" style="display: none;">
			<h2><?php _e( 'People', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description"><?php _e( 'Map a post type to "team members" and configure which entries are authors / founders / linked WordPress users. The mapped team CPT is the source of truth for Person, founder[], employee[], and ProfilePage schema.', 'bw-ai-schema-pro' ); ?></p>

			<table class="form-table">
				<tr>
					<th><label for="team_post_type"><?php _e( 'Team member post type', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="team_post_type" name="team_post_type" style="min-width: 300px;">
							<option value=""><?php _e( '— Select Post Type —', 'bw-ai-schema-pro' ); ?></option>
							<?php
							foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $pt ) {
								if ( in_array( $pt->name, array( 'attachment', 'revision', 'nav_menu_item', 'post', 'page' ), true ) ) {
									continue;
								}
								?>
								<option value="<?php echo esc_attr( $pt->name ); ?>" <?php selected( $team_post_type, $pt->name ); ?>><?php echo esc_html( $pt->labels->name ); ?></option>
								<?php
							}
							?>
						</select>
					</td>
				</tr>
			</table>

			<?php
			if ( $team_post_type ) :
				$team_members = get_posts( array(
					'post_type'      => $team_post_type,
					'posts_per_page' => -1,
					'post_status'    => 'publish',
					'orderby'        => 'title',
					'order'          => 'ASC',
				) );
				$wp_users     = get_users( array(
					'role__in' => array( 'administrator', 'editor', 'author', 'contributor' ),
					'orderby'  => 'display_name',
				) );
				if ( $team_members ) :
					?>
					<h3 style="margin-top: 30px;"><?php _e( 'Team members', 'bw-ai-schema-pro' ); ?></h3>
					<p class="description"><?php _e( 'Per-row flags: is this member shown as an author byline? Is this member shown as a founder on the Organization node? Linked WordPress user lets the /author/USER/ archive redirect to the team page.', 'bw-ai-schema-pro' ); ?></p>
					<table class="widefat striped" style="margin-top: 10px;">
						<thead>
							<tr>
								<th style="width: 30%;"><?php _e( 'Team Member', 'bw-ai-schema-pro' ); ?></th>
								<th style="width: 12%; text-align: center;"><?php _e( 'Is Author', 'bw-ai-schema-pro' ); ?></th>
								<th style="width: 12%; text-align: center;"><?php _e( 'Is Founder', 'bw-ai-schema-pro' ); ?></th>
								<th style="width: 12%; text-align: center;"><?php _e( 'Exclude from schema', 'bw-ai-schema-pro' ); ?></th>
								<th><?php _e( 'Linked WordPress user', 'bw-ai-schema-pro' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $team_members as $tm ) :
								$is_author = get_post_meta( $tm->ID, '_bw_schema_is_author', true ) === '1';
								$is_leader = get_post_meta( $tm->ID, '_bw_schema_is_leader', true ) === '1';
								$is_exclude = get_post_meta( $tm->ID, '_bw_schema_exclude_from_schema', true ) === '1';
								$linked    = get_post_meta( $tm->ID, '_bw_schema_linked_user', true );
								?>
								<tr>
									<td><strong><?php echo esc_html( $tm->post_title ); ?></strong></td>
									<td style="text-align: center;"><input type="checkbox" name="team_member_is_author[<?php echo (int) $tm->ID; ?>]" value="1" <?php checked( $is_author ); ?> /></td>
									<td style="text-align: center;"><input type="checkbox" name="team_member_is_leader[<?php echo (int) $tm->ID; ?>]" value="1" <?php checked( $is_leader ); ?> /></td>
									<td style="text-align: center;"><input type="checkbox" name="team_member_exclude[<?php echo (int) $tm->ID; ?>]" value="1" <?php checked( $is_exclude ); ?> title="<?php esc_attr_e( 'For placeholder rows like "Join our team"', 'bw-ai-schema-pro' ); ?>" /></td>
									<td>
										<select name="team_member_linked_user[<?php echo (int) $tm->ID; ?>]" style="width: 100%;">
											<option value=""><?php _e( '— None —', 'bw-ai-schema-pro' ); ?></option>
											<?php foreach ( $wp_users as $u ) : ?>
												<option value="<?php echo (int) $u->ID; ?>" <?php selected( $linked, $u->ID ); ?>><?php echo esc_html( $u->display_name ); ?> (<?php echo esc_html( $u->user_email ); ?>)</option>
											<?php endforeach; ?>
										</select>
									</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php endif;
			endif; ?>
		</div>

		<!-- ========== CONTACT TAB ========== -->
		<div class="bw-section-panel" data-panel="contact" style="display: none;">
			<h2><?php _e( 'Address & contact points', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description"><?php _e( 'Primary postal address (emitted as PostalAddress on the Organization) and contactPoint[] entries for customer service / sales / etc.', 'bw-ai-schema-pro' ); ?></p>

			<table class="form-table">
				<tr>
					<th><?php _e( 'Street address', 'bw-ai-schema-pro' ); ?></th>
					<td><input type="text" name="address[street]" value="<?php echo esc_attr( $address['street'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><?php _e( 'City', 'bw-ai-schema-pro' ); ?></th>
					<td><input type="text" name="address[city]" value="<?php echo esc_attr( $address['city'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><?php _e( 'State / region', 'bw-ai-schema-pro' ); ?></th>
					<td><input type="text" name="address[state]" value="<?php echo esc_attr( $address['state'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><?php _e( 'Postal / ZIP code', 'bw-ai-schema-pro' ); ?></th>
					<td><input type="text" name="address[postal]" value="<?php echo esc_attr( $address['postal'] ?? '' ); ?>" /></td>
				</tr>
				<tr>
					<th><?php _e( 'Country', 'bw-ai-schema-pro' ); ?></th>
					<td><input type="text" name="address[country]" value="<?php echo esc_attr( $address['country'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'e.g. CA, US, GB', 'bw-ai-schema-pro' ); ?>" /></td>
				</tr>
			</table>

			<h3 style="margin-top: 30px;"><?php _e( 'Contact points', 'bw-ai-schema-pro' ); ?></h3>
			<table class="widefat striped" style="max-width: 900px;">
				<thead>
					<tr>
						<th style="width: 25%;"><?php _e( 'Type', 'bw-ai-schema-pro' ); ?></th>
						<th><?php _e( 'Telephone', 'bw-ai-schema-pro' ); ?></th>
						<th><?php _e( 'Email', 'bw-ai-schema-pro' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php
					$cps = is_array( $contact_points ) ? array_values( $contact_points ) : array();
					// Always render at least 3 rows so the admin can add new entries without JS.
					while ( count( $cps ) < 3 ) {
						$cps[] = array( 'type' => '', 'telephone' => '', 'email' => '' );
					}
					foreach ( $cps as $i => $cp ) :
						?>
						<tr>
							<td>
								<select name="contact_points[<?php echo (int) $i; ?>][type]">
									<option value=""><?php _e( '— Select type —', 'bw-ai-schema-pro' ); ?></option>
									<?php
									$cp_types = array( 'customer service', 'sales', 'technical support', 'billing support', 'reservations', 'emergency' );
									foreach ( $cp_types as $type ) :
										?>
										<option value="<?php echo esc_attr( $type ); ?>" <?php selected( $cp['type'] ?? '', $type ); ?>><?php echo esc_html( ucwords( $type ) ); ?></option>
									<?php endforeach; ?>
								</select>
							</td>
							<td><input type="tel" name="contact_points[<?php echo (int) $i; ?>][telephone]" value="<?php echo esc_attr( $cp['telephone'] ?? '' ); ?>" class="regular-text" /></td>
							<td><input type="email" name="contact_points[<?php echo (int) $i; ?>][email]" value="<?php echo esc_attr( $cp['email'] ?? '' ); ?>" class="regular-text" /></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>

		<!-- ========== PAGES TAB ========== -->
		<div class="bw-section-panel" data-panel="pages" style="display: none;">
			<h2><?php _e( 'Page mappings', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description"><?php _e( 'Map specific pages on your site to schema page-types so they get the right rich result treatment.', 'bw-ai-schema-pro' ); ?></p>

			<?php
			$page_type_choices = array(
				'about'    => array( 'label' => __( 'About Page', 'bw-ai-schema-pro' ), 'schema' => 'AboutPage' ),
				'services' => array( 'label' => __( 'Services / Products', 'bw-ai-schema-pro' ), 'schema' => 'CollectionPage' ),
				'contact'  => array( 'label' => __( 'Contact Page', 'bw-ai-schema-pro' ), 'schema' => 'ContactPage' ),
				'blog'     => array( 'label' => __( 'Blog Index', 'bw-ai-schema-pro' ), 'schema' => 'Blog' ),
				'team'     => array( 'label' => __( 'Team / Staff Page', 'bw-ai-schema-pro' ), 'schema' => 'CollectionPage' ),
				'terms'    => array( 'label' => __( 'Terms of Service', 'bw-ai-schema-pro' ), 'schema' => 'WebPage' ),
				'privacy'  => array( 'label' => __( 'Privacy Policy', 'bw-ai-schema-pro' ), 'schema' => 'WebPage' ),
			);
			$all_pages = get_posts( array( 'post_type' => 'page', 'numberposts' => -1, 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC' ) );
			?>
			<table class="form-table">
				<?php foreach ( $page_type_choices as $key => $cfg ) :
					$current = isset( $page_mappings[ $key ] ) ? $page_mappings[ $key ] : '';
					?>
					<tr>
						<th>
							<label for="page_map_<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $cfg['label'] ); ?></label>
							<small style="display: block; color: #6c7781;"><code><?php echo esc_html( $cfg['schema'] ); ?></code></small>
						</th>
						<td>
							<select id="page_map_<?php echo esc_attr( $key ); ?>" name="page_mappings[<?php echo esc_attr( $key ); ?>]" style="min-width: 320px;">
								<option value=""><?php _e( '— Not mapped —', 'bw-ai-schema-pro' ); ?></option>
								<?php foreach ( $all_pages as $p ) : ?>
									<option value="<?php echo (int) $p->ID; ?>" <?php selected( (int) $current, $p->ID ); ?>><?php echo esc_html( $p->post_title ); ?></option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
				<?php endforeach; ?>
			</table>
		</div>

		<!-- ========== CONTENT TAB ========== -->
		<div class="bw-section-panel" data-panel="content" style="display: none;">
			<h2><?php _e( 'Post-type defaults', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description"><?php _e( 'Default schema type per post type. Pick "Don\'t emit" for post types another plugin already handles (e.g. WP Job Manager for job_listing) or that the plugin handles via a dedicated path (your team CPT → ProfilePage + Person).', 'bw-ai-schema-pro' ); ?></p>

			<?php $schema_types = BW_Schema_Core::get_schema_types(); ?>
			<table class="form-table">
				<?php
				$content_post_types = get_post_types( array( 'public' => true ), 'objects' );
				unset( $content_post_types['attachment'] );
				foreach ( $content_post_types as $pt ) :
					$current = $post_defaults[ $pt->name ] ?? '';
					// Same handled-elsewhere detection as the wizard.
					$handled = null;
					if ( $pt->name === $team_post_type ) {
						$handled = __( 'AI Schema Pro emits this directly as ProfilePage + Person on each team-member page (no Article needed on top)', 'bw-ai-schema-pro' );
					} elseif ( $pt->name === 'job_listing' && ( defined( 'JOB_MANAGER_VERSION' ) || class_exists( 'WP_Job_Manager' ) ) ) {
						$handled = __( 'WP Job Manager emits JobPosting on these', 'bw-ai-schema-pro' );
					} elseif ( $pt->name === 'product' && class_exists( 'WooCommerce' ) ) {
						$handled = __( 'WooCommerce emits Product schema on these', 'bw-ai-schema-pro' );
					}
					?>
					<tr>
						<th><label for="pt_default_<?php echo esc_attr( $pt->name ); ?>"><?php echo esc_html( $pt->labels->name ); ?></label></th>
						<td>
							<select id="pt_default_<?php echo esc_attr( $pt->name ); ?>" name="post_type_defaults[<?php echo esc_attr( $pt->name ); ?>]" style="min-width: 320px;">
								<option value=""><?php _e( '— Select default —', 'bw-ai-schema-pro' ); ?></option>
								<option value="none" <?php selected( $current, 'none' ); ?>><?php _e( '— Don\'t emit — handled elsewhere —', 'bw-ai-schema-pro' ); ?></option>
								<?php foreach ( $schema_types as $key => $type ) : ?>
									<optgroup label="<?php echo esc_attr( $type['label'] ); ?>">
										<?php foreach ( $type['subtypes'] as $sk => $sl ) : ?>
											<option value="<?php echo esc_attr( $key . ':' . $sk ); ?>" <?php selected( $current, $key . ':' . $sk ); ?>><?php echo esc_html( $sl ); ?></option>
										<?php endforeach; ?>
									</optgroup>
								<?php endforeach; ?>
							</select>
							<?php if ( $handled ) : ?>
								<p class="description" style="color: #1e7e34;"><span class="dashicons dashicons-yes-alt"></span> <?php echo esc_html( $handled ); ?>. <?php _e( '"Don\'t emit" is the right pick here.', 'bw-ai-schema-pro' ); ?></p>
							<?php endif; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			</table>
		</div>

		<!-- ========== FEATURES TAB ========== -->
		<div class="bw-section-panel" data-panel="features" style="display: none;">
			<h2><?php _e( 'Features', 'bw-ai-schema-pro' ); ?></h2>

			<table class="form-table">
				<tr>
					<th><?php _e( 'Enable schema', 'bw-ai-schema-pro' ); ?></th>
					<td><label><input type="checkbox" name="enable_schema" value="yes" <?php checked( $enable_schema, 'yes' ); ?> /> <?php _e( 'Master switch for all schema output.', 'bw-ai-schema-pro' ); ?></label></td>
				</tr>
				<tr>
					<th><?php _e( 'Breadcrumbs', 'bw-ai-schema-pro' ); ?></th>
					<td><label><input type="checkbox" name="enable_breadcrumbs" value="yes" <?php checked( $enable_breadcr, 'yes' ); ?> /> <?php _e( 'Emit BreadcrumbList on appropriate pages.', 'bw-ai-schema-pro' ); ?></label></td>
				</tr>
				<tr>
					<th><?php _e( 'Sitelinks search box', 'bw-ai-schema-pro' ); ?></th>
					<td><label><input type="checkbox" name="enable_sitelinks_search" value="yes" <?php checked( $enable_search, 'yes' ); ?> /> <?php _e( 'Add SearchAction so Google can render the sitelinks search box.', 'bw-ai-schema-pro' ); ?></label></td>
				</tr>
				<tr>
					<th><?php _e( 'Schema cache', 'bw-ai-schema-pro' ); ?></th>
					<td><label><input type="checkbox" name="enable_cache" value="yes" <?php checked( $enable_cache, 'yes' ); ?> /> <?php _e( 'Cache generated schema per post for performance.', 'bw-ai-schema-pro' ); ?></label></td>
				</tr>
				<tr>
					<th><?php _e( 'Output Org on homepage', 'bw-ai-schema-pro' ); ?></th>
					<td><label><input type="checkbox" name="output_org_homepage" value="yes" <?php checked( $output_org_hp, 'yes' ); ?> /> <?php _e( 'Recommended for business sites — emit Organization (or subtype) as the top-level node on the front page.', 'bw-ai-schema-pro' ); ?></label></td>
				</tr>
				<tr>
					<th><label for="publishing_principles"><?php _e( 'Publishing principles URL', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="url" id="publishing_principles" name="publishing_principles" value="<?php echo esc_url( $publish_princ ); ?>" class="large-text" /></td>
				</tr>
				<tr>
					<th><label for="fact_checking_policy"><?php _e( 'Fact-checking policy URL', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="url" id="fact_checking_policy" name="fact_checking_policy" value="<?php echo esc_url( $fact_policy ); ?>" class="large-text" /></td>
				</tr>
			</table>

			<h3 style="margin-top: 30px;"><?php _e( 'Take over from other schema plugins', 'bw-ai-schema-pro' ); ?></h3>
			<p class="description" style="max-width: 720px;">
				<?php _e( 'AI Schema Pro emits a richer Organization schema than most general-purpose SEO plugins. Because Schema.org uses a shared <code>#organization</code> @id, both plugins describing the same organization can produce inconsistent output. Checking a plugin here suppresses its schema output so AI Schema Pro is the only source.', 'bw-ai-schema-pro' ); ?>
			</p>
			<?php if ( ! empty( $detected_srcs ) ) : ?>
				<?php foreach ( $detected_srcs as $key => $src ) : ?>
					<p>
						<label>
							<input type="checkbox" name="disable_sources[]" value="<?php echo esc_attr( $key ); ?>" <?php checked( in_array( $key, $disabled_srcs, true ) ); ?> />
							<strong><?php printf( esc_html__( 'Take over from %s', 'bw-ai-schema-pro' ), esc_html( $src['name'] ) ); ?></strong>
						</label>
					</p>
				<?php endforeach; ?>
			<?php else : ?>
				<p style="color: #1e7e34;"><span class="dashicons dashicons-yes-alt"></span> <?php _e( 'No other schema sources detected.', 'bw-ai-schema-pro' ); ?></p>
			<?php endif; ?>

			<!-- Post FAQ section (already wired) -->
			<h3 style="margin-top: 30px;"><?php _e( 'Post FAQ', 'bw-ai-schema-pro' ); ?></h3>
			<?php
			$faq_settings   = class_exists( 'BW_Schema_Post_FAQ' ) ? BW_Schema_Post_FAQ::get_settings() : null;
			$faq_modes      = class_exists( 'BW_Schema_Post_FAQ' ) ? BW_Schema_Post_FAQ::insertion_modes() : array();
			$faq_post_types = get_post_types( array( 'public' => true ), 'objects' );
			unset( $faq_post_types['attachment'] );
			if ( $faq_settings ) :
				?>
				<table class="form-table">
					<tr>
						<th><?php _e( 'Enable Post FAQ', 'bw-ai-schema-pro' ); ?></th>
						<td><label><input type="checkbox" name="bw_schema_post_faq_enabled" value="1" <?php checked( $faq_settings['enabled'] ); ?> /> <?php _e( 'Adds the FAQ metabox to chosen post types and renders the FAQ on the front-end.', 'bw-ai-schema-pro' ); ?></label></td>
					</tr>
					<tr>
						<th><?php _e( 'Post types', 'bw-ai-schema-pro' ); ?></th>
						<td>
							<?php foreach ( $faq_post_types as $pt ) : ?>
								<label style="display: inline-block; margin-right: 14px;">
									<input type="checkbox" name="bw_schema_post_faq_post_types[]" value="<?php echo esc_attr( $pt->name ); ?>" <?php checked( in_array( $pt->name, (array) $faq_settings['post_types'], true ) ); ?> />
									<?php echo esc_html( $pt->labels->name ); ?>
								</label>
							<?php endforeach; ?>
						</td>
					</tr>
					<tr>
						<th><label for="bw_schema_post_faq_default_heading"><?php _e( 'Default heading', 'bw-ai-schema-pro' ); ?></label></th>
						<td><input type="text" id="bw_schema_post_faq_default_heading" name="bw_schema_post_faq_default_heading" value="<?php echo esc_attr( $faq_settings['default_heading'] ); ?>" class="regular-text" /></td>
					</tr>
					<tr>
						<th><?php _e( 'Default accordion', 'bw-ai-schema-pro' ); ?></th>
						<td><label><input type="checkbox" name="bw_schema_post_faq_default_accordion" value="1" <?php checked( $faq_settings['default_accordion'] ); ?> /> <?php _e( 'Show answers as an accordion by default.', 'bw-ai-schema-pro' ); ?></label></td>
					</tr>
					<tr>
						<th><label for="bw_schema_post_faq_insertion_mode"><?php _e( 'Insertion point', 'bw-ai-schema-pro' ); ?></label></th>
						<td>
							<select id="bw_schema_post_faq_insertion_mode" name="bw_schema_post_faq_insertion_mode">
								<?php foreach ( $faq_modes as $k => $l ) : ?>
									<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $k, $faq_settings['insertion_mode'] ); ?>><?php echo esc_html( $l ); ?></option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
					<tr>
						<th><label for="bw_schema_post_faq_selector"><?php _e( 'CSS selector', 'bw-ai-schema-pro' ); ?></label></th>
						<td><input type="text" id="bw_schema_post_faq_selector" name="bw_schema_post_faq_selector" value="<?php echo esc_attr( $faq_settings['selector'] ); ?>" class="regular-text" placeholder=".entry-content, #post-author" /></td>
					</tr>
				</table>
			<?php endif; ?>

			<!-- Author Box section -->
			<h3 style="margin-top: 30px;"><?php _e( 'Author box', 'bw-ai-schema-pro' ); ?></h3>
			<?php
			$author_box_enabled    = get_option( 'bw_schema_author_box_enabled', 'yes' );
			$author_box_post_types = get_option( 'bw_schema_author_box_post_types', array( 'post' ) );
			$author_box_position   = get_option( 'bw_schema_author_box_position', 'after_content' );
			$redirect_author_arch  = get_option( 'bw_schema_redirect_author_archives', 'yes' );
			$ab_post_types         = get_post_types( array( 'public' => true ), 'objects' );
			unset( $ab_post_types['attachment'] );
			?>
			<table class="form-table">
				<tr>
					<th><?php _e( 'Enable author box', 'bw-ai-schema-pro' ); ?></th>
					<td><label><input type="checkbox" name="author_box_enabled" value="yes" <?php checked( $author_box_enabled, 'yes' ); ?> /></label></td>
				</tr>
				<tr>
					<th><?php _e( 'Display on post types', 'bw-ai-schema-pro' ); ?></th>
					<td>
						<?php foreach ( $ab_post_types as $pt ) : ?>
							<label style="display: inline-block; margin-right: 14px;"><input type="checkbox" name="author_box_post_types[]" value="<?php echo esc_attr( $pt->name ); ?>" <?php checked( in_array( $pt->name, (array) $author_box_post_types, true ) ); ?> /> <?php echo esc_html( $pt->labels->name ); ?></label>
						<?php endforeach; ?>
					</td>
				</tr>
				<tr>
					<th><label for="author_box_position"><?php _e( 'Position', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="author_box_position" name="author_box_position">
							<option value="after_content" <?php selected( $author_box_position, 'after_content' ); ?>><?php _e( 'After content', 'bw-ai-schema-pro' ); ?></option>
							<option value="before_content" <?php selected( $author_box_position, 'before_content' ); ?>><?php _e( 'Before content', 'bw-ai-schema-pro' ); ?></option>
							<option value="manual" <?php selected( $author_box_position, 'manual' ); ?>><?php _e( 'Manual (shortcode)', 'bw-ai-schema-pro' ); ?></option>
						</select>
					</td>
				</tr>
				<tr>
					<th><?php _e( 'Redirect author archives', 'bw-ai-schema-pro' ); ?></th>
					<td><label><input type="checkbox" name="redirect_author_archives" value="yes" <?php checked( $redirect_author_arch, 'yes' ); ?> /> <?php _e( 'Visiting /author/USER/ 301s to that user\'s linked team page.', 'bw-ai-schema-pro' ); ?></label></td>
				</tr>
			</table>
		</div>

		<!-- ========== ADVANCED TAB ========== -->
		<div class="bw-section-panel" data-panel="advanced" style="display: none;">
			<h2><?php _e( 'Advanced identifiers + location', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description"><?php _e( 'Schema.org fields the wizard doesn\'t expose. Most sites don\'t need these — fill what applies.', 'bw-ai-schema-pro' ); ?></p>

			<table class="form-table">
				<tr>
					<th><label for="org_tax_id"><?php _e( 'Tax ID', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_tax_id" name="org_tax_id" value="<?php echo esc_attr( $org_data['taxID'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_vat_id"><?php _e( 'VAT ID', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_vat_id" name="org_vat_id" value="<?php echo esc_attr( $org_data['vatID'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_duns"><?php _e( 'DUNS number', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_duns" name="org_duns" value="<?php echo esc_attr( $org_data['duns'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_naics"><?php _e( 'NAICS code', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_naics" name="org_naics" value="<?php echo esc_attr( $org_data['naics'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_lei"><?php _e( 'LEI code', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_lei" name="org_lei" value="<?php echo esc_attr( $org_data['leiCode'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_iso6523"><?php _e( 'ISO 6523 code', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_iso6523" name="org_iso6523" value="<?php echo esc_attr( $org_data['iso6523Code'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
			</table>

			<h3 style="margin-top: 30px;"><?php _e( 'Geo coordinates', 'bw-ai-schema-pro' ); ?></h3>
			<p class="description"><?php _e( 'For Local Business / EmploymentAgency subtypes. Emit as GeoCoordinates on the Organization.', 'bw-ai-schema-pro' ); ?></p>
			<table class="form-table">
				<tr>
					<th><label for="org_geo_lat"><?php _e( 'Latitude', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_geo_lat" name="org_geo[latitude]" value="<?php echo esc_attr( $org_data['geo']['latitude'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
				<tr>
					<th><label for="org_geo_lng"><?php _e( 'Longitude', 'bw-ai-schema-pro' ); ?></label></th>
					<td><input type="text" id="org_geo_lng" name="org_geo[longitude]" value="<?php echo esc_attr( $org_data['geo']['longitude'] ?? '' ); ?>" class="regular-text" /></td>
				</tr>
			</table>

			<h3 style="margin-top: 30px;"><?php _e( 'Opening hours', 'bw-ai-schema-pro' ); ?></h3>
			<p class="description"><?php _e( 'For sites that want to publish hours. Emit as openingHoursSpecification[] on the Organization. One row per range.', 'bw-ai-schema-pro' ); ?></p>
			<?php
			$hours_rows = isset( $org_data['openingHours'] ) && is_array( $org_data['openingHours'] ) ? array_values( $org_data['openingHours'] ) : array();
			while ( count( $hours_rows ) < 2 ) {
				$hours_rows[] = array( 'days' => array(), 'opens' => '', 'closes' => '' );
			}
			$dow = array( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' );
			?>
			<table class="widefat striped" style="max-width: 900px;">
				<thead>
					<tr>
						<th><?php _e( 'Days', 'bw-ai-schema-pro' ); ?></th>
						<th style="width: 130px;"><?php _e( 'Opens', 'bw-ai-schema-pro' ); ?></th>
						<th style="width: 130px;"><?php _e( 'Closes', 'bw-ai-schema-pro' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $hours_rows as $i => $row ) :
						$row_days = isset( $row['days'] ) && is_array( $row['days'] ) ? $row['days'] : array();
						?>
						<tr>
							<td>
								<?php foreach ( $dow as $day ) : ?>
									<label style="display: inline-block; margin-right: 8px;">
										<input type="checkbox" name="org_opening_hours[<?php echo (int) $i; ?>][days][]" value="<?php echo esc_attr( $day ); ?>" <?php checked( in_array( $day, $row_days, true ) ); ?> />
										<?php echo esc_html( substr( $day, 0, 3 ) ); ?>
									</label>
								<?php endforeach; ?>
							</td>
							<td><input type="time" name="org_opening_hours[<?php echo (int) $i; ?>][opens]" value="<?php echo esc_attr( $row['opens'] ?? '' ); ?>" /></td>
							<td><input type="time" name="org_opening_hours[<?php echo (int) $i; ?>][closes]" value="<?php echo esc_attr( $row['closes'] ?? '' ); ?>" /></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>

			<h3 style="margin-top: 30px;"><?php _e( 'Founding location', 'bw-ai-schema-pro' ); ?></h3>
			<table class="form-table">
				<tr>
					<th><label for="org_founding_location"><?php _e( 'Founding location', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<input type="text" id="org_founding_location" name="org_founding_location" value="<?php echo esc_attr( $org_data['foundingLocation'] ?? '' ); ?>" class="regular-text" placeholder="<?php esc_attr_e( 'e.g. Victoria, BC, Canada', 'bw-ai-schema-pro' ); ?>" />
						<p class="description"><?php _e( 'Emit as <code>foundingLocation → Place</code> on the Organization.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
			</table>

			<h3 style="margin-top: 30px;"><?php _e( 'Meta box display', 'bw-ai-schema-pro' ); ?></h3>
			<?php
			$metabox_position = get_option( 'bw_schema_metabox_position', 'normal' );
			$metabox_default_state = get_option( 'bw_schema_metabox_default_state', 'collapsed' );
			$metabox_post_types = get_option( 'bw_schema_metabox_post_types', array() );
			if ( empty( $metabox_post_types ) ) {
				$all_post_types = get_post_types( array( 'public' => true ), 'names' );
				$metabox_post_types = array_diff( $all_post_types, array( 'attachment' ) );
			}
			$mb_post_types = get_post_types( array( 'public' => true ), 'objects' );
			$mb_excluded   = array( 'attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_block', 'wp_template', 'wp_template_part', 'wp_navigation' );
			?>
			<table class="form-table">
				<tr>
					<th><label for="metabox_position"><?php _e( 'Position', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="metabox_position" name="metabox_position">
							<option value="normal" <?php selected( $metabox_position, 'normal' ); ?>><?php _e( 'Below editor', 'bw-ai-schema-pro' ); ?></option>
							<option value="side" <?php selected( $metabox_position, 'side' ); ?>><?php _e( 'Sidebar', 'bw-ai-schema-pro' ); ?></option>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="metabox_default_state"><?php _e( 'Default state', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="metabox_default_state" name="metabox_default_state">
							<option value="collapsed" <?php selected( $metabox_default_state, 'collapsed' ); ?>><?php _e( 'Collapsed', 'bw-ai-schema-pro' ); ?></option>
							<option value="expanded" <?php selected( $metabox_default_state, 'expanded' ); ?>><?php _e( 'Expanded', 'bw-ai-schema-pro' ); ?></option>
						</select>
					</td>
				</tr>
				<tr>
					<th><?php _e( 'Show meta box on', 'bw-ai-schema-pro' ); ?></th>
					<td>
						<?php foreach ( $mb_post_types as $pt ) {
							if ( in_array( $pt->name, $mb_excluded, true ) ) {
								continue;
							}
							?>
							<label style="display: inline-block; margin-right: 14px;">
								<input type="checkbox" name="metabox_post_types[]" value="<?php echo esc_attr( $pt->name ); ?>" <?php checked( in_array( $pt->name, (array) $metabox_post_types, true ) ); ?> />
								<?php echo esc_html( $pt->labels->name ); ?>
							</label>
							<?php
						} ?>
					</td>
				</tr>
			</table>
		</div>

		<p class="submit">
			<input type="submit" name="bw_schema_save_settings" class="button-primary" value="<?php _e( 'Save Settings', 'bw-ai-schema-pro' ); ?>" />
		</p>
	</form>
</div>

<script>
(function () {
	// Tab switcher (since 2.3.1). Active tab is also written to the hidden input
	// so the save handler can land you back on the same tab after submit.
	var buttons  = document.querySelectorAll('.bw-section-tab');
	var panels   = document.querySelectorAll('.bw-section-panel');
	var hiddenInput = document.getElementById('bw_schema_active_tab');

	function activate(tab) {
		buttons.forEach(function (b) {
			var on = b.getAttribute('data-tab') === tab;
			b.style.borderBottomColor = on ? '#2271b1' : 'transparent';
			b.style.color = on ? '#2271b1' : '';
			b.style.fontWeight = on ? '600' : '500';
		});
		panels.forEach(function (p) {
			p.style.display = p.getAttribute('data-panel') === tab ? '' : 'none';
		});
		if (hiddenInput) {
			hiddenInput.value = tab;
		}
		if (window.history && typeof window.history.replaceState === 'function') {
			window.history.replaceState(null, '', '#tab=' + tab);
		}
	}

	buttons.forEach(function (b) {
		b.addEventListener('click', function () {
			activate(b.getAttribute('data-tab'));
		});
	});

	// Open the tab from URL hash, or default to organization.
	var hash = (window.location.hash || '').replace('#tab=', '');
	var initial = hash && document.querySelector('.bw-section-panel[data-panel="' + hash + '"]') ? hash : 'organization';
	activate(initial);
})();
</script>
