<?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', '' );

$room_post_type  = get_option( 'bw_schema_room_post_type', '' );
$room_settings   = class_exists( 'BW_Schema_Room' ) ? BW_Schema_Room::get_module_settings() : array(
	'currency' => 'USD', 'rate_unit' => 'DAY', 'rate_stale_days' => 180,
);

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' ),
	'rooms'        => array( 'label' => __( 'Rooms', 'bw-ai-schema-pro' ),        'icon' => 'dashicons-admin-multisite' ),
	'locations'    => array( 'label' => __( 'Locations', 'bw-ai-schema-pro' ),    'icon' => 'dashicons-location' ),
	'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>
		<a href="<?php echo admin_url( 'options-general.php?page=bw-ai-schema-tools' ); ?>" class="nav-tab">
			<span class="dashicons dashicons-database-export"></span> <?php _e( 'Export / Import', '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><?php _e( 'Areas served', 'bw-ai-schema-pro' ); ?></th>
					<td>
						<?php
						$areas = is_array( $org_data['areaServed'] ?? null ) ? implode( ', ', $org_data['areaServed'] ) : ( $org_data['areaServed'] ?? '' );
						if ( '' !== trim( (string) $areas ) ) :
							?>
							<p style="margin-top:0;"><code><?php echo esc_html( $areas ); ?></code></p>
						<?php endif; ?>
						<p class="description">
							<?php
							printf(
								/* translators: %s: link to the Locations tab */
								esc_html__( 'Moved to the %s tab, where an area can be a city, province/state, country, or a radius from a point rather than a bare string. Existing values above are carried over automatically the first time you save that tab.', 'bw-ai-schema-pro' ),
								'<a href="#tab=locations" class="bw-goto-locations">' . esc_html__( 'Locations', 'bw-ai-schema-pro' ) . '</a>'
							);
							?>
						</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>

		<!-- ========== ROOMS TAB ========== -->
		<div class="bw-section-panel" data-panel="rooms" style="display: none;">
			<h2><?php _e( 'Rooms & Accommodations', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description">
				<?php _e( 'Map a post type to "rooms" for lodging clients — resorts, hotels, villa rentals. Each room page emits a HotelRoom / Suite node, and the property Organization gains a containsPlace list linking to them. The site Organization IS the property: set its type to Resort or Hotel on the Organization tab.', 'bw-ai-schema-pro' ); ?>
			</p>

			<table class="form-table">
				<tr>
					<th><label for="room_post_type"><?php _e( 'Room post type', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="room_post_type" name="room_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( $room_post_type, $pt->name ); ?>><?php echo esc_html( $pt->labels->name ); ?></option>
								<?php
							}
							?>
						</select>
						<p class="description">
						<?php _e( 'Leave unselected to keep the module off entirely.', 'bw-ai-schema-pro' ); ?>
						<br />
						<?php
						printf(
							/* translators: %s: link to the Permalinks settings screen */
							esc_html__( 'If your room URLs return 404, the post type was registered after permalinks were last saved — open %s and click Save once to flush the rewrite rules.', 'bw-ai-schema-pro' ),
							'<a href="' . esc_url( admin_url( 'options-permalink.php' ) ) . '">' . esc_html__( 'Settings → Permalinks', 'bw-ai-schema-pro' ) . '</a>'
						);
						?>
					</p>
					</td>
				</tr>
				<tr>
					<th><label for="room_currency"><?php _e( 'Default currency', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<input type="text" id="room_currency" maxlength="3" name="room_currency" value="<?php echo esc_attr( $room_settings['currency'] ); ?>" class="small-text" />
						<p class="description"><?php _e( 'Three-letter ISO code (USD, CAD, EUR). Used when a room doesn\'t set its own.', 'bw-ai-schema-pro' ); ?></p>
					</td>
				</tr>
				<tr>
					<th><label for="room_rate_unit"><?php _e( 'Default rate period', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<select id="room_rate_unit" name="room_rate_unit">
							<?php
							$rate_units = class_exists( 'BW_Schema_Room' ) ? BW_Schema_Room::get_rate_units() : array( 'DAY' => 'per night' );
							foreach ( $rate_units as $code => $label ) :
								?>
								<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $room_settings['rate_unit'], $code ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<tr>
					<th><label for="room_rate_stale_days"><?php _e( 'Rate staleness window', 'bw-ai-schema-pro' ); ?></label></th>
					<td>
						<input type="number" min="0" step="1" id="room_rate_stale_days" name="room_rate_stale_days" value="<?php echo esc_attr( $room_settings['rate_stale_days'] ); ?>" class="small-text" />
						<?php _e( 'days', 'bw-ai-schema-pro' ); ?>
						<p class="description">
							<?php _e( 'A rate not confirmed within this many days is withheld from the schema output, and the room list warns about it. Seasonal resort rates go stale silently — a wrong price in structured data is worse than no price. Set to 0 to disable the guard.', 'bw-ai-schema-pro' ); ?>
						</p>
					</td>
				</tr>
			</table>

			<?php
			if ( $room_post_type && class_exists( 'BW_Schema_Room' ) ) :
				$room_ids = BW_Schema_Room::get_room_ids();
				if ( $room_ids ) :
					?>
					<h3 style="margin-top: 30px;"><?php _e( 'Rooms', 'bw-ai-schema-pro' ); ?></h3>
					<p class="description"><?php _e( 'Per-room detail is edited on each room\'s own edit screen. This table is the fleet view — type, starting rate, and whether the rate has gone stale.', 'bw-ai-schema-pro' ); ?></p>
					<table class="widefat striped" style="margin-top: 10px;">
						<thead>
							<tr>
								<th style="width: 34%;"><?php _e( 'Room', 'bw-ai-schema-pro' ); ?></th>
								<th style="width: 16%;"><?php _e( 'Type', 'bw-ai-schema-pro' ); ?></th>
								<th style="width: 18%;"><?php _e( 'From rate', 'bw-ai-schema-pro' ); ?></th>
								<th style="width: 18%;"><?php _e( 'Rate confirmed', 'bw-ai-schema-pro' ); ?></th>
								<th style="width: 14%; text-align: center;"><?php _e( 'Exclude', 'bw-ai-schema-pro' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php
							$room_type_labels = BW_Schema_Room::get_room_types();
							foreach ( $room_ids as $room_id ) :
								$r_type  = get_post_meta( $room_id, '_bw_schema_room_type', true );
								$r_type  = isset( $room_type_labels[ $r_type ] ) ? $room_type_labels[ $r_type ] : $room_type_labels['HotelRoom'];
								$r_offer = get_post_meta( $room_id, '_bw_schema_room_offer', true );
								$r_offer = is_array( $r_offer ) ? $r_offer : array();
								$r_excl  = BW_Schema_Room::is_excluded( $room_id );
								$r_stale = BW_Schema_Room::rate_is_stale( $room_id );
								?>
								<tr>
									<td>
										<strong><a href="<?php echo esc_url( get_edit_post_link( $room_id ) ); ?>"><?php echo esc_html( get_the_title( $room_id ) ); ?></a></strong>
									</td>
									<td><?php echo esc_html( $r_type ); ?></td>
									<td>
										<?php
										if ( ! empty( $r_offer['from_price'] ) ) {
											echo esc_html( ( $r_offer['currency'] ?? $room_settings['currency'] ) . ' ' . $r_offer['from_price'] );
										} else {
											echo '<span style="color:#787c82;">—</span>';
										}
										?>
									</td>
									<td>
										<?php
										if ( ! empty( $r_offer['reviewed'] ) ) {
											echo esc_html( $r_offer['reviewed'] );
										} elseif ( ! empty( $r_offer['from_price'] ) ) {
											echo '<span style="color:#996800;">' . esc_html__( 'never', 'bw-ai-schema-pro' ) . '</span>';
										} else {
											echo '<span style="color:#787c82;">—</span>';
										}
										if ( $r_stale ) {
											echo ' <span class="dashicons dashicons-warning" style="color:#dba617;" title="' . esc_attr__( 'Stale — offers withheld from output', 'bw-ai-schema-pro' ) . '"></span>';
										}
										?>
									</td>
									<td style="text-align: center;">
										<input type="checkbox" name="room_exclude[<?php echo (int) $room_id; ?>]" value="1" <?php checked( $r_excl ); ?> />
									</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
					<input type="hidden" name="room_table_posted" value="1" />
				<?php else : ?>
					<p style="margin-top: 20px;"><em><?php _e( 'No published entries in that post type yet.', 'bw-ai-schema-pro' ); ?></em></p>
				<?php endif;
			endif; ?>
		</div>

		<!-- ========== LOCATIONS TAB ========== -->
		<?php
		$bw_locations     = class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_locations() : array();
		$bw_service_areas = class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_service_areas() : array();
		$bw_area_kinds    = class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_area_kinds() : array();
		$bw_radius_units  = class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_radius_units() : array();
		$bw_loc_types     = class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_location_types() : array();
		$bw_countries     = class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_countries() : array();

		/**
		 * Render one service-area repeater row.
		 *
		 * @param string $name_prefix Field name prefix, e.g. "service_areas[0]".
		 * @param array  $area        Stored row (or empty for a blank row).
		 */
		$bw_render_area_row = function ( $name_prefix, $area = array() ) use ( $bw_area_kinds, $bw_radius_units ) {
			$kind      = isset( $area['kind'] ) ? $area['kind'] : 'city';
			$is_radius = ( 'radius' === $kind );
			?>
			<div class="bw-area-row" style="display:flex; gap:8px; align-items:flex-start; margin-bottom:8px; flex-wrap:wrap;">
				<select name="<?php echo esc_attr( $name_prefix ); ?>[kind]" class="bw-area-kind" style="flex:0 0 170px;">
					<?php foreach ( $bw_area_kinds as $k => $meta ) : ?>
						<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $kind, $k ); ?>><?php echo esc_html( $meta['label'] ); ?></option>
					<?php endforeach; ?>
				</select>
				<input type="text" name="<?php echo esc_attr( $name_prefix ); ?>[name]"
					value="<?php echo esc_attr( $area['name'] ?? '' ); ?>"
					placeholder="<?php esc_attr_e( 'e.g. Calgary, Alberta, Canada', 'bw-ai-schema-pro' ); ?>"
					style="flex:1 1 200px;" />
				<span class="bw-area-radius-fields" style="display:<?php echo $is_radius ? 'flex' : 'none'; ?>; gap:6px; flex-wrap:wrap;">
					<input type="text" name="<?php echo esc_attr( $name_prefix ); ?>[lat]" value="<?php echo esc_attr( $area['lat'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'latitude', 'bw-ai-schema-pro' ); ?>" style="width:110px;" />
					<input type="text" name="<?php echo esc_attr( $name_prefix ); ?>[lng]" value="<?php echo esc_attr( $area['lng'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'longitude', 'bw-ai-schema-pro' ); ?>" style="width:110px;" />
					<input type="number" min="0" step="any" name="<?php echo esc_attr( $name_prefix ); ?>[radius]" value="<?php echo esc_attr( $area['radius'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'radius', 'bw-ai-schema-pro' ); ?>" style="width:90px;" />
					<select name="<?php echo esc_attr( $name_prefix ); ?>[unit]" style="width:120px;">
						<?php foreach ( $bw_radius_units as $u => $meta ) : ?>
							<option value="<?php echo esc_attr( $u ); ?>" <?php selected( $area['unit'] ?? 'km', $u ); ?>><?php echo esc_html( $meta['label'] ); ?></option>
						<?php endforeach; ?>
					</select>
				</span>
				<span class="bw-remove-area dashicons dashicons-no-alt" title="<?php esc_attr_e( 'Remove', 'bw-ai-schema-pro' ); ?>" style="color:#b32d2e; cursor:pointer; padding:4px;"></span>
			</div>
			<?php
		};

		/**
		 * Render one location card.
		 *
		 * @param int|string $index    Row index, or the literal __i__ placeholder for the JS template.
		 * @param array      $location Stored row (or empty for a blank row).
		 */
		$bw_render_location = function ( $index, $location = array() ) use ( $bw_loc_types, $bw_countries, $bw_render_area_row ) {
			$prefix  = 'locations[' . $index . ']';
			$country = $location['country'] ?? '';
			$mixed   = class_exists( 'BW_Schema_Location' ) && BW_Schema_Location::has_mixed_signals( $location );
			?>
			<div class="bw-location-card" style="background:#f6f7f7; border-left:3px solid #2271b1; border-radius:4px; padding:14px; margin-bottom:14px;">
				<input type="hidden" name="<?php echo esc_attr( $prefix ); ?>[id]" value="<?php echo esc_attr( $location['id'] ?? '' ); ?>" />

				<div style="display:flex; justify-content:space-between; align-items:center; gap:10px; margin-bottom:10px;">
					<strong class="bw-location-title"><?php echo esc_html( $location['name'] ?? __( 'New location', 'bw-ai-schema-pro' ) ); ?></strong>
					<button type="button" class="button-link bw-remove-location" style="color:#b32d2e;"><?php esc_html_e( 'Remove location', 'bw-ai-schema-pro' ); ?></button>
				</div>

				<?php if ( $mixed ) : ?>
					<div style="background:#fcf9e8; border-left:4px solid #dba617; padding:8px 10px; margin-bottom:10px;">
						<?php esc_html_e( 'This location has both a street address and a radius service area. That is allowed, and we publish both — but it can read as mixed storefront / service-area signals. If this branch is really a dispatch office rather than somewhere customers visit, consider clearing the street address.', 'bw-ai-schema-pro' ); ?>
					</div>
				<?php endif; ?>

				<div style="display:flex; gap:12px; flex-wrap:wrap;">
					<p style="flex:2 1 260px;">
						<label><strong><?php esc_html_e( 'Location name', 'bw-ai-schema-pro' ); ?></strong></label><br />
						<input type="text" class="widefat bw-location-name" name="<?php echo esc_attr( $prefix ); ?>[name]" value="<?php echo esc_attr( $location['name'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'e.g. Prairie Air — Calgary', 'bw-ai-schema-pro' ); ?>" />
					</p>
					<p style="flex:1 1 200px;">
						<label><strong><?php esc_html_e( 'Business type', 'bw-ai-schema-pro' ); ?></strong></label><br />
						<select class="widefat" name="<?php echo esc_attr( $prefix ); ?>[type]">
							<?php foreach ( $bw_loc_types as $t => $label ) : ?>
								<option value="<?php echo esc_attr( $t ); ?>" <?php selected( $location['type'] ?? 'LocalBusiness', $t ); ?>><?php echo esc_html( $label ); ?></option>
							<?php endforeach; ?>
						</select>
					</p>
				</div>

				<div style="display:flex; gap:12px; flex-wrap:wrap;">
					<p style="flex:2 1 260px;">
						<label><?php esc_html_e( 'Street address', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="text" class="widefat" name="<?php echo esc_attr( $prefix ); ?>[street]" value="<?php echo esc_attr( $location['street'] ?? '' ); ?>" />
					</p>
					<p style="flex:1 1 160px;">
						<label><?php esc_html_e( 'City', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="text" class="widefat" name="<?php echo esc_attr( $prefix ); ?>[city]" value="<?php echo esc_attr( $location['city'] ?? '' ); ?>" />
					</p>
				</div>

				<div style="display:flex; gap:12px; flex-wrap:wrap;">
					<p style="flex:1 1 160px;">
						<label class="bw-region-label"><?php echo esc_html( class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_region_label( $country ) : __( 'Region', 'bw-ai-schema-pro' ) ); ?></label><br />
						<input type="text" class="widefat bw-region-input" list="bw_schema_regions" name="<?php echo esc_attr( $prefix ); ?>[region]" value="<?php echo esc_attr( $location['region'] ?? '' ); ?>" placeholder="<?php esc_attr_e( 'e.g. AB', 'bw-ai-schema-pro' ); ?>" />
					</p>
					<p style="flex:1 1 140px;">
						<label><?php esc_html_e( 'Postal / ZIP code', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="text" class="widefat bw-postal-input" name="<?php echo esc_attr( $prefix ); ?>[postal]" value="<?php echo esc_attr( $location['postal'] ?? '' ); ?>" placeholder="<?php echo esc_attr( class_exists( 'BW_Schema_Location' ) ? BW_Schema_Location::get_postal_placeholder( $country ) : '' ); ?>" />
					</p>
					<p style="flex:1 1 160px;">
						<label><?php esc_html_e( 'Country', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="text" class="widefat bw-country-input" list="bw_schema_countries" maxlength="2" name="<?php echo esc_attr( $prefix ); ?>[country]" value="<?php echo esc_attr( $country ); ?>" placeholder="CA" />
						<span class="description"><?php esc_html_e( 'Two-letter ISO code', 'bw-ai-schema-pro' ); ?></span>
					</p>
				</div>

				<div style="display:flex; gap:12px; flex-wrap:wrap;">
					<p style="flex:1 1 140px;">
						<label><?php esc_html_e( 'Latitude', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="text" class="widefat" name="<?php echo esc_attr( $prefix ); ?>[lat]" value="<?php echo esc_attr( $location['lat'] ?? '' ); ?>" placeholder="51.04473" />
					</p>
					<p style="flex:1 1 140px;">
						<label><?php esc_html_e( 'Longitude', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="text" class="widefat" name="<?php echo esc_attr( $prefix ); ?>[lng]" value="<?php echo esc_attr( $location['lng'] ?? '' ); ?>" placeholder="-114.07131" />
					</p>
					<p style="flex:1 1 160px;">
						<label><?php esc_html_e( 'Telephone', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="text" class="widefat" name="<?php echo esc_attr( $prefix ); ?>[telephone]" value="<?php echo esc_attr( $location['telephone'] ?? '' ); ?>" placeholder="+1-403-555-0100" />
					</p>
					<p style="flex:1 1 180px;">
						<label><?php esc_html_e( 'Email', 'bw-ai-schema-pro' ); ?></label><br />
						<input type="email" class="widefat" name="<?php echo esc_attr( $prefix ); ?>[email]" value="<?php echo esc_attr( $location['email'] ?? '' ); ?>" />
					</p>
				</div>

				<p>
					<label><?php esc_html_e( 'Page about this location (optional)', 'bw-ai-schema-pro' ); ?></label><br />
					<input type="url" class="widefat" name="<?php echo esc_attr( $prefix ); ?>[url]" value="<?php echo esc_attr( $location['url'] ?? '' ); ?>" placeholder="https://…" />
				</p>

				<p>
					<label>
						<input type="checkbox" name="<?php echo esc_attr( $prefix ); ?>[use_main_hours]" value="1" <?php checked( ! empty( $location['use_main_hours'] ) ); ?> />
						<?php esc_html_e( 'Uses the main opening hours (set on the Advanced tab)', 'bw-ai-schema-pro' ); ?>
					</label>
				</p>

				<p style="margin-bottom:6px;"><strong><?php esc_html_e( 'Areas this location serves', 'bw-ai-schema-pro' ); ?></strong></p>
				<div class="bw-areas-repeater">
					<?php
					$loc_areas = ( isset( $location['areas'] ) && is_array( $location['areas'] ) && $location['areas'] )
						? $location['areas']
						: array( array() );
					foreach ( $loc_areas as $ai => $area ) {
						$bw_render_area_row( $prefix . '[areas][' . $ai . ']', $area );
					}
					?>
				</div>
				<button type="button" class="button bw-add-area"><span class="dashicons dashicons-plus-alt2"></span> <?php esc_html_e( 'Add area', 'bw-ai-schema-pro' ); ?></button>
			</div>
			<?php
		};
		?>
		<div class="bw-section-panel" data-panel="locations" style="display: none;">
			<h2><?php _e( 'Locations & service areas', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description" style="max-width: 860px;">
				<?php _e( 'Covers three shapes. <strong>One location:</strong> leave the Locations list empty and use the address on the Contact tab — add service areas below if you travel to customers. <strong>Several real branches:</strong> add one entry per branch; each is emitted as its own LocalBusiness linked to the parent company. <strong>Service-area business</strong> (you drive to the customer): add service areas by city, province/state, country, or a radius from a point.', 'bw-ai-schema-pro' ); ?>
			</p>

			<h3 style="margin-top: 24px;"><?php _e( 'Service areas (whole company)', 'bw-ai-schema-pro' ); ?></h3>
			<p class="description">
				<?php _e( 'Emitted as <code>areaServed</code> on the parent Organization. A radius entry needs a centre point and a distance — a radius without both is meaningless, so incomplete rows are dropped rather than published half-built.', 'bw-ai-schema-pro' ); ?>
			</p>
			<div class="bw-areas-repeater" style="max-width: 900px;">
				<?php
				$site_areas = $bw_service_areas ? $bw_service_areas : array( array() );
				foreach ( $site_areas as $i => $area ) {
					$bw_render_area_row( 'service_areas[' . $i . ']', $area );
				}
				?>
			</div>
			<button type="button" class="button bw-add-area"><span class="dashicons dashicons-plus-alt2"></span> <?php _e( 'Add area', 'bw-ai-schema-pro' ); ?></button>
			<input type="hidden" name="bw_locations_tab_posted" value="1" />

			<h3 style="margin-top: 32px;"><?php _e( 'Locations', 'bw-ai-schema-pro' ); ?></h3>
			<p class="description" style="max-width: 860px;">
				<?php _e( 'Leave empty if the business has a single location. Each entry becomes a LocalBusiness node with <code>parentOrganization</code> pointing at the parent company, and the parent gains a <code>subOrganization</code> reference back. Full location details are emitted on the homepage.', 'bw-ai-schema-pro' ); ?>
			</p>

			<div id="bw-locations-list" style="max-width: 900px;">
				<?php foreach ( $bw_locations as $i => $location ) {
					$bw_render_location( $i, $location );
				} ?>
			</div>
			<button type="button" class="button button-secondary" id="bw-add-location">
				<span class="dashicons dashicons-plus-alt2"></span> <?php _e( 'Add location', 'bw-ai-schema-pro' ); ?>
			</button>

			<script type="text/template" id="bw-location-template">
				<?php $bw_render_location( '__i__' ); ?>
			</script>

			<datalist id="bw_schema_countries">
				<?php foreach ( $bw_countries as $code => $label ) : ?>
					<option value="<?php echo esc_attr( $code ); ?>"><?php echo esc_html( $label ); ?></option>
				<?php endforeach; ?>
			</datalist>
			<datalist id="bw_schema_regions">
				<?php
				if ( class_exists( 'BW_Schema_Location' ) ) {
					foreach ( BW_Schema_Location::get_ca_regions() as $code => $label ) {
						echo '<option value="' . esc_attr( $code ) . '">' . esc_html( $label . ' (CA)' ) . '</option>';
					}
					foreach ( BW_Schema_Location::get_us_regions() as $code => $label ) {
						echo '<option value="' . esc_attr( $code ) . '">' . esc_html( $label . ' (US)' ) . '</option>';
					}
				}
				?>
			</datalist>
		</div>

		<script>
		jQuery(function ($) {
			var $panel = $('.bw-section-panel[data-panel="locations"]');
			if (!$panel.length) { return; }

			// Region label + postal placeholder follow the country. "State" is
			// wrong in Canada and "Province" is wrong in the US; anywhere else we
			// stay neutral rather than guess.
			var REGION_LABELS = {
				CA: <?php echo wp_json_encode( __( 'Province', 'bw-ai-schema-pro' ) ); ?>,
				US: <?php echo wp_json_encode( __( 'State', 'bw-ai-schema-pro' ) ); ?>
			};
			var REGION_FALLBACK = <?php echo wp_json_encode( __( 'Region / Province / State', 'bw-ai-schema-pro' ) ); ?>;
			var POSTAL_HINTS = { CA: 'A1A 1A1', US: '12345', GB: 'SW1A 1AA', AU: '2000', NL: '1012 AB', DE: '10115', FR: '75001' };

			function applyCountry($card) {
				var code = ($card.find('.bw-country-input').val() || '').toUpperCase();
				$card.find('.bw-region-label').text(REGION_LABELS[code] || REGION_FALLBACK);
				$card.find('.bw-postal-input').attr('placeholder', POSTAL_HINTS[code] || '');
			}

			$panel.on('input change', '.bw-country-input', function () {
				applyCountry($(this).closest('.bw-location-card'));
			});

			// Radius fields only make sense for a radius entry.
			$panel.on('change', '.bw-area-kind', function () {
				$(this).closest('.bw-area-row')
					.find('.bw-area-radius-fields')
					.css('display', $(this).val() === 'radius' ? 'flex' : 'none');
			});

			// Card heading tracks the name field so a long list stays readable.
			$panel.on('input', '.bw-location-name', function () {
				var val = $.trim($(this).val());
				$(this).closest('.bw-location-card').find('.bw-location-title')
					.text(val || <?php echo wp_json_encode( __( 'New location', 'bw-ai-schema-pro' ) ); ?>);
			});

			// Add an area row to whichever repeater the button belongs to (the
			// site-wide one, or one nested inside a location card).
			$panel.on('click', '.bw-add-area', function () {
				var $repeater = $(this).prevAll('.bw-areas-repeater').first();
				if (!$repeater.length) { $repeater = $(this).siblings('.bw-areas-repeater').first(); }
				var $rows = $repeater.find('.bw-area-row');
				var $row = $rows.first().clone();
				var next = $rows.length;

				$row.find('input, select').each(function () {
					var name = $(this).attr('name');
					if (name) {
						// Rewrite only the final [index][field] pair, so nested
						// names like locations[0][areas][2][kind] stay intact.
						$(this).attr('name', name.replace(/\[(\d+)\]\[(\w+)\]$/, '[' + next + '][$2]'));
					}
					if ($(this).is('select')) { $(this).prop('selectedIndex', 0); } else { $(this).val(''); }
				});
				$row.find('.bw-area-radius-fields').css('display', 'none');
				$repeater.append($row);
			});

			$panel.on('click', '.bw-remove-area', function () {
				var $repeater = $(this).closest('.bw-areas-repeater');
				if ($repeater.find('.bw-area-row').length > 1) {
					$(this).closest('.bw-area-row').remove();
				} else {
					$(this).closest('.bw-area-row').find('input').val('');
				}
			});

			// Gaps in the index sequence are fine — PHP iterates whatever arrives,
			// so removal never needs a reindex pass.
			$('#bw-add-location').on('click', function () {
				var tpl = $('#bw-location-template').html();
				var next = $('#bw-locations-list .bw-location-card').length;
				var seq = next;
				while ($('#bw-locations-list').find('[name^="locations[' + seq + ']"]').length) { seq++; }
				var $card = $(tpl.replace(/__i__/g, seq));
				$('#bw-locations-list').append($card);
				applyCountry($card);
			});

			$panel.on('click', '.bw-remove-location', function () {
				$(this).closest('.bw-location-card').remove();
			});

			$panel.find('.bw-location-card').each(function () { applyCountry($(this)); });
		});
		</script>

		<!-- ========== 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 ( $room_post_type && $pt->name === $room_post_type ) {
						$handled = __( 'AI Schema Pro emits this directly as HotelRoom / Suite on each room 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>
