<?php
global $wp;
$selected_type = isset( $_GET['sg_archive_type'] ) ? $_GET['sg_archive_type'] : 'running';
?>
	<form class="sg-archive-form" method="GET">
		<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'] ) ? $_GET['sg_category'] : 0,
				'show_option_none' => __( 'None', 'sg-archive' ),
				'option_none_value' => 0,
			));
			?>
		</div>
		<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>

		<button type="submit"><?php esc_html_e( 'Filter' ); ?></button>
	</form>
	<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',
					));
					?>
				</div>
				<?php
			}
		} else {
			echo '<div class="sg-archive-giveaways-notice>' . __( 'No Giveaways found.', 'sg-archive' ) . '</div>';
		}
		?>
	</div>
<?php
if ( $total_pages > 1 ) {
	$query = $_GET;
	$current_page = isset( $_GET['sg_page'] ) ? $_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( $page = 1; $page <= $total_pages; $page++ ) {
			$page_url = add_query_arg( 'sg_page', $page, $url );
			?>
			<li>
				<?php
				if ( absint( $current_page ) === $page ) {
					?>
					<span><?php echo $page; ?></span>
					<?php
				} else {
					?>
					<a href="<?php echo esc_url( $page_url ); ?>"><?php echo $page; ?></a>
					<?php
				} ?>
			</li>
			<?php
		}
		?>
	</ul>
	<?php
}
?>