<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$search_val     = ! empty( $args['search'] ) ? $args['search'] : '';
$location       = ! empty( $args['location'] ) ? $args['location'] : '';
$category       = ! empty( $args['category'] ) ? $args['category'] : '';
$sub_category   = ! empty( $args['subcategory'] ) ? $args['subcategory'] : '';
$specific_types = ! empty( $args['specific_types'] ) ? $args['specific_types'] : array();
$skills         = ! empty( $args['skills'] ) ? $args['skills'] : array();
$types          = ! empty( $args['type'] ) ? $args['type'] : ( ! empty( $args['types'] ) ? $args['types'] : array() );
$delivery       = ! empty( $args['delivery'] ) ? $args['delivery'] : array();
$featured       = ! empty( $args['featured'] ) ? $args['featured'] : false;
$verified       = ! empty( $args['verified'] ) ? $args['verified'] : false;
$min_price      = ! empty( $args['min_price'] ) ? $args['min_price'] : '';
$max_price      = ! empty( $args['max_price'] ) ? $args['max_price'] : '';
$map_address    = ! empty( $args['map_address'] ) ? $args['map_address'] : '';
$map_location   = ! empty( $args['map_location'] ) ? $args['map_location'] : '';
$map_radius     = ! empty( $args['map_radius'] ) ? $args['map_radius'] : '';
$sort           = ! empty( $args['sort'] ) ? $args['sort'] : 'recommended';
$rating         = ! empty( $args['rating'] ) ? $args['rating'] : '';
$hide_map       = ! empty( $args['hide_map'] ) ? $args['hide_map'] : false;

$is_map_enabled      = apply_filters( 'gigneo_is_map_enabled', false );
$disabled_cities     = gigneo_option( '_gigneo_ts_disable_cities', false );
$service_type_option = gigneo_option( '_gigneo_ts_service_type', 'both' );

