<?php

$theme_options_data = theme_options_data();

$args_category = array(
	'type'     => 'recipe',
	'taxonomy' => 'recipe_category'
);
$categories    = get_categories( $args_category );

$args_cuisine = array(
	'type'     => 'recipe',
	'taxonomy' => 'recipe_cuisine'
);
$cuisines     = get_categories( $args_cuisine );
?>
	<div class="search-recipe">
		<div class="search-recipe-content">
			<form method="get">
				<div class="row">

					<div class="col-sm-4">
						<select name="category">
							<option value=""><?php echo esc_html__( 'Recipe Category', 'cakeart' ); ?></option>
							<?php
							if ( $categories ) {
								foreach ( $categories as $category ) {
									?>
									<option value=<?php echo esc_attr( $category->slug ) ?> <?php if (isset( $_GET['category'] ) && $_GET['category'] == esc_attr( $category->slug )) { ?> selected="selected" <?php } ?> > <?php echo esc_html( $category->cat_name ) ?> </option>
								<?php
								}
							}
							?>
						</select>
					</div>

					<div class="col-sm-4">
						<select name="cuisine">
							<option value=""><?php echo esc_html__( 'Recipe Cuisine', 'cakeart' ); ?></option>
							<?php
							if ( $cuisines ) {
								foreach ( $cuisines as $cuisine ) {
									echo '<option value="' . esc_attr( $cuisine->slug ) . '"';
									if ( isset( $_GET['cuisine'] ) && $_GET['cuisine'] == $cuisine->slug ) {
										echo 'selected="selected"';
									}
									echo '>' . esc_html( $cuisine->cat_name ) . '</option>';
								}
							}
							?>
						</select>
					</div>

					<div class="col-sm-4">
						<select name="sort">
							<option value=""><?php echo esc_html__( 'Sort Recipes', 'cakeart' ); ?></option>
							<option value="title-asc" <?php if ( isset( $_GET['sort'] ) && $_GET['sort'] == 'title-asc' ) { ?> selected="selected" <?php } ?>><?php echo esc_html__( 'Title (ascending) ', 'cakeart' ); ?></option>
							<option value="title-desc" <?php if ( isset( $_GET['sort'] ) && $_GET['sort'] == 'title-desc' ) { ?> selected="selected" <?php } ?>><?php echo esc_html__( 'Title (descending) ', 'cakeart' ); ?></option>
							<option value="date-new" <?php if ( isset( $_GET['sort'] ) && $_GET['sort'] == 'date-new' ) { ?> selected="selected" <?php } ?>><?php echo esc_html__( 'Date (newest first) ', 'cakeart' ); ?></option>
							<option value="date-old" <?php if ( isset( $_GET['sort'] ) && $_GET['sort'] == 'date-old' ) { ?> selected="selected" <?php } ?>><?php echo esc_html__( 'Date (oldest first) ', 'cakeart' ); ?></option>
						</select>
					</div>

					<div class="col-sm-4">
						<input type="text" name="ingredient" value='<?php if ( isset( $_GET['ingredient'] ) && $_GET['ingredient'] != null ) {
							echo esc_html( $_GET['ingredient'] );
						} ?>' placeholder="<?php echo esc_attr__( 'Input ingredient here...', 'cakeart' ); ?>" />
					</div>

					<div class="col-sm-4">
						<input type="text" name="keyword" value='<?php if ( isset( $_GET['keyword'] ) && $_GET['keyword'] != null ) {
							echo esc_html( $_GET['keyword'] );
						} ?>' placeholder="<?php echo esc_attr__( 'Input keyword here...', 'cakeart' ); ?>" />
					</div>

					<div class="col-sm-4">
						<button class="submit" type="submit"><?php echo esc_html__( 'SEARCH FOR RECIPES', 'cakeart' ); ?></button>

					</div>

				</div>
			</form>
		</div>
	</div>
<?php

$tax_category = $tax_cuisine = '';
$order        = $orderby = '';
$ingredient   = $keyword = '';
if ( !empty( $_GET['category'] ) && isset( $_GET['category'] ) ) {
	$tax_category = array(
		'taxonomy' => 'recipe_category',
		'field'    => 'slug',
		'terms'    => $_GET['category'],
	);
}
if ( !empty( $_GET['cuisine'] ) && isset( $_GET['cuisine'] ) ) {
	$tax_cuisine = array(
		'taxonomy' => 'recipe_cuisine',
		'field'    => 'slug',
		'terms'    => $_GET['cuisine'],
	);
}
if ( !empty( $_GET['sort'] ) && isset( $_GET['sort'] ) ) {
	if ( $_GET['sort'] == 'title-asc' ) {
		$orderby = 'title';
		$order   = 'ASC';
	}
	if ( $_GET['sort'] == 'title-desc' ) {
		$orderby = 'title';
		$order   = 'DESC';
	}
	if ( $_GET['sort'] == 'date-new' ) {
		$orderby = 'date';
		$order   = 'DESC';
	}
	if ( $_GET['sort'] == 'date-old' ) {
		$orderby = 'date';
		$order   = 'ASC';
	}
}
if ( !empty( $_GET['ingredient'] ) && isset( $_GET['ingredient'] ) ) {
	$ingredient = array(
		'key'     => 'recipe_ingredients',
		'value'   => esc_attr( $_GET['ingredient'] ),
		'compare' => 'LIKE',
	);
}
if ( !empty( $_GET['keyword'] ) && isset( $_GET['keyword'] ) ) {
	$keyword = esc_attr( $_GET['keyword'] );
}

if ( isset( $theme_options_data['thim_recipe_cate_number_recipe_per_page'] ) ) {
	$number_recipe_per_page = $theme_options_data['thim_recipe_cate_number_recipe_per_page'];
} else {
	$number_recipe_per_page = 16;
}
$paged       = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$recipe_loop = array(
	'post_type'      => 'recipe',
	'paged'          => $paged,
	'posts_per_page' => $number_recipe_per_page,
	'orderby'        => $orderby,
	'order'          => $order,
	's'              => $keyword,
	'tax_query'      => array(
		'relation' => 'AND',
		$tax_category,
		$tax_cuisine
	),
	'meta_query'     => array(
		$ingredient,
	)
);

$recipe = new WP_Query( $recipe_loop );

if ( $recipe->have_posts() ) :

	if ( isset( $_GET['category'] ) || isset( $_GET['cuisine'] ) || isset( $_GET['sort'] ) || isset( $_GET['ingredient'] ) || isset( $_GET['keyword'] ) ) {
		echo do_shortcode( '[thim-heading title="' . esc_html__( 'SEARCH RESULTS', 'cakeart' ) . '" alignment="center"]' );
	} else {
		echo do_shortcode( '[thim-heading title="' . esc_html__( 'ALL RECIPES', 'cakeart' ) . '" alignment="center"]' );
		echo '<ul id="filterOptions">
		<li class="active"><a href="#" class="all">' . esc_html__( 'All', 'cakeart' ) . '</a></li>';
		if ( $categories ) {
			foreach ( $categories as $category ) {
				echo '<li><a href="#" class="' . $category->slug . '">' . esc_html( $category->cat_name ) . '</a></li>';
			}
		}
		echo '</ul>';
	}

	?>

	<div id="ourHolder" class="archive-content">
		<?php
		/* Start the Loop */

		while ( $recipe->have_posts() ) : $recipe->the_post();
			get_template_part( 'content', 'recipe' );
		endwhile;

		$pagenum_link = html_entity_decode( get_pagenum_link() );

		$query_args = array();
		$url_parts  = explode( '?', $pagenum_link );

		if ( isset( $url_parts[1] ) ) {
			wp_parse_str( $url_parts[1], $query_args );
		}

		$pagenum_link = esc_url( remove_query_arg( array_keys( $query_args ), $pagenum_link ) );
		$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';

		$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && !strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
		$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';

		// Set up paginated links.
		$links = paginate_links( array(
			'base'      => $pagenum_link,
			'format'    => $format,
			'total'     => $recipe->max_num_pages,
			'current'   => $paged,
			'mid_size'  => 2,
			'add_args'  => array_map( 'urlencode', $query_args ),
			'prev_text' => esc_html__( 'Prev', 'cakeart' ),
			'next_text' => esc_html__( 'Next', 'cakeart' ),
			'type'      => 'list'
		) );

		if ( $links ) :
			?>
			<div class="pagination loop-pagination display">
				<?php echo ent2ncr( $links ); ?>
			</div>
			<!-- .pagination -->
		<?php
		endif;
		?>
	</div>
<?php
else : ?>
	<section class="not-found-recipe">
		<div class="container">
			<div class="inner">
				<p><?php esc_html_e( 'No recipe found!', 'cakeart' ); ?></p>
			</div>
		</div>
	</section>
<?php endif;