<?php

namespace Elementor;

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

/**
 * Sidebar Filter: Attributes (standalone Elementor widget).
 *
 * Registered from storzen-elementor-widgets-init.php
 */
class Storzen_Sidebar_Filter_Attributes extends Widget_Base {

	public function get_name() { return 'storzen-sidebar-filter-attributes'; }
	public function get_title() { return esc_html__( 'Sidebar Filter: Attributes', 'storzen' ); }
	public function get_icon() { return 'eicon-tags'; }
	public function get_categories() { return array( 'storzen' ); }
	public function get_script_depends() { return array( 'storzen-shop-sidebar' ); }

	protected function register_controls() {
		$this->start_controls_section(
			'filter_section',
			array(
				'label' => esc_html__( 'Filter Settings', 'storzen' ),
			)
		);

		$this->add_control(
			'style',
			array(
				'label'   => esc_html__( 'Select Style', 'storzen' ),
				'type'    => Controls_Manager::SELECT2,
				'options' => array(
					'style-1' => esc_html__( 'Style 1', 'storzen' ),
				),
				'default' => 'style-1',
			)
		);

		$this->add_control(
			'title',
			array(
				'label'   => esc_html__( 'Title', 'storzen' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Attributes', 'storzen' ),
			)
		);

		$this->add_control(
			'attribute_taxonomy',
			array(
				'label'       => esc_html__( 'Attribute taxonomy slug', 'storzen' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'pa_color',
				'description' => esc_html__( 'Example: pa_color, pa_size', 'storzen' ),
			)
		);

		$this->add_control(
			'target_element_id',
			array(
				'label'       => esc_html__( 'Target Archive Widget ID', 'storzen' ),
				'type'        => Controls_Manager::TEXT,
				'description' => esc_html__( 'Paste the ID of the Archive Products Grid widget (right column).', 'storzen' ),
			)
		);

		$this->end_controls_section();

		storzen_elementor_sidebar_filter_widget_style_sections( $this, 'list' );
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$target   = isset( $settings['target_element_id'] ) ? sanitize_text_field( (string) $settings['target_element_id'] ) : '';
		$id       = $target !== '' ? $target : $this->get_id();
		$title    = isset( $settings['title'] ) ? $settings['title'] : esc_html__( 'Attributes', 'storzen' );

		$tax = isset( $settings['attribute_taxonomy'] ) ? sanitize_key( (string) $settings['attribute_taxonomy'] ) : '';

		$terms = array();
		if ( $tax && taxonomy_exists( $tax ) ) {
			$terms = get_terms(
				array(
					'taxonomy'   => $tax,
					'hide_empty' => true,
					'number'     => 50,
				)
			);
			if ( is_wp_error( $terms ) ) {
				$terms = array();
			}
		}
		?>
		<div class="szp-shop-sidebar" data-szp-element="<?php echo esc_attr( $id ); ?>">
			<div class="shop-sidebar">
				<div class="widget widget-menu">
					<h6 class="widget-title"><?php echo esc_html( $title ); ?></h6>
					<ul class="szp-filter-list szp-filter-list--attribute">
						<?php foreach ( $terms as $term ) : ?>
							<li>
								<button type="button"
									class="szp-filter-link"
									data-szp-element="<?php echo esc_attr( $id ); ?>"
									data-szp-filter="attribute_slugs"
									data-szp-slug="<?php echo esc_attr( $term->slug ); ?>"
									aria-pressed="false"><?php echo esc_html( $term->name ); ?>
									<span>(<?php echo esc_html( (string) $term->count ); ?>)</span>
								</button>
							</li>
						<?php endforeach; ?>
					</ul>
				</div>
			</div>
		</div>
		<?php
	}
}