$category_list = gigneo_term_list(
	array(
		'taxonomy' => 'gigneo_facet',
		'parent'   => 0,
	),
	false
);
$skill_list    = gigneo_term_list( array( 'taxonomy' => 'gigneo_skill' ), false );
?>
<div class="gn-archive-filters-wrapper gn-achive-filter-layout-1 gn-service-archive-filter">
	<form id="gn-archive-filters-form" method="GET" action="">
		<div class="gn-archive-search-wrapper">
			<fieldset class="gn-form-fields">
				<!--Keyword Search-->
				<div class="gn-form-field gn-keyword-search">
					<label for="gn-keyword-search"><?php echo esc_html__( 'Search by keywords', 'gigneo' ); ?></label>
					<div class="gn-search">
						<input type="search" class="gn-search-input" name="search"
							value="<?php echo esc_attr( $search_val ); ?>" id="gn-keyword-search" autocomplete="off"
							placeholder="<?php echo esc_html__( 'Search by keywords', 'gigneo' ); ?>" />
						<button type="submit" class="gn-search-icon">
							<i class="hi hi-magnifying-glass" aria-hidden="true"></i>
						</button>
					</div>
				</div>
				<!--Location-->
				<div class="gn-form-field <?php echo esc_attr( $is_map_enabled && ! $hide_map ? 'gn-map-location' : 'gn-location gn-select2' ); ?>">
					<?php if ( $is_map_enabled && ! $hide_map ) { ?>
						<label for="gn-map-address" class="gn-required-field-label">
							<?php echo esc_html__( 'Map location', 'gigneo' ); ?>
						</label>
						<div class="gn-map-address-wrapper">
							<input id="gn-map-address" type="text" name="address" autocomplete="off"
									placeholder="<?php echo esc_attr__( 'Enter map location', 'gigneo' ); ?>" value="<?php echo esc_attr( $map_address ); ?>">
							<input id="gn-map-location" type="hidden" name="map_location" value="<?php echo esc_attr( $map_location ); ?>">
							<button type="button" class="gn-current-location"
									title="<?php echo esc_html__( 'Current location', 'gigneo' ); ?>">
								<i class="hi hi-viewfinder-circle" aria-hidden="true"></i>
							</button>
						</div>
						<?php
					} else {
						$locations            = array();
						$allowed_countries    = gigneo_allowed_countries();
						$location_placeholder = $disabled_cities ? esc_html__( 'Select country or state', 'gigneo' ) : esc_html__( 'Select country, state or city', 'gigneo' );

						foreach ( $allowed_countries as $country_code => $country_name ) {
							$states = WC()->countries->get_states( $country_code );

							if ( empty( $states ) ) {
								$locations[] = array(
									'type'  => 'country',
									'value' => $country_code,
									'label' => $country_name,
								);
								continue;
							}

							$locations[] = array(
								'type'  => 'country',
								'value' => $country_code,
								'label' => $country_name,
							);

							foreach ( $states as $state_code => $state_name ) {
								$state_label = $country_name . ', ' . $state_name;
								$state_value = $country_code . ':' . $state_code;

								$locations[] = array(
									'type'  => 'state',
									'value' => $state_value,
									'label' => $state_label,
								);

								if ( ! $disabled_cities ) {
									$cities = gigneo_country_state_cities( $state_value, false );

									foreach ( $cities as $city_slug => $city_name ) {
										$locations[] = array(
											'type'  => 'city',
											'value' => $city_slug,
											'label' => $state_label . ', ' . $city_name,
										);
									}
								}
							}
						}
						?>
						<label for="gn-location">
							<?php esc_html_e( 'Location', 'gigneo' ); ?>
						</label>
						<span class="gn-select">
							<select id="gn-location" name="location" data-dropdown-class="gn-service-archive-search-location"
									placeholder="<?php echo esc_attr( $location_placeholder ); ?>">
								<option value="">
									<?php echo esc_html( $location_placeholder ); ?>
								</option>
								<?php foreach ( $locations as $item ) { ?>
									<option value="<?php echo esc_attr( $item['value'] ); ?>" <?php selected( $location, $item['value'] ); ?>>
										<?php echo esc_html( $item['label'] ); ?>
									</option>
								<?php } ?>
							</select>
						</span>
					<?php } ?>
				</div>
				<!--Submit-->
				<button class="gn-btn gn-btn-primary" type="submit">
					<i class="hi hi-magnifying-glass" aria-hidden="true"></i>
					<?php echo esc_html__( 'Search', 'gigneo' ); ?>
				</button>
			</fieldset>
			<?php if ( ! empty( $search_val ) ) { ?>
				<h6 class="gn-showing-search-result">
					<?php
					printf(
						// translators: %s is search query
						esc_html__( 'Showing result for %s', 'gigneo' ),
						'<span class="gn-search-query">"' . esc_html( $search_val ) . '"</span>'
					);
					?>
				</h6>
			<?php } ?>
		</div>
		<div class="gn-archive-filter-wrapper">
			<button id="gn-responsive-filter-toggle" type="button" class="gn-btn gn-btn-outline-grey">
				<?php echo esc_html__( 'Filters', 'gigneo' ); ?>
				<i class="hi hi-adjustments-horizontal" aria-hidden="true"></i>
			</button>
			<div class="gn-archive-filters">
			<div class="gn-head">
				<h5 class="gn-title"><?php echo esc_html__( 'Filters', 'gigneo' ); ?></h5>
				<button id="gn-responsive-filter-close" type="button" class="gn-btn gn-btn-outline-grey">
					<i class="hi hi-x-mark" aria-hidden="true"></i>
				</button>
			</div>
			<div class="gn-body">
				<!--Categories-->
				<?php if ( ! empty( $category_list ) ) { ?>
				<div class="gn-filter-dropdown-container gn-categories-filter-dropdown">
					<button class="gn-btn gn-btn-outline-grey gn-filter-dropdown-toggle" type="button">
						<?php echo esc_html__( 'Category', 'gigneo' ); ?>
						<i class="hi hi-chevron-down" aria-hidden="true"></i>
					</button>
					<div class="gn-filter-dropdown">
						<div class="gn-body">
							<fieldset class="gn-form-fields gn-category-fields-wrapper">
								<div class="gn-form-field gn-category gn-select2">
									<label for="gn-category"><?php echo esc_html__( 'Category', 'gigneo' ); ?></label>
									<select id="gn-category" name="category" data-dropdown-class="gn-service-archive-search-category"
										placeholder="<?php echo esc_html__( 'Select category', 'gigneo' ); ?>">
										<option value=""><?php echo esc_html__( 'Select category', 'gigneo' ); ?>
										</option>
										<?php
										if ( ! empty( $category_list ) && is_array( $category_list ) ) {
											foreach ( $category_list as $cat_slug => $cat_name ) {
												?>
												<option value="<?php echo esc_attr( $cat_slug ); ?>" <?php selected( $category, $cat_slug ); ?>><?php echo esc_html( $cat_name ); ?></option>
												<?php
											}
										}
										?>
									</select>
								</div>
								<div class="gn-form-field gn-subcategory gn-select2">
									<?php
									$sub_categories = array();
									foreach ( $category_list as $cat_slug => $cat_name ) {
										$cat_term = get_term_by( 'slug', $cat_slug, 'gigneo_facet' );
										if ( $cat_term && ! is_wp_error( $cat_term ) ) {
											$sub_category_list           = gigneo_term_list(
												array(
													'taxonomy' => 'gigneo_facet',
													'parent' => $cat_term->term_id,
												),
												false
											);
											$sub_categories[ $cat_slug ] = $sub_category_list;
											foreach ( $sub_category_list as $sub_cat_slug => $sub_cat ) {
												$sub_cat_term = get_term_by( 'slug', $sub_cat_slug, 'gigneo_facet' );
												if ( $sub_cat_term && ! is_wp_error( $sub_cat_term ) ) {
													$sub_categories[ $sub_cat_slug ] = gigneo_term_list(
														array(
															'taxonomy' => 'gigneo_facet',
															'parent' => $sub_cat_term->term_id,
														),
														false
													);
												}
											}
										}
									}

									$parent_category = get_term_by( 'slug', $category, 'gigneo_facet' );
									$child_category  = get_term_by( 'slug', $sub_category, 'gigneo_facet' );

									$sub_category_list = array();
									if ( ! empty( $parent_category->term_id ) ) {
										$sub_category_list = gigneo_term_list(
											array(
												'taxonomy' => 'gigneo_facet',
												'parent'   => $parent_category->term_id,
											),
											false
										);
									}

									$specific_type_list = array();
									if ( ! empty( $child_category->term_id ) ) {
										$specific_type_list = gigneo_term_list(
											array(
												'taxonomy' => 'gigneo_facet',
												'parent'   => $child_category->term_id,
											),
											false
										);
									}
									?>
									<label
										for="gn-subcategory"><?php echo esc_html__( 'Subcategory', 'gigneo' ); ?></label>
									<select id="gn-subcategory" name="subcategory" data-dropdown-class="gn-service-archive-search-subcategory"
										data-gn-categories="<?php echo esc_attr( wp_json_encode( $sub_categories ) ); ?>"
										placeholder="<?php echo esc_html__( 'Select sub category', 'gigneo' ); ?>">
										<?php
										if ( ! empty( $sub_category_list ) && is_array( $sub_category_list ) ) {
											foreach ( $sub_category_list as $sub_cat_slug => $sub_cat_name ) {
												?>
												<option value="<?php echo esc_attr( $sub_cat_slug ); ?>" <?php selected( $sub_category, $sub_cat_slug ); ?>>
													<?php echo esc_html( $sub_cat_name ); ?>
												</option>
												<?php
											}
										}
										?>
									</select>
								</div>
								<div
									class="gn-form-field gn-specific-types-field<?php echo esc_attr( empty( $specific_type_list ) || ! is_array( $specific_type_list ) ? ' gn-hide' : '' ); ?>">
									<h6 class="gn-field-label"><?php echo esc_html__( 'Specific types', 'gigneo' ); ?></h6>
									<div class="gn-specific-types">
										<?php
										if ( ! empty( $specific_type_list ) && is_array( $specific_type_list ) ) {
											foreach ( $specific_type_list as $specific_type_slug => $specific_type_name ) {
												?>
												<div class="gn-checkbox"><input type="checkbox"
														value="<?php echo esc_attr( $specific_type_slug ); ?>" <?php echo esc_attr( in_array( $specific_type_slug, $specific_types, true ) ? ' checked' : '' ); ?> name="specific_types[]"
														id="gn-specific-type-<?php echo esc_attr( $specific_type_slug ); ?>">
													<label
														for="gn-specific-type-<?php echo esc_attr( $specific_type_slug ); ?>"><span><?php echo esc_html( $specific_type_name ); ?></span></label>
												</div>
												<?php
											}
										}
										?>
									</div>
								</div>
							</fieldset>
						</div>
						<div class="gn-footer">
							<button type="button"
								class="gn-btn gn-btn-outline-grey gn-btn-md gn-clear-filter"><?php echo esc_html__( 'Clear', 'gigneo' ); ?></button>
							<button type="submit"
								class="gn-btn gn-btn-primary gn-btn-md gn-apply-filter"><?php echo esc_html__( 'Apply', 'gigneo' ); ?></button>
						</div>
					</div>
				</div>
				<?php } ?>
				<!--Skills-->
				<?php if ( ! empty( $skill_list ) ) { ?>
				<div class="gn-filter-dropdown-container gn-skills-filter-dropdown">
					<button class="gn-btn gn-btn-outline-grey gn-filter-dropdown-toggle" type="button">
						<?php echo esc_html__( 'Skills', 'gigneo' ); ?>
						<i class="hi hi-chevron-down" aria-hidden="true"></i>
					</button>
					<div class="gn-filter-dropdown">
						<div class="gn-body">
							<fieldset class="gn-form-fields">
								<div class="gn-form-field gn-custom-scroll">
									<h6 class="gn-field-label"><?php echo esc_html__( 'Skills', 'gigneo' ); ?></h6>
									<?php
									foreach ( $skill_list as $skill_slug => $skill_name ) {
										?>
										<div class="gn-checkbox">
											<input type="checkbox" value="<?php echo esc_attr( $skill_slug ); ?>" <?php echo esc_attr( in_array( $skill_slug, $skills, true ) ? ' checked' : '' ); ?>
												name="skills[]" id="gn-skill-<?php echo esc_attr( $skill_slug ); ?>">
											<label
												for="gn-skill-<?php echo esc_attr( $skill_slug ); ?>"><span><?php echo esc_html( $skill_name ); ?></span></label>
										</div>
										<?php
									}
									?>
								</div>
							</fieldset>
						</div>
						<div class="gn-footer">
							<button type="button"
								class="gn-btn gn-btn-outline-grey gn-btn-md gn-clear-filter"><?php echo esc_html__( 'Clear', 'gigneo' ); ?></button>
							<button type="submit"
								class="gn-btn gn-btn-primary gn-btn-md gn-apply-filter"><?php echo esc_html__( 'Apply', 'gigneo' ); ?></button>
						</div>
					</div>
				</div>
				<?php } ?>
				<!--Price-->
				<div class="gn-filter-dropdown-container gn-price-filter-dropdown">
					<button class="gn-btn gn-btn-outline-grey gn-filter-dropdown-toggle" type="button">
						<?php echo esc_html__( 'Price', 'gigneo' ); ?>
						<i class="hi hi-chevron-down" aria-hidden="true"></i>
					</button>
					<div class="gn-filter-dropdown">
						<div class="gn-body">
							<fieldset class="gn-form-fields gn-nouislider">
								<div class="gn-form-field">
									<h6 class="gn-field-label"><?php echo esc_html__( 'Price range', 'gigneo' ); ?></h6>
									<div class="gn-price-range-slider" data-gn-min="0" data-gn-max="10000"></div>
									<div class="gn-range-display">
										<div class="gn-range-min">
											<input id="gn-min-price" class="gn-min-price"
												value="<?php echo esc_attr( $min_price ); ?>" name="min_price"
												type="number" min="0" max="10000" readonly>
											<label class="gn-label"
												for="gn-min-price"><?php echo esc_html__( 'Min.', 'gigneo' ); ?></label>
										</div>
										<div class="gn-range-max">
											<input id="gn-max-price" class="gn-max-price"
												value="<?php echo esc_attr( $max_price ); ?>" name="max_price"
												type="number" min="0" max="10000" readonly>
											<label class="gn-label"
												for="gn-max-price"><?php echo esc_html__( 'Max.', 'gigneo' ); ?></label>
										</div>
									</div>
								</div>
							</fieldset>
						</div>
						<div class="gn-footer">
							<button type="button"
								class="gn-btn gn-btn-outline-grey gn-btn-md gn-clear-filter"><?php echo esc_html__( 'Clear', 'gigneo' ); ?></button>
							<button type="submit"
								class="gn-btn gn-btn-primary gn-btn-md gn-apply-filter"><?php echo esc_html__( 'Apply', 'gigneo' ); ?></button>
						</div>
					</div>
				</div>
				<?php if ( 'both' === $service_type_option ) { ?>
					<!--Type-->
					<div class="gn-filter-dropdown-container gn-type-filter-dropdown">
						<button class="gn-btn gn-btn-outline-grey gn-filter-dropdown-toggle" type="button">
							<?php echo esc_html__( 'Type', 'gigneo' ); ?>
							<i class="hi hi-chevron-down" aria-hidden="true"></i>
						</button>
						<div class="gn-filter-dropdown">
							<div class="gn-body">
								<fieldset class="gn-form-fields">
									<div class="gn-form-field">
										<div class="gn-checkbox">
											<input type="checkbox" id="gn-type-remote" name="type[]" <?php echo esc_attr( in_array( 'remote', $types, true ) ? ' checked' : '' ); ?>
												value="remote">
											<label
												for="gn-type-remote"><?php echo esc_html__( 'Remote', 'gigneo' ); ?></label>
										</div>
										<div class="gn-checkbox">
											<input type="checkbox" id="gn-type-onsite" name="type[]" value="onsite" <?php echo esc_attr( in_array( 'onsite', $types, true ) ? ' checked' : '' ); ?>>
											<label
												for="gn-type-onsite"><?php echo esc_html__( 'Onsite', 'gigneo' ); ?></label>
										</div>
									</div>
								</fieldset>
							</div>
						</div>
					</div>
				<?php } ?>
				<?php if ( 'remote' !== $service_type_option ) { ?>
					<!--Delivery-->
					<div class="gn-filter-dropdown-container gn-delivery-filter-dropdown">
						<button class="gn-btn gn-btn-outline-grey gn-filter-dropdown-toggle" type="button">
							<?php echo esc_html__( 'Delivery', 'gigneo' ); ?>
							<i class="hi hi-chevron-down" aria-hidden="true"></i>
						</button>
						<div class="gn-filter-dropdown">
							<div class="gn-body">
								<fieldset class="gn-form-fields">
									<div class="gn-form-field">
										<div class="gn-checkbox">
											<input type="checkbox" id="gn-delivery-at-my-location" name="delivery[]" <?php echo esc_attr( in_array( 'on_location', $delivery, true ) ? ' checked' : '' ); ?>
												value="on_location">
											<label for="gn-delivery-at-my-location">
											<?php
											printf(
												// translators: %s is freelancer singular
												esc_html__( '%s visit me', 'gigneo' ),
												esc_html( gigneo_freelancer_singular() )
											);
											?>
											</label>
										</div>
										<div class="gn-checkbox">
											<input type="checkbox" id="gn-delivery-at-provider-location" name="delivery[]"
												<?php echo esc_attr( in_array( 'visit_me', $delivery, true ) ? ' checked' : '' ); ?> value="visit_me">
											<label for="gn-delivery-at-provider-location">
											<?php
											printf(
												// translators: %s is freelancer singular
												esc_html__( "I'll visit %s", 'gigneo' ),
												esc_html( gigneo_freelancer_singular() )
											);
											?>
											</label>
										</div>
									</div>
								</fieldset>
							</div>
						</div>
					</div>
				<?php } ?>
				<!--Rating-->
				<div class="gn-filter-dropdown-container gn-ratings-filter-dropdown">
					<button class="gn-btn gn-btn-outline-grey gn-filter-dropdown-toggle" type="button">
						<?php echo esc_html__( 'Rating', 'gigneo' ); ?>
						<i class="hi hi-chevron-down" aria-hidden="true"></i>
					</button>
					<div class="gn-filter-dropdown">
						<div class="gn-body">
							<fieldset class="gn-form-fields">
								<div class="gn-form-field">
									<?php
									foreach ( array( 5, 4, 3, 2, 1 ) as $star ) {
										?>
										<div class="gn-radio">
											<input type="radio" id="gn-rating-<?php echo esc_attr( $star ); ?>"
												name="rating" value="<?php echo esc_attr( $star ); ?>" <?php checked( (int) $rating === $star ); ?>>
											<label for="gn-rating-<?php echo esc_attr( $star ); ?>">
												<span class="gn-rating-stars">
													<?php
													for ( $i = 1; $i <= 5; $i++ ) {
														echo '<i class="hi hi-star-alt' . ( $i <= $star ? ' gn-filled' : '' ) . '" aria-hidden="true"></i>';
													}

													?>
												</span>
											</label>
										</div>
										<?php
									}
									?>
								</div>
							</fieldset>
						</div>
					</div>
				</div>
				<!--Filter Actions-->
					<div class="gn-filters-actions">
						<!--Clear Filter-->
						<button type="reset" class="gn-link gn-link-primary gn-clear-filters">
							<?php echo esc_html__( 'Clear filters', 'gigneo' ); ?>
						</button>
						<!--More Filters-->
						<div class="gn-filter-dropdown-container gn-more-filter-dropdown">
							<button class="gn-btn gn-btn-outline-grey gn-filter-dropdown-toggle" type="button">
								<?php echo esc_html__( 'More', 'gigneo' ); ?>
								<i class="hi hi-adjustments-horizontal" aria-hidden="true"></i>
							</button>
							<div class="gn-filter-dropdown">
								<div class="gn-body">
									<fieldset class="gn-form-fields">
										<div class="gn-form-field">
											<div class="gn-switch">
												<label class="gn-checkbox" for="gn-featured">
													<input type="checkbox" id="gn-featured" name="featured" <?php echo esc_attr( $featured ? ' checked' : '' ); ?> value="1">
												</label>
												<span> <?php echo esc_html__( 'Featured', 'gigneo' ); ?></span>
											</div>
											<div class="gn-switch">
												<label class="gn-checkbox" for="gn-verified">
													<input type="checkbox" id="gn-verified" name="verified" <?php echo esc_attr( $verified ? ' checked' : '' ); ?> value="1">
												</label>
												<span> <?php echo esc_html__( 'Verified', 'gigneo' ); ?></span>
											</div>
										</div>
									</fieldset>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="gn-footer">
					<!--Clear Filter-->
					<button type="reset" class="gn-link gn-link-primary gn-clear-filters">
						<?php echo esc_html__( 'Clear filters', 'gigneo' ); ?>
					</button>
				</div>
			</div>
		</div>
		<?php
		// Built active filter pills via template part
		gigneo_load_template( 'archive/service/parts/filter-pills', false, $args );
		?>
		<div class="gn-archive-sort-wrapper">
			<h6 class="gn-result-count">
				<?php
				printf(
					// translators: %s is found posts
					esc_html__( '%s Results found', 'gigneo' ),
					esc_html( $args['found_posts'] ?? 0 )
				);
				?>
			</h6>
			<div class="gn-archive-sort gn-select2">
				<label for="gn-sort"><?php echo esc_html__( 'Sort by:', 'gigneo' ); ?></label>
				<select id="gn-sort" data-dropdown-class="gn-service-archive-search-sort" name="sort" data-minimum-results-for-search="Infinity" placeholder="<?php echo esc_html__( 'Top Rated', 'gigneo' ); ?>">
					<option value="recommended" <?php selected( $sort, 'recommended' ); ?>>
						<?php echo esc_html__( 'Recommended', 'gigneo' ); ?>
					</option>
					<option value="top_rated" <?php selected( $sort, 'top_rated' ); ?>>
						<?php echo esc_html__( 'Top rated', 'gigneo' ); ?>
					</option>
					<option value="price_low_to_high" <?php selected( $sort, 'price_low_to_high' ); ?>>
						<?php echo esc_html__( 'Price low to high', 'gigneo' ); ?>
					</option>
					<option value="price_high_to_low" <?php selected( $sort, 'price_high_to_low' ); ?>>
						<?php echo esc_html__( 'Price high to low', 'gigneo' ); ?>
					</option>
				</select>
			</div>
		</div>
	</form>
</div>
