<?php
/**
 * Autocomplete settings template.
 *
 * @package WLC_Algolia
 *
 * @var array  $post_types
 * @var string $settings_tab
 * @var array  $checked
 * @var string $default_index_name
 * @var array  $options
 * @var array  $indexes
 */

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="container">
	<div class="enable-autocomplete">
		<label class="enable-autocomplete-label">
			<?php
			$wlc_algolia_enabled_autocomplete = $options['enable-autocomplete'] ?? '';
			?>
			<input
				type="checkbox"
				class="<?php echo esc_attr( 'autocomplete-enable' ); ?>"
				name="wlc_algolia_settings_<?php echo esc_attr( $settings_tab ); ?>[<?php echo esc_attr( 'enable-autocomplete' ); ?>]"
				id="<?php echo esc_attr( 'enable-autocomplete' ); ?>"
				value="1" <?php echo checked( 1, esc_attr( $wlc_algolia_enabled_autocomplete ), false ); ?>
			/>
			<span class="slider round"></span>
			<span class="wlc-algolia-toggle-label"><?php esc_html_e( 'Enable Autocomplete', 'wlc-search-integration-with-algolia' ); ?></span>
		</label>
	</div>

	<table class="index-container container">
		<thead>
		<tr>
			<th><?php esc_html_e( 'Post type', 'wlc-search-integration-with-algolia' ); ?></th>
			<th><?php esc_html_e( 'enable', 'wlc-search-integration-with-algolia' ); ?></th>
			<th><?php esc_html_e( 'index name', 'wlc-search-integration-with-algolia' ); ?></th>
			<th><?php esc_html_e( 'max suggestions', 'wlc-search-integration-with-algolia' ); ?></th>
			<th><?php esc_html_e( 'custom heading for autocomplete', 'wlc-search-integration-with-algolia' ); ?></th>
		</tr>
		</thead>
		<tbody>
		<?php if ( ! empty( $post_types ) ) : ?>
			<?php
			foreach ( $post_types as $wlc_algolia_single_cpt ) : // phpcs:ignore
				$wlc_algolia_index_name      = in_array( $wlc_algolia_single_cpt->name, $indexes, true ) ? $default_index_name . '_' . $wlc_algolia_single_cpt->name : $default_index_name;
				$wlc_algolia_max_suggestions = ! empty( $options[ $wlc_algolia_single_cpt->name ]['max-suggestions'] ) ? $options[ $wlc_algolia_single_cpt->name ]['max-suggestions'] : 10;
				$wlc_algolia_custom_heading  = ! empty( $options[ $wlc_algolia_single_cpt->name ]['custom-heading'] ) ? $options[ $wlc_algolia_single_cpt->name ]['custom-heading'] : '';
				?>
				<tr class="">
					<td>
						<?php echo esc_html( $wlc_algolia_single_cpt->label ); ?>
					</td>
					<td>
						<label>
							<input type="hidden" name="wlc_algolia_settings_<?php echo esc_attr( $settings_tab ); ?>[<?php echo esc_attr( $wlc_algolia_single_cpt->name ); ?>]" />
							<?php $wlc_algolia_checked_index = $options[ $wlc_algolia_single_cpt->name ]['enable'] ?? ''; ?>
							<input
								type="checkbox"
								class="<?php echo esc_attr( 'index autocomplete-input' ); ?>"
								name="wlc_algolia_settings_<?php echo esc_attr( $settings_tab ); ?>[<?php echo esc_attr( $wlc_algolia_single_cpt->name ); ?>][<?php echo esc_attr( 'enable' ); ?>]"
								id="<?php echo esc_attr( 'index' ); ?>"<?php //phpcs:ignore ?>
								value="1" <?php echo checked( 1, esc_attr( $wlc_algolia_checked_index ), false ); ?>
							/>
							<span class="slider round autocomplete-switch"></span>
						</label>
					</td>
					<td>
						<?php echo esc_attr( $wlc_algolia_index_name ); ?>
						<input
							type="hidden"
							name="wlc_algolia_settings_<?php echo esc_attr( $settings_tab ); ?>[<?php echo esc_attr( $wlc_algolia_single_cpt->name ); ?>][<?php echo esc_attr( 'index_name' ); ?>]"
							value="<?php echo esc_attr( $wlc_algolia_index_name ); ?>"
						/>
						<input
							type="hidden"
							name="wlc_algolia_settings_<?php echo esc_attr( $settings_tab ); ?>[<?php echo esc_attr( $wlc_algolia_single_cpt->name ); ?>][<?php echo esc_attr( 'index_id' ); ?>]"
							value="<?php echo esc_attr( 'searchable_posts_' . $wlc_algolia_single_cpt->name ); ?>"
						/>
						<input
							type="hidden"
							name="wlc_algolia_settings_<?php echo esc_attr( $settings_tab ); ?>[<?php echo esc_attr( $wlc_algolia_single_cpt->name ); ?>][<?php echo esc_attr( 'tmpl_suggestion' ); ?>]"
							value="<?php echo esc_attr( 'autocomplete_' . $wlc_algolia_single_cpt->name . '_suggestion' ); ?>"
						/>
					</td>
					<td>
						<input
							id="max-suggestions-<?php echo esc_attr( $wlc_algolia_single_cpt->name ); ?>"
							min="0"
							type="number"
							name="<?php echo esc_attr( "wlc_algolia_settings_{$settings_tab}[{$wlc_algolia_single_cpt->name}][max-suggestions]" ); ?>"
							value="<?php echo esc_attr( $wlc_algolia_max_suggestions ); ?>"
							class="max-suggestions"
						/>
					</td>
					<td>
						<input
							id="custom-heading-<?php echo esc_attr( $wlc_algolia_single_cpt->name ); ?>"
							type="text"
							name="<?php echo esc_attr( "wlc_algolia_settings_{$settings_tab}[{$wlc_algolia_single_cpt->name}][custom-heading]" ); ?>"
							value="<?php echo esc_attr( $wlc_algolia_custom_heading ); ?>"
							class="custom-heading"
						/>
					</td>
				</tr>
			<?php endforeach; ?>
		<?php endif; ?>
		</tbody>
	</table>
</div>
	<?php
} )( get_defined_vars() );
