<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Template/class file; variables are widget-scoped, not true globals.
/**
 * Sidebar Filter widget template.
 *
 * @package storzen
 */

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

$szp_price_min = 0;
$szp_price_max = 1000;
global $wpdb;
$lookup = $wpdb->prefix . 'wc_product_meta_lookup';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$table_ok = ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $lookup ) ) === $lookup );
if ( $table_ok ) {
	$cache_key = 'storzen_price_range_' . md5( $lookup );
	$row       = wp_cache_get( $cache_key, 'storzen' );
	if ( false === $row ) {
		$row = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- No WP API for this aggregation; result is cached via wp_cache_get/set.
			"SELECT MIN(min_price) AS min_p, MAX(max_price) AS max_p FROM " . esc_sql( $lookup ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			ARRAY_A
		);
		wp_cache_set( $cache_key, $row, 'storzen', HOUR_IN_SECONDS );
	}
	if ( $row && null !== $row['min_p'] && null !== $row['max_p'] ) {
		$szp_price_min = (float) $row['min_p'];
		$szp_price_max = (float) $row['max_p'];
		if ( $szp_price_max <= $szp_price_min ) {
			$szp_price_max = $szp_price_min + 1;
		}
	}
}

$cur_min = isset( $szp_filters['min_price'] ) ? (float) $szp_filters['min_price'] : $szp_price_min;
$cur_max = isset( $szp_filters['max_price'] ) ? (float) $szp_filters['max_price'] : $szp_price_max;
$cur_min = max( $szp_price_min, min( $cur_min, $szp_price_max ) );
$cur_max = min( $szp_price_max, max( $cur_max, $szp_price_min ) );
$keyword = isset( $szp_filters['keyword'] ) ? (string) $szp_filters['keyword'] : '';

$szp_cat_terms = get_terms(
	array(
		'taxonomy'   => 'product_cat',
		'hide_empty' => true,
	)
);
if ( is_wp_error( $szp_cat_terms ) ) {
	$szp_cat_terms = array();
}

$szp_attr_tax = isset( $settings['attribute_taxonomy'] ) ? sanitize_key( (string) $settings['attribute_taxonomy'] ) : '';
$szp_attr_terms = array();
if ( $szp_attr_tax && taxonomy_exists( $szp_attr_tax ) ) {
	$szp_attr_terms = get_terms(
		array(
			'taxonomy'   => $szp_attr_tax,
			'hide_empty' => true,
		)
	);
	if ( is_wp_error( $szp_attr_terms ) ) {
		$szp_attr_terms = array();
	}
}

$szp_active_cats   = isset( $szp_filters['product_cat_slugs'] ) && is_array( $szp_filters['product_cat_slugs'] ) ? $szp_filters['product_cat_slugs'] : array();
$szp_active_attrs  = isset( $szp_filters['attribute_slugs'] ) && is_array( $szp_filters['attribute_slugs'] ) ? $szp_filters['attribute_slugs'] : array();
$szp_active_stock  = isset( $szp_filters['stock_states'] ) && is_array( $szp_filters['stock_states'] ) ? $szp_filters['stock_states'] : array();
$szp_active_rating = isset( $szp_filters['rating_values'] ) && is_array( $szp_filters['rating_values'] ) ? array_map( 'intval', $szp_filters['rating_values'] ) : array();
$szp_stock_items = array(
	'instock'     => __( 'In stock', 'storzen' ),
	'outofstock'  => __( 'Out of stock', 'storzen' ),
	'onbackorder' => __( 'On backorder', 'storzen' ),
);
?>
<section class="shop-section fix section-padding szp-shop-sidebar" data-szp-element="<?php echo esc_attr( $szp_element_id ); ?>" data-szp-multi="<?php echo 'yes' === ( $settings['sidebar_multi_filter'] ?? 'yes' ) ? '1' : '0'; ?>">
	<div class="wco-container">
		<div class="wco-row wco-g-4">
			<div class="wco-col-xl-3 wco-col-lg-4">
				<div class="main-sidebar shop-sidebar rmb-75">
					<?php if ( 'yes' === ( $settings['show_sidebar_search'] ?? 'yes' ) ) : ?>
					<div class="widget widget-search wow fadeInUp delay-0-2s">
						<h6 class="widget-title"><?php echo esc_html( $settings['sidebar_search_title'] ?? __( 'Search products', 'storzen' ) ); ?></h6>
						<form class="szp-keyword-filter-form" data-szp-element="<?php echo esc_attr( $szp_element_id ); ?>">
							<input type="text" class="szp-keyword-input" value="<?php echo esc_attr( $keyword ); ?>" placeholder="<?php esc_attr_e( 'Search products...', 'storzen' ); ?>" />
							<button type="submit"><?php esc_html_e( 'Search', 'storzen' ); ?></button>
						</form>
					</div>
					<?php endif; ?>

					<?php if ( 'yes' === ( $settings['show_sidebar_categories'] ?? 'yes' ) && ! empty( $szp_cat_terms ) ) : ?>
					<div class="widget widget-menu wow fadeInUp delay-0-2s">
						<h6 class="widget-title"><?php echo esc_html( $settings['sidebar_cat_title'] ?? __( 'Category', 'storzen' ) ); ?></h6>
						<ul class="szp-filter-list szp-filter-list--category">
							<?php foreach ( $szp_cat_terms as $term ) : ?>
								<?php $active = in_array( $term->slug, $szp_active_cats, true ); ?>
								<li><button type="button" class="szp-filter-link<?php echo $active ? ' is-active' : ''; ?>" data-szp-element="<?php echo esc_attr( $szp_element_id ); ?>" data-szp-filter="product_cat_slugs" data-szp-slug="<?php echo esc_attr( $term->slug ); ?>" aria-pressed="<?php echo $active ? 'true' : 'false'; ?>"><?php echo esc_html( $term->name ); ?><span>(<?php echo esc_html( (string) $term->count ); ?>)</span></button></li>
							<?php endforeach; ?>
						</ul>
					</div>
					<?php endif; ?>

					<?php if ( 'yes' === ( $settings['show_sidebar_attributes'] ?? 'yes' ) && ! empty( $szp_attr_terms ) ) : ?>
					<div class="widget widget-menu wow fadeInUp delay-0-2s">
						<h6 class="widget-title"><?php echo esc_html( $settings['sidebar_attr_title'] ?? __( 'Attributes', 'storzen' ) ); ?></h6>
						<ul class="szp-filter-list szp-filter-list--attribute">
							<?php foreach ( $szp_attr_terms as $term ) : ?>
								<?php $active = in_array( $term->slug, $szp_active_attrs, true ); ?>
								<li><button type="button" class="szp-filter-link<?php echo $active ? ' is-active' : ''; ?>" data-szp-element="<?php echo esc_attr( $szp_element_id ); ?>" data-szp-filter="attribute_slugs" data-szp-slug="<?php echo esc_attr( $term->slug ); ?>" aria-pressed="<?php echo $active ? 'true' : 'false'; ?>"><?php echo esc_html( $term->name ); ?><span>(<?php echo esc_html( (string) $term->count ); ?>)</span></button></li>
							<?php endforeach; ?>
						</ul>
					</div>
					<?php endif; ?>

					<?php if ( 'yes' === ( $settings['show_sidebar_stock'] ?? 'yes' ) ) : ?>
					<div class="widget widget-menu wow fadeInUp delay-0-2s">
						<h6 class="widget-title"><?php echo esc_html( $settings['sidebar_stock_title'] ?? __( 'Stock', 'storzen' ) ); ?></h6>
						<ul class="szp-filter-list szp-filter-list--stock">
							<?php foreach ( $szp_stock_items as $stock_key => $stock_label ) : ?>
								<?php $active = in_array( $stock_key, $szp_active_stock, true ); ?>
								<li><button type="button" class="szp-filter-link<?php echo $active ? ' is-active' : ''; ?>" data-szp-element="<?php echo esc_attr( $szp_element_id ); ?>" data-szp-filter="stock_states" data-szp-slug="<?php echo esc_attr( $stock_key ); ?>" aria-pressed="<?php echo $active ? 'true' : 'false'; ?>"><?php echo esc_html( $stock_label ); ?></button></li>
							<?php endforeach; ?>
						</ul>
					</div>
					<?php endif; ?>

					<?php if ( 'yes' === ( $settings['show_sidebar_ratings'] ?? 'yes' ) ) : ?>
					<div class="widget widget-menu wow fadeInUp delay-0-2s">
						<h6 class="widget-title"><?php echo esc_html( $settings['sidebar_ratings_title'] ?? __( 'Ratings', 'storzen' ) ); ?></h6>
						<ul class="szp-filter-list szp-filter-list--ratings">
						<?php for ( $i = 5; $i >= 1; $i-- ) : ?>
							<?php $active = in_array( $i, $szp_active_rating, true ); ?>
							<li><button type="button" class="szp-filter-link<?php echo $active ? ' is-active' : ''; ?>" data-szp-element="<?php echo esc_attr( $szp_element_id ); ?>" data-szp-filter="rating_values" data-szp-slug="<?php echo esc_attr( (string) $i ); ?>" aria-pressed="<?php echo $active ? 'true' : 'false'; ?>">
								<?php
								/* translators: %d: number of stars */
								echo esc_html( sprintf( __( '%d stars & up', 'storzen' ), $i ) );
								?>
							</button></li>
						<?php endfor; ?>
						</ul>
					</div>
					<?php endif; ?>

					<?php if ( 'yes' === ( $settings['show_sidebar_price'] ?? 'yes' ) ) : ?>
					<div class="widget widget-filter wow fadeInUp delay-0-2s">
						<h6 class="widget-title"><?php echo esc_html( $settings['sidebar_price_title'] ?? __( 'Filter by price', 'storzen' ) ); ?></h6>
						<form method="get" action="<?php echo esc_url( $szp_shop_sidebar_base_url ); ?>" class="szp-price-filter-form" data-szp-element="<?php echo esc_attr( $szp_element_id ); ?>">
							<div class="price-filter-wrap" data-min="<?php echo esc_attr( (string) $szp_price_min ); ?>" data-max="<?php echo esc_attr( (string) $szp_price_max ); ?>">
								<div class="price-slider-range"></div>
								<div class="btn-price">
									<button type="submit"><?php esc_html_e( 'Filter', 'storzen' ); ?></button>
									<input type="text" class="szp-price-readout" readonly="readonly" value="" />
									<input type="hidden" name="min_price" value="<?php echo esc_attr( (string) $cur_min ); ?>" />
									<input type="hidden" name="max_price" value="<?php echo esc_attr( (string) $cur_max ); ?>" />
								</div>
							</div>
						</form>
					</div>
					<?php endif; ?>
				</div>
			</div>
			<div class="wco-col-xl-9 wco-col-lg-8">
				<?php include storzen_get_template( 'shop-sidebar-main.php' ); ?>
			</div>
		</div>
	</div>
</section>
