<?php
/**
 * Schema attributes template.
 *
 * @package WLC_Algolia
 *
 * @var array  $args
 * @var array  $options
 * @var array  $index_types
 * @var string $tab
 * @var string $post_type
 */

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

( static function ( array $wlc_algolia_scope_vars ) {
	extract( $wlc_algolia_scope_vars, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract

	?>
<div class="wlc-cpt-fields-toggle">
	<label class="wlc-algolia-toggle">
		<input type="checkbox" id="wlc-cpt-show-fields" value="1" />
		<span class="wlc-algolia-toggle-slider"></span>
	</label>
	<label for="wlc-cpt-show-fields" class="wlc-algolia-checkbox-label">
		<?php esc_html_e( 'Show all post fields', 'wlc-search-integration-with-algolia' ); ?>
	</label>
</div>

<div class="wlc-cpt-fields">
<table class="schema-container container index-container">
	<thead>
	<tr>
		<th><?php esc_html_e( 'Post type', 'wlc-search-integration-with-algolia' ); ?></th>
		<th><?php esc_html_e( 'Type', 'wlc-search-integration-with-algolia' ); ?></th>
		<th><?php esc_html_e( 'enable Indexing', 'wlc-search-integration-with-algolia' ); ?></th>
		<th><?php esc_html_e( 'Facet', 'wlc-search-integration-with-algolia' ); ?></th>
		<th><?php esc_html_e( 'index name', 'wlc-search-integration-with-algolia' ); ?></th>
	</tr>
	</thead>
	<tbody>
	<?php if ( ! empty( $args['args']['cpt_fields'] ) ) : ?>
		<?php foreach ( $args['args']['cpt_fields'] as $cpt_field ) : ?>
			<?php
			// Determine if this field is enabled for indexing (saved value wins, otherwise defaults).
			$wlc_algolia_is_enabled = false;
			if ( isset( $options[ $cpt_field ]['index'] ) ) {
				$wlc_algolia_is_enabled = '1' === (string) $options[ $cpt_field ]['index'];
			} else {
				$wlc_algolia_is_enabled = ! empty( $default[ $post_type ][ $cpt_field ]['index'] );
			}

			$wlc_algolia_row_class = $wlc_algolia_is_enabled ? 'wlc-cpt-row-enabled' : 'wlc-cpt-row-disabled';
			?>
			<tr class="wlc-cpt-row <?php echo esc_attr( $wlc_algolia_row_class ); ?>">
				<td>
					<?php echo esc_attr( $cpt_field ); ?>
					<input type="hidden" name="wlc_algolia_settings_<?php echo esc_attr( $tab ); ?>_<?php echo esc_attr( $post_type ); ?>[<?php echo esc_attr( $cpt_field ); ?>]"/>
					<input type="hidden" name="wlc_algolia_settings_<?php echo esc_attr( $tab ); ?>_<?php echo esc_attr( $post_type ); ?>[<?php echo esc_attr( 'selected_post_type' ); ?>]" value="<?php echo esc_attr( $post_type ); ?>" />
				</td>
				<td>
					<label>
						<select name="wlc_algolia_settings_<?php echo esc_attr( $tab ); ?>_<?php echo esc_attr( $post_type ); ?>[<?php echo esc_attr( $cpt_field ); ?>][type]">
							<?php foreach ( $index_types as $index => $type ) : //phpcs:ignore?>
								<?php
								// Use saved value, otherwise fallback to defaults.
								$selected_type = $options[ $cpt_field ]['type'] ?? ( $default[ $post_type ][ $cpt_field ]['type'] ?? '' ); //phpcs:ignore
								?>
								<option value="<?php echo esc_attr( $index ); ?>" <?php selected( $index, esc_attr( $selected_type ), true ); ?>><?php echo esc_html( $type ); ?></option>
							<?php endforeach; ?>
						</select>
					</label>
				</td>
				<td>
					<label>
						<?php
						$wlc_algolia_checked_index = isset( $options[ $cpt_field ]['index'] )
							? $options[ $cpt_field ]['index']
							: ( ( $default[ $post_type ][ $cpt_field ]['index'] ?? false ) ? '1' : '0' );
						?>
						<input
							type="hidden"
							value="0"
							name="wlc_algolia_settings_<?php echo esc_attr( $tab ); ?>_<?php echo esc_attr( $post_type ); ?>[<?php echo esc_attr( $cpt_field ); ?>][index]" />
						<input
							type="checkbox"
							class="<?php echo esc_attr( 'index' ); ?>"
							name="wlc_algolia_settings_<?php echo esc_attr( $tab ); ?>_<?php echo esc_attr( $post_type ); ?>[<?php echo esc_attr( $cpt_field ); ?>][index]"
							id="<?php echo esc_attr( 'index-' . $cpt_field ); ?>"
							value="1"
							<?php echo checked( 1, $wlc_algolia_checked_index, false ); ?> />
						<span class="slider round"></span>
					</label>
				</td>
				<td>
					<label>
						<?php
						$wlc_algolia_checked_facet = isset( $options[ $cpt_field ]['facet'] )
							? $options[ $cpt_field ]['facet']
							: '0';
						?>
						<input
							type="hidden"
							value="0"
							name="wlc_algolia_settings_<?php echo esc_attr( $tab ); ?>_<?php echo esc_attr( $post_type ); ?>[<?php echo esc_attr( $cpt_field ); ?>][facet]" />
						<input
							type="checkbox"
							class="<?php echo esc_attr( 'facet' ); ?>"
							name="wlc_algolia_settings_<?php echo esc_attr( $tab ); ?>_<?php echo esc_attr( $post_type ); ?>[<?php echo esc_attr( $cpt_field ); ?>][facet]"
							id="<?php echo esc_attr( 'facet-' . $cpt_field ); ?>"
							value="1"
							<?php echo checked( 1, $wlc_algolia_checked_facet, false ); ?> />
						<span class="slider round"></span>
					</label>
				</td>
				<td>
					<?php
					// Generate index name based on post type and field.
					$wlc_algolia_default_index_name = get_option( 'wlc_algolia_settings_general' );
					$wlc_algolia_index_name_prefix  = 'wp_';
					if ( isset( $wlc_algolia_default_index_name['general'] ) ) {
						$wlc_algolia_index_name_prefix = $wlc_algolia_default_index_name['general']['index_name'] ?? 'wp_';
					}
					$wlc_algolia_field_index_name = $wlc_algolia_index_name_prefix . '_' . $post_type . '_' . $cpt_field;
					echo esc_html( $wlc_algolia_field_index_name );
					?>
				</td>
			</tr>
		<?php endforeach; ?>
	<?php endif; ?>
	</tbody>
</table>
</div>

	<?php
} )( get_defined_vars() );
?>
