<?php
/**
 * Search form template
 *
 * @package shopbar
 */

$shopbar_search_context  = isset( $args['context'] ) ? sanitize_key( $args['context'] ) : '';
$shopbar_current_post_type = isset( $_GET['post_type'] ) ? sanitize_key( $_GET['post_type'] ) : '';

?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
	<div class="search-field-wrapper">
		<svg class="search-field-icon" width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
			<circle cx="11" cy="11" r="8" stroke="currentColor" stroke-width="1.5" fill="none"/>
			<path d="m21 21-4.35-4.35" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
		</svg>
		<label for="s" class="screen-reader-text"><?php esc_html_e( 'Search for:', 'shopbar' ); ?></label>
		<input type="search" id="s" class="search-field" placeholder="<?php echo 'header' === $shopbar_search_context ? esc_attr_e( 'Search for products, brands and more...', 'shopbar' ) : esc_attr_e( 'What are you looking for?', 'shopbar' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" autocomplete="off" />
		<?php if ( 'header' === $shopbar_search_context && shopbar_is_active_woocommerce() ) : ?>
			<?php
			$shopbar_top_cats = get_terms( array(
				'taxonomy'   => 'product_cat',
				'hide_empty' => true,
				'number'     => 10,
				'parent'     => 0,
			) );
			if ( ! is_wp_error( $shopbar_top_cats ) && ! empty( $shopbar_top_cats ) ) :
			?>
			<select name="product_cat" class="sb-search-cat" aria-label="<?php esc_attr_e( 'Filter by category', 'shopbar' ); ?>">
				<option value=""><?php esc_html_e( 'All Categories', 'shopbar' ); ?></option>
				<?php foreach ( $shopbar_top_cats as $shopbar_cat ) : ?>
					<option value="<?php echo esc_attr( $shopbar_cat->slug ); ?>" <?php selected( $shopbar_cat->slug, get_query_var( 'product_cat' ) ); ?>><?php echo esc_html( $shopbar_cat->name ); ?></option>
				<?php endforeach; ?>
			</select>
			<?php endif; ?>
			<input type="hidden" name="post_type" value="product" />
		<?php elseif ( 'blog' === $shopbar_search_context ) : ?>
			<input type="hidden" name="post_type" value="post" />
		<?php elseif ( shopbar_is_active_woocommerce() ) : ?>
			<input type="hidden" name="post_type" value="product" />
		<?php elseif ( $shopbar_current_post_type ) : ?>
			<input type="hidden" name="post_type" value="<?php echo esc_attr( $shopbar_current_post_type ); ?>" />
		<?php endif; ?>
	</div>
	<button type="submit" class="search-submit" aria-label="<?php esc_attr_e( 'Search', 'shopbar' ); ?>">
		<svg width="18" height="18" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
			<path d="M5 12h14M12 5l7 7-7 7" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
		</svg>
	</button>
</form>
