<?php
global $wp;
$selected_type = isset( $_GET['sg_archive_type'] ) ? \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['sg_archive_type'] ) : 'running';
?>
	<?php
	if ( empty( $hide_form ) || !$hide_form ) :
	?>
	<form class="sg-archive-form" method="GET">
		<?php
			if ( ! empty( $hide_categories ) && $hide_categories ) :
		?>
		<div class="sg-archive-category">
			<label for="sg_category"><?php esc_html_e( 'Category', 'sg-archive' ); ?></label>
			<?php
			wp_dropdown_categories(
				array(
					'name'              => 'sg_category',
					'taxonomy'          => 'giveaway_category',
					'selected'          => isset( $_GET['sg_category'] ) ? absint( $_GET['sg_category'] ) : 0,
					'show_option_none'  => __( 'None', 'sg-archive' ),
					'option_none_value' => 0,
				)
			);
			?>
		</div>
		<?php endif; ?>

		<?php
		if ( ! empty( $hide_statuses ) && $hide_statuses ) :
		?>
		<div class="sg-archive-type">
			<label for="sg_archive_type"><?php esc_html_e( 'Status', 'sg-archive' ); ?></label>
			<select name="sg_archive_type" id="sg_archive_type">
				<option <?php selected( $selected_type, 'running', true ); ?> value="running">Running</option>
				<option <?php selected( $selected_type, 'ended', true ); ?> value="ended">Ended</option>
			</select>
		</div>
		<?php endif; ?>

		<button type="submit"><?php esc_html_e( 'Filter', 'giveasap' ); ?></button>
	</form>
	<?php endif; ?>
	<div class="sg-archive-giveaways">
		<?php
		if ( $giveaways ) {
			foreach ( $giveaways as $giveaway ) {
				?>
				<div class="sg-archive-giveaway-col">
					<?php
					echo giveasap_shortcode(
						array(
							'id'                  => $giveaway->ID,
							'show_entries'        => '1',
							'show_total_entries'  => '1',
							'countdown_in_header' => '1',
							'hide_winners_number' => '1',
							'hide_prize_value'    => '1',
							'hide_prize_title'    => '1',
							'rules'               => ! empty( $rules ) ? $rules : '1',
						)
					);
					?>
				</div>
				<?php
			}
		} else {
			echo '<div class="sg-archive-giveaways-notice>' . esc_html_e( 'No Giveaways found.', 'giveasap' ) . '</div>';
		}
		?>
	</div>
<?php
if ( $total_pages > 1 ) {
	$query        = \Simple_Giveaways\Helpers::unslash_and_clean( $_GET );
	$current_page = isset( $_GET['sg_page'] ) ? absint( \Simple_Giveaways\Helpers::unslash_and_clean( $_GET['sg_page'] ) ) : 0;
	unset( $query['sg_page'] );
	$url = add_query_arg( $query, home_url( $wp->request ) );
	?>
	<ul class="sg-archive-giveaways-pages">
		<?php
		for ( $p = 1; $p <= $total_pages; $p++ ) {
			$page_url = add_query_arg( 'sg_page', $p, $url );
			?>
			<li>
				<?php
				if ( absint( $current_page ) === $p ) {
					?>
					<span><?php echo esc_html( $p ); ?></span>
					<?php
				} else {
					?>
					<a href="<?php echo esc_url( $page_url ); ?>"><?php echo esc_html( $p ); ?></a>
					<?php
				}
				?>
			</li>
			<?php
		}
		?>
	</ul>
	<?php
}
?>
