<?php
/* Template Name: Attributes Filter shortcode */

use Prodigy\Includes\Frontend\Mappers\Prodigy_Filter_Data_Mapper;
use Prodigy\includes\helpers\Prodigy_Elementor_Layouts;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\Includes\Prodigy;

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

<?php if ( isset( $attr_shortcode['idWidget'] ) ) : ?>
	<input type="hidden" class="filter-widget-id" value="<?php echo esc_attr( $attr_shortcode['idWidget'] ); ?>">
<?php endif; ?>
<div class="prodigy-filter-by-title-js">
	<?php if ( ( Prodigy_Elementor_Layouts::is_elementor_template() || Prodigy_Elementor_Layouts::is_elementor_live_preview() ) ) : ?>
		<p class="prodigy-filter__title"><?php echo esc_html( $heading_text ); ?></p>
	<?php endif; ?>
</div>

<div class="prodigy-filter-js prodigy-filter__list-filter prodigy-filter__list">
	<?php foreach ( $filters ?? array() as $filter ) : ?>
		<?php if ( ! empty( $filter['values'] ) ) : ?>
			<div class="prodigy-filter__item">
				<p class="prodigy-filter__subtitle">
					<span><?php echo esc_html( ucwords( $filter['name'] ) ); ?></span>
				</p>

				<?php
				$index = 1;
				?>
				<ul class="prodigy-filter__item-list filter__item-list-js"
					id="filter_attr_<?php echo esc_attr( $filter['id'] ); ?>"
					data-count-show="<?php echo esc_attr( $count_show_attributes_value ); ?>">
					<?php
					foreach ( $filter['values'] as $attribute_values ) {
						if ( count( $filter['values'] ) < Prodigy_Filter_Data_Mapper::SHOW_ATTRIBUTE_VALUE_BY_COUNT_PRODUCTS
						) {
							continue;
						}

						if ( isset( $current_object->taxonomy ) && ! empty( $current_object->taxonomy === Prodigy::get_prodigy_category_type() )
							&& count( $filter['values'] ) < Prodigy_Filter_Data_Mapper::SHOW_ATTRIBUTE_VALUE_BY_COUNT_PRODUCTS
						) {
							continue;
						}

						$list_args = $filters_output[ $attribute_values['name'] ];

						if ( empty( $list_args ) ) {
							$list_args = build_query( array( $filter['id'] => $attribute_values['name'] ) );
						}


						$filter_url = $list_args;

						$active_value = false;
						if ( isset( $attribute_values['slug'] ) && ! empty( $get_request['attr'] ) && array_key_exists( $filter['id'], $get_request['attr'] ) ) {
							$pieces       = explode( ';', $get_request['attr'][ $filter['id'] ] );
							$active_value = in_array( $attribute_values['slug'], $pieces, true );
						}
						?>

						<?php if ( ! empty( $count_show_attributes_value ) ) : ?>
							<li class="prodigy-filter__item-list-li" style="display:<?php echo $index > $count_show_attributes_value ? 'none' : ''; ?>" >
						<?php else : ?>
							<li class="prodigy-filter__item-list-li">
						<?php endif; ?>

						<span class="category-filter-js prodigy-filter__item-list-category-filter prodigy-filter__item-list-txt flex-1
							<?php
							if ( $active_value ) {
								echo 'filter__item-list-txt--active';
							}
							?>
						">
							<?php $url = ! empty( $attribute_values['count'] ) ? urldecode( $filter_url ) : ''; ?>
						<input type="checkbox"
								class="attribute-filter-js"
								aria-label="<?php echo esc_attr( $attribute_values['slug'] ?? $attribute_values['name'] ); ?>"
								data-attribute-id="<?php echo esc_attr( $filter['id'] ); ?>"
								data-attribute-value="<?php echo esc_attr( $attribute_values['slug'] ?? $attribute_values['name'] ); ?>"
								data-filter="<?php echo ! empty( $attribute_values['count'] ) ? esc_attr( urldecode( $filter_url ) ) : ''; ?>"
							<?php echo $active_value ? 'checked' : ''; ?>>
						<span class="attribute-filter-js prodigy-filter__item-list-name">
							<?php echo esc_html( $attribute_values['name'] ); ?>
						</span>
							<?php if ( $args['idWidget'] ) : ?>
								<?php if ( Prodigy_Widget_Options::$bool_mapper[ $attr_shortcode['attrs_content_prod_count'] ] ) : ?>
									<span class="prodigy-filter__item-list-info">
									(<?php echo esc_html( $attribute_values['count'] ); ?>)
								</span>
								<?php endif; ?>
							<?php else : ?>
								<span class="prodigy-filter__item-list-info">
									(<?php echo esc_html( $attribute_values['count'] ); ?>)
								</span>
							<?php endif; ?>
					</span>
						</li>
						<?php
						++$index;
					}

					$attributes_value_count = 0;
					if ( $active_filters ) {
						$attributes_value_count = count( $filter['values'] );
					}
					if ( $attributes_value_count > $count_show_attributes_value ) :
						?>
						<?php if ( ! empty( $count_show_attributes_value ) ) : ?>
							<?php $hidden_values = count( $filter['values'] ) - $count_show_attributes_value; ?>
						<button class="prodigy-filter__btn prodigy-main-button__filter-btn filter__btn-js"
								data-id="<?php echo esc_attr( $filter['id'] ); ?>"
								id="btn_txt_more_<?php echo esc_attr( $filter['id'] ); ?>">
							<span class="prodigy-cursor-pointer filter__btn-txt-js"><?php esc_attr_e( 'Show more', 'prodigy-commerce' ); ?></span>
							<span class="filter__btn-hidden-count"><?php echo '(+' . esc_html( $hidden_values . ')' ); ?></span>
						</button>
					<?php endif; ?>
					<?php endif; ?>
				</ul>
			</div>
			<div class="prodigy-divider-block prodigy-divider-block--light w-100"></div>
		<?php endif; ?>
	<?php endforeach; ?>
</div>
