<?php

/**
 * Layout: Horizontal.
 *
 * @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'] ) || isset( $_GET['topic'] ) || isset( $_GET['season'] ) || isset( $_GET['sort'] ) ) {
	$is_form_submitted = 1;
}

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

$has_search_button = ( 'search' === $form_mode ) ? 1 : 0;

$has_reset_button = (int) $attributes['filters_reset'];
if ( $has_reset_button && 'ajax' !== $form_mode ) {
	$has_reset_button = $is_form_submitted;
}

$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-horizontal 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', 'topic', 'season', 'sort' ) ); ?>
		<?php if ( ! empty( $attributes['filters_keyword'] ) ) : ?>
			<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'] ) ) ) : ''; ?>" />
			</div>
		<?php endif; ?>
		
		<?php if ( ! empty( $attributes['filters_tags'] ) ) : ?>  
			<div class="aiopod-filters__tags aiopod-form-control">
				<input type="hidden" name="topic[]" value="" />
				<?php
				$tags_selected = array();

				if ( isset( $_GET['topic'] ) ) {
					$tags_selected = wp_unslash( (array) $_GET['topic'] );
				} elseif ( isset( $attributes['tags'] ) ) {
					$tags_selected = is_array( $attributes['tags'] ) ? $attributes['tags'] : explode( ',', $attributes['tags'] );
				}

				$tags_selected = array_filter( array_map( 'intval', $tags_selected ) );

				if ( empty( $tags_selected ) ) {
					if ( $term_slug = get_query_var( 'aiopod_tag' ) ) {        
						if ( $term = get_term_by( 'slug', sanitize_text_field( $term_slug ), 'aiopod_tags' ) ) {  
							$tags_selected = (array) $term->term_id;
						}
					}
				}

				$tags_args = array(
					'show_option_none'  => '— ' . esc_html__( 'Select Tags', 'aio-podcast-studio' ) . ' —',
					'option_none_value' => '',
					'taxonomy'          => 'aiopod_tags',
					'name' 			    => 'topic[]',
					'class'             => 'aiopod-form-field',
					'orderby'           => 'name',
					'order'             => 'asc',
					'selected'          => $tags_selected,
					'hierarchical'      => false,
					'show_count'        => false,
					'hide_empty'        => false
				);

				$tags_args = apply_filters( 'aiopod_search_form_tags_args', $tags_args );
				aiopod_dropdown_terms( $tags_args );
				?>
			</div>
		<?php endif; ?>
		
		<?php if ( ! empty( $attributes['filters_season'] ) ) : ?>  
			<div class="aiopod-filters__season aiopod-form-control">
				<?php
				$podcast_id      = (int) $attributes['podcast'];
				$season_options  = aiopod_get_seasons( $podcast_id );
				$sesson_selected = isset( $_GET['season'] ) ? (int) wp_unslash( $_GET['season'] ) : '';

				echo '<select name="season" class="aiopod-form-field">';
				echo sprintf( '<option value="">— %s —</option>', esc_html__( 'All Seasons', 'aio-podcast-studio' ) );

				foreach ( $season_options as $option ) {
					echo sprintf(
						'<option value="%s"%s>%s</option>',
						esc_attr( $option ),
						selected( $option, $sesson_selected, false ), 
						sprintf( esc_html__( 'Season %d', 'aio-podcast-studio' ), $option )
					);
				}

				echo '</select>';
				?>
			</div>
		<?php endif; ?>

		<?php if ( ! empty( $attributes['filters_sort'] ) ) : ?>  
			<div class="aiopod-filters__sort aiopod-form-control">
				<?php
				$sort_options  = aiopod_get_filters_form_sort_options();
				$sort_selected = isset( $_GET['sort'] ) ? sanitize_key( wp_unslash( $_GET['sort'] ) ) : '';

				echo '<select name="sort" class="aiopod-form-field">';
				echo sprintf( '<option value="">— %s —</option>', esc_html__( 'Sort By', 'aio-podcast-studio' ) );

				foreach ( $sort_options as $key => $value ) {
					echo sprintf(
						'<option value="%s"%s>%s</option>',
						esc_attr( $key ),
						selected( $key, $sort_selected, false ), 
						esc_html( $value )
					);
				}

				echo '</select>';
				?>
			</div>
		<?php endif; ?>

		<!-- Hook for developers to add new fields -->
        <?php do_action( 'aiopod_search_form_fields', $attributes ); ?>

		<?php if ( $has_search_button && $has_reset_button ) : ?>
			<div class="aiopod-filters__actions aiopod-button-group">
		<?php endif; ?>

		<?php if ( $has_search_button ) : ?>
			<input type="submit" class="aiopod-button aiopod-button--submit" value="<?php esc_attr_e( 'Search', 'aio-podcast-studio' ); ?>" /> 
		<?php endif; ?>
		
		<?php if ( $has_reset_button ) : ?>
			<a href="<?php echo esc_url( aiopod_get_current_page_url() ); ?>" class="aiopod-button aiopod-button--reset" <?php if ( 'ajax' === $form_mode ) : ?>hidden<?php endif; ?>>
				<?php esc_html_e( 'Reset', 'aio-podcast-studio' ); ?>
			</a>
		<?php endif; ?>

		<?php if ( $has_search_button && $has_reset_button ) : ?>
			</div>
		<?php endif; ?>
	</form> 
</div>
