<?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__accordion prodigy-filter-js prodigy-filter-title-js">
	<?php $iterator = 0; ?>
	<?php foreach ( $args['filters'] as $filter ) : ?>
		<?php if ( ! empty( $filter['values'] ) ) : ?>
			<?php ++$iterator; ?>
			<div class="prodigy-filter__card prodigy-filter__card-list-item mb-0">
				<div class="position-relative d-flex justify-content-between align-items-center prodigy-filter__card-header prodigy-filter__accordion-trigger"
					data-toggle="collapse"
					data-target="#collapse_<?php echo esc_attr( ucwords( $filter['slug'] ) ); ?>"
					role="button"
					tabindex="0"
					aria-expanded="<?php echo $iterator <= $args['vision_section_amount'] ? 'true' : 'false'; ?>"
					aria-controls="collapse_<?php echo esc_attr( ucwords( $filter['slug'] ) ); ?>">
					<p class="prodigy-filter__subtitle mb-0"><?php echo esc_html( ucwords( $filter['name'] ) ); ?></p>
					<button
						class="prodigy-filter__card-btn prodigy-icon-btn"
						type="button"
						aria-label="<?php esc_attr_e( 'Expand', 'prodigy-commerce' ); ?>">
						<i class="icon-arrow-down"></i>
					</button>
				</div>
				<div
						class="collapse <?php echo $iterator <= $args['vision_section_amount'] ? 'show' : ''; ?>"
						id="collapse_<?php echo esc_attr( ucwords( $filter['slug'] ) ); ?>"
				>
					<div class="prodigy-filter__card-body">
						<ul class="prodigy-filter__card-list filter__item-list-js"
							id="filter_attr_<?php echo esc_attr( $filter['id'] ); ?>"
							data-count-show="<?php echo esc_attr( $args['count_show_attributes_value'] ); ?>">
							<?php
							$count = false;
							$index = 1;
							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;
								}

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

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

									$params     = array(
										'_wpnonce' => $filter_nonce,
									);
									$filter_url = add_query_arg( $params, $list_args );
								}

								$active_value = false;
								if ( ! 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( (int) $args['count_show_attributes_value'] ) ) : ?>
								<li class="prodigy-filter__card-list-item" style="display:<?php echo $index > (int) $args['count_show_attributes_value'] ? 'none' : ''; ?>" >
							<?php else : ?>
								<li class="prodigy-filter__card-list-item">
							<?php endif; ?>
								<span class="prodigy-filter__item-list-txt d-flex flex-row justify-content-start align-items-center mb-16">
							<input
									class="attribute-filter-js"
									aria-label="<?php echo esc_attr( $attribute_values['slug'] ?? $attribute_values['name'] ); ?>"
									type="checkbox"
									data-attribute-id="<?php echo esc_attr( $filter['id'] ); ?>"
									data-attribute-value="<?php echo esc_attr( $attribute_values['slug'] ?? $attribute_values['name'] ); ?>"
									data-attribute-name="<?php echo esc_attr( $attribute_values['name'] ); ?>"
									data-filter="<?php echo ! empty( $attribute_values['count'] ) ? esc_attr( urldecode( $filter_url ) ) : ''; ?>"
									<?php echo $active_value ? 'checked' : ''; ?>
							>
								<span class="prodigy-filter__item-list-name">
									<?php echo esc_html( $attribute_values['name'] ); ?>
								</span>
								<?php if ( $args['idWidget'] && Prodigy_Widget_Options::$bool_mapper[ $args['attrs_content_prod_count'] ] ) : ?>
									<span class="prodigy-filter__item-list-info">(<?php echo esc_html( $attribute_values['count'] ); ?>)</span>
								<?php endif; ?>
								</span>
								</li>
								<?php
								++$index;
							endforeach;
							?>
							<?php
							$attributes_value_count = 0;
							if ( $active_filters ) {
								$attributes_value_count = count( $filter['values'] );
							}

							if ( $attributes_value_count > $args['count_show_attributes_value'] ) :
								?>
								<?php if ( ! empty( $args['count_show_attributes_value'] ) ) : ?>
									<?php $hidden_values = count( $filter['values'] ) - (int) $args['count_show_attributes_value']; ?>
							<?php endif; ?>
								<?php if ( ! empty( (int) $args['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_html_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>
			</div>
		<?php endif; ?>
	<?php endforeach; ?>
</div>

