<?php
if( get_query_var( 'paged' ) )
	$paged = get_query_var( 'paged' );
else {
	if( get_query_var( 'page' ) )
		$paged = get_query_var( 'page' );
	else
		$paged = 1;
	set_query_var( 'paged', $paged );
}

$args = array(
	'post_type' => 'deal',
	'posts_per_page' => fw_ssd_get_option('deals-per-page'),
	'paged' => $paged
);

if ( !empty($_GET['deal_search']) ) {
	$args['s'] = $_GET['deal_search'];
}

if ( !empty($_GET['sort']) ) {

	if ( $_GET['sort'] == 'rating' ) {

		$args['meta_key'] = 'rating_average';
		$args['meta_query'] = array(
			'rating_average_clause' => array(
				'key' => 'rating_average'
				),
			'stars_total_clause' => array(
				'key' => 'stars_total',
			)
		);
		$args['orderby'] = array(
			'rating_average_clause' => 'DESC',
			'stars_total_clause' => 'DESC'
		);

	}

	if ( $_GET['sort'] == 'expiring' ) {
		$args['meta_key'] = 'expiring_date';
		$args['orderby'] = 'meta_value_num';
		$args['order'] = 'asc';
	}

	if ( $_GET['sort'] == 'popular' ) {
		$args['orderby'] = 'comment_count';
		$args['order'] = 'desc';
	}

	if ( $_GET['sort'] == 'viewed' ) {
		$args['meta_key'] = 'ssd_post_views_count';
		$args['orderby'] = 'meta_value_num';
		$args['order'] = 'desc';
	}

}

$args['tax_query']['relation'] = 'AND';

if ( !empty($_GET['s_deal_category']) ) {
	$cat_slug = $_GET['s_deal_category'];

	array_push($args['tax_query'], 
		array(
			'taxonomy' => 'deal_category',
			'field' => 'slug',
			'terms' => $cat_slug
			) 
	);
}

if ( !empty($_GET['s_deal_company']) ) {
	$company_slug = $_GET['s_deal_company'];

	array_push($args['tax_query'], 
		array(
			'taxonomy' => 'deal_company',
			'field' => 'slug',
			'terms' => $company_slug
			) 
	);

	// $args['tax_query'] = array(
	// 	array(
	// 		'taxonomy' => 'deal_company',
	// 		'field' => 'slug',
	// 		'terms' => $company_slug

	// 	)
	// );
} else {

	$search_keyword = isset($args['s']) ? $args['s'] : '';

	array_push($args['tax_query'], 
		array(
			'taxonomy' => 'deal_company',
			'field' => 'name',
			'terms' => $search_keyword,
			'operator'    => 'LIKE'
			) 
	);
}

if ( !empty($_GET['days_start_range']) && !empty($_GET['days_end_range']) ) {

	$min = (int)$_GET['days_start_range'];
	$max = (int)$_GET['days_end_range'];

	$days_start = new DateTime('now + ' . $min . ' days');
	$days_start = $days_start->format('Ymd');
	$days_end = new DateTime('now + ' . $max . ' days');
	$days_end = $days_end->format('Ymd');

	if ( version_compare(phpversion(), '5.3') < 0 ) : ?>
		<div class="alert"><?php esc_html_e("It seems like your hosting provider isn't supporting PHP version at least 5.3. Please contact him or choose another provider as the date functions will not work correctly on your current configuration.", 'couponhut') ?></div>
	<?php
	endif;

	if ( !isset( $args['meta_query'] ) ) {
		$args['meta_query'] = array();
	}

	array_push(
		$args['meta_query'],
		array(
			'relation' => 'or',
			array(
				'key' => 'expiring_date',
				'value' => array( $days_start, $days_end ),
				'compare' => 'BETWEEN',
				'type' => 'UNSIGNED'
			),
			array(
				'key' => 'expiring_date',
				'value' => ''
			)
		)
	);

}

// Query only not expired deals

if ( !isset( $args['meta_query'] ) ) {
	$args['meta_query'] = array();
}

array_push(
	$args['meta_query'], 
	array(
		array(
			'relation' => 'OR',
			array(
				'key' => 'expiring_date',
				'value' => date('Ymd'),
				'compare' => '>',
				'type' => 'UNSIGNED'
			),
			array(
				'key' => 'expiring_date',
				'value' => ''
			)
		)
	)
);

// Hide deals with enabled "Registered Members Only" when not registered
if ( fw_ssd_woocommerce() && !is_user_logged_in() && !fw_ssd_get_option('member-only-show-switch') ) {
	$registered_array = array(
		'relation' => 'OR',
		array(
			'key' => 'registered_members_only',
			'value' => false,
			'compare' => '=',
			'type' => 'UNSIGNED'
		),
		array(
			'key' => 'registered_members_only',
			'compare' => 'NOT EXISTS'
			)
	);
	$args['meta_query']['relation'] = 'AND';
	array_push($args['meta_query'], $registered_array);
}

// Geolocation Filter
if ( !empty($_GET['deal_country']) ) {

	$country_slug = sanitize_title($_GET['deal_country']);

	$country_array = array(
		'key' => 'geo_country_slug',
		'value' => $country_slug,
		'compare' => '='
	);
	$args['meta_query']['relation'] = 'AND';
	array_push($args['meta_query'], $country_array);
}


if ( !empty($_GET['deal_city']) ) {

	$city_slug = sanitize_title($_GET['deal_city']);

	$city_array = array(
		'key' => 'geo_city_slug',
		'value' => $city_slug,
		'compare' => '='
	);
	$args['meta_query']['relation'] = 'AND';
	array_push($args['meta_query'], $city_array);
}

$deals_query = new WP_Query($args); // The Query
?>

<?php 
	/**
	*  Posts Loop
	*/
	if ($deals_query->have_posts()) : ?>

	<div class="grid-wrapper is-ajax-deals-content">
		
		<?php while  ( $deals_query->have_posts() ) : $deals_query->the_post(); ?>

			<div class="deal-item-wrapper col-xs-6 col-sm-6 col-md-4">
				<?php get_template_part('loop/content', 'deal'); ?>
			</div>
				
		<?php
		endwhile;
		?>

	</div><!-- end grid-wrapper -->

	<?php
	// Pagination
	$ajax = fw_ssd_get_option('ajax-switch');
	fw_ssd_paging_nav($deals_query, $ajax);

	wp_reset_postdata();

	else :
	?>
	<div class="grid-wrapper is-ajax-deals-content"></div>
	<div class="ajax-deals-notice">
		<h3><?php esc_html_e('No deals found.', 'couponhut'); ?></h3>
	</div>
<?php
endif; //have_posts()
?>