<?php

use Elementor\Icons_Manager;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$settings    = $this->get_settings_for_display();
$layout      = $settings['layout'] ?? '1';
$types       = $settings['search_types'] ?? array();
$placeholder = $settings['search_placeholder'] ?? '';

$search_types = array(
	'services'    => esc_html__( 'Services', 'gigneo' ),
	'jobs'        => esc_html__( 'Jobs', 'gigneo' ),
	'freelancers' => gigneo_freelancer_plural(),
	'employers'   => gigneo_employer_plural(),
);

$button_text = $settings['search_button_text'] ?? '';
$button_icon = $settings['search_button_icon']['value'] ?? '';

$search_urls = array();

if ( ! empty( $types ) ) {
	foreach ( $types as $s_type ) {
		$template_id            = gigneo_option( "_gigneo_ts_template_$s_type" );
		$search_urls[ $s_type ] = ! empty( $template_id ) ? get_permalink( $template_id ) : '';
	}
}

?>
<div class="gn-ele-search-bar-wrapper gn-search-bar-layout-<?php echo esc_attr( $layout ); ?>">
	<form class="gn-search-bar-form" data-gn-urls="<?php echo esc_attr( wp_json_encode( $search_urls ) ); ?>">
		<?php if ( '1' === $layout && ! empty( $types ) ) { ?>
			<div class="gn-search-types">
				<?php foreach ( $types as $s_key => $s_type ) { ?>
					<span class="gn-search-type gn-search-type-<?php echo esc_attr( $s_type ); ?>">
						<input id="gn-search-type-<?php echo esc_attr( $s_type ); ?>" type="radio" class="gn-search-type-input" name="type" value="<?php echo esc_attr( $s_type ); ?>"<?php checked( 0, $s_key ); ?>>
						<label for="gn-search-type-<?php echo esc_attr( $s_type ); ?>"><?php echo esc_html( $search_types[ $s_type ] ); ?></label>
					</span>
				<?php } ?>
			</div>
		<?php } ?>
		<div class="gn-search-bar">
			<label><input type="text" class="gn-search-input" name="search" placeholder="<?php echo esc_attr( $placeholder ); ?>"></label>
			<?php if ( '1' !== $layout && ! empty( $types ) ) { ?>
				<div class="gn-search-types gn-select2">
					<label class="gn-select">
						<select name="type" data-dropdown-class="gn-search-type-select-dropdown" class="gn-search-type-select" data-minimum-results-for-search="Infinity">
							<?php foreach ( $types as $s_key => $s_type ) { ?>
								<option value="<?php echo esc_attr( $s_type ); ?>"<?php selected( 0, $s_key ); ?>>
									<?php echo esc_html( ucfirst( $s_type ) ); ?>
								</option>
							<?php } ?>
						</select>
					</label>
				</div>
				<?php
			}

			if ( ! empty( $button_icon ) || ! empty( $button_text ) ) {
				?>
				<button type="submit" class="gn-search-button">
					<?php
					Icons_Manager::render_icon( $settings['search_button_icon'], array( 'aria-hidden' => 'true' ) );
					echo esc_html( $button_text );
					?>
				</button>
				<?php
			}
			?>
		</div>
	</form>
</div>