<?php

/**
 * Layout: Compact.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$is_form_submitted = 0;
if ( isset( $_GET['q'] ) ) {
	$is_form_submitted = 1;
}

$form_mode = sanitize_key( $attributes['filters_mode'] );
if ( ! in_array( $form_mode, array( 'live', 'ajax', 'search' ) ) ) {
	$form_mode = 'ajax';
}

$json_params = array();
if ( 'ajax' === $form_mode ) {
	$json_params = aiopod_prepare_attributes_for_ajax( $attributes );
}

$unique_id = wp_unique_id();
?>
<div class="aiopod-filters aiopod-filters--layout-compact aiopod-filters--mode-<?php echo esc_attr( $form_mode ); ?>" data-params="<?php echo esc_attr( wp_json_encode( $json_params ) ); ?>">
	<form method="get" action="<?php echo esc_url( aiopod_get_current_page_url() ); ?>">
		<?php aiopod_render_preserved_query_fields( array( 'q' ) ); ?>
		<div class="aiopod-filters__keyword aiopod-form-control">
			<label class="aiopod-screen-reader-text" for="aiopod-filters-keyword-<?php echo esc_attr( $unique_id ); ?>"><?php esc_html_e( 'Search by keyword', 'aio-podcast-studio' ); ?></label>
			<input type="text" id="aiopod-filters-keyword-<?php echo esc_attr( $unique_id ); ?>" name="q" class="aiopod-form-field" placeholder="<?php esc_attr_e( 'Enter your Keyword', 'aio-podcast-studio' ); ?>" value="<?php echo isset( $_GET['q'] ) ? esc_attr( sanitize_text_field( wp_unslash( $_GET['q'] ) ) ) : ''; ?>" />
			<button type="submit" class="aiopod-button aiopod-button--submit" aria-label="<?php esc_attr_e( 'Search', 'aio-podcast-studio' ); ?>"<?php if ( $is_form_submitted ) : ?> hidden<?php endif; ?>>
				<svg xmlns="http://www.w3.org/2000/svg" fill="none" width="16" height="16" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
					<path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
				</svg>
			</button>
			<button type="button" class="aiopod-button aiopod-button--reset" aria-label="<?php esc_attr_e( 'Clear search', 'aio-podcast-studio' ); ?>" onclick="location.href='<?php echo esc_url( aiopod_get_current_page_url() ); ?>';"<?php if ( ! $is_form_submitted ) : ?> hidden<?php endif; ?>>
				<svg xmlns="http://www.w3.org/2000/svg" fill="none" width="16" height="16" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true">
					<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
				</svg>
			</button>
		</div>
		
		<!-- Hook for developers to add new fields -->
        <?php do_action( 'aiopod_search_form_fields', $attributes ); ?>
	</form> 
</div>
