<?php

/**
 * Sidebar Filter (AJAX) widget.
 *
 * @package storzen
 */

namespace Elementor;

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

class Storzen_Sidebar_Filter extends Widget_Base
{
	public function get_name()
	{
		return 'storzen-sidebar-filter';
	}

	public function get_title()
	{
		return esc_html__('Sidebar Filter (AJAX)', 'storzen');
	}

	public function get_icon()
	{
		return 'eicon-filter';
	}

	public function get_categories()
	{
		return array('storzen');
	}

	public function get_script_depends()
	{
		return array('jquery-ui-slider', 'storzen-shop-sidebar');
	}

	protected function register_controls()
	{
		$this->start_controls_section(
			'sidebar_section',
			array(
				'label' => esc_html__('Sidebar Filters', '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(
			'show_sidebar_search',
			array(
				'label'        => esc_html__('Sidebar Ajax Search Filter', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'sidebar_search_title',
			array(
				'label'     => esc_html__('Search title', 'storzen'),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__('Search products', 'storzen'),
				'condition' => array('show_sidebar_search' => 'yes'),
			)
		);

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

		$this->add_control(
			'show_sidebar_categories',
			array(
				'label'        => esc_html__('Sidebar Ajax Categories', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

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

		$this->add_control(
			'show_sidebar_attributes',
			array(
				'label'        => esc_html__('Sidebar Ajax Attributes', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$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'),
				'condition'   => array('show_sidebar_attributes' => 'yes'),
			)
		);

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

		$this->add_control(
			'show_sidebar_stock',
			array(
				'label'        => esc_html__('Sidebar Ajax Stock', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

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

		$this->add_control(
			'show_sidebar_ratings',
			array(
				'label'        => esc_html__('Sidebar Ajax Ratings', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_sidebar_price',
			array(
				'label'        => esc_html__('Price Filter', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'sidebar_price_title',
			array(
				'label'     => esc_html__('Price title', 'storzen'),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__('Filter by price', 'storzen'),
				'condition' => array('show_sidebar_price' => 'yes'),
			)
		);

		$this->add_control(
			'sidebar_multi_filter',
			array(
				'label'        => esc_html__('Multi-select filters', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'grid_section',
			array(
				'label' => esc_html__('Product Grid', 'storzen'),
			)
		);

		$this->add_control(
			'column',
			array(
				'label'   => esc_html__('Columns', 'storzen'),
				'type'    => Controls_Manager::SELECT2,
				'options' => array(
					'2' => esc_html__('6', 'storzen'),
					'3' => esc_html__('4', 'storzen'),
					'4' => esc_html__('3', 'storzen'),
					'6' => esc_html__('2', 'storzen'),
				),
				'default' => '4',
			)
		);

		$this->add_control(
			'ppr',
			array(
				'label'   => esc_html__('Products per page', 'storzen'),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 48,
				'default' => 12,
			)
		);

		$this->add_control(
			'show_toolbar',
			array(
				'label'        => esc_html__('Show toolbar', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_grid_list_icons',
			array(
				'label'        => esc_html__('Grid/List toggle', 'storzen'),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => array('show_toolbar' => 'yes'),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'sidebar_style',
			array(
				'label' => esc_html__('Sidebar Style', 'storzen'),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		storzen_elementor_style_options($this, 'Sidebar Widget Title', '{{WRAPPER}} .shop-sidebar .widget-title', array('style-1'));
		storzen_elementor_style_options($this, 'Search Input', '{{WRAPPER}} .shop-sidebar .szp-keyword-input', array('style-1'));
		storzen_elementor_style_options($this, 'Search Button', '{{WRAPPER}} .shop-sidebar .szp-keyword-filter-form button', array('style-1'));
		storzen_elementor_style_options($this, 'Sidebar Filter Link', '{{WRAPPER}} .shop-sidebar .szp-filter-link', array('style-1'));
		storzen_elementor_style_options($this, 'Sidebar Active Filter', '{{WRAPPER}} .shop-sidebar .szp-filter-link.is-active', array('style-1'));
		storzen_elementor_style_options($this, 'Sidebar Active Filter Background', '{{WRAPPER}} .shop-sidebar .szp-filter-link.is-active', array('style-1'), 'background-color', false, true);
		storzen_elementor_style_options($this, 'Price Filter Button', '{{WRAPPER}} .shop-sidebar .btn-price button', array('style-1'));
		storzen_elementor_style_options($this, 'Price Readout', '{{WRAPPER}} .shop-sidebar .szp-price-readout', array('style-1'));
		storzen_elementor_style_options($this, 'Price Slider', '{{WRAPPER}} .shop-sidebar .price-slider-range', array('style-1'));

		$this->end_controls_section();

		storzen_elementor_shop_sidebar_content_style_section($this);

		$this->start_controls_section(
			'toolbar_style',
			array(
				'label' => esc_html__('Toolbar Style', 'storzen'),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		storzen_elementor_style_options($this, 'Result Text', '{{WRAPPER}} .szp-result-text', array('style-1'));
		storzen_elementor_style_options($this, 'Sort Label', '{{WRAPPER}} .sort-label', array('style-1'));
		storzen_elementor_style_options($this, 'Sort Dropdown', '{{WRAPPER}} .szp-sort-select', array('style-1'));
		storzen_elementor_style_options($this, 'View Toggle Icons', '{{WRAPPER}} .szp-view-btn', array('style-1'));

		$this->end_controls_section();

		$this->start_controls_section(
			'pagination_style',
			array(
				'label' => esc_html__('Pagination Style', 'storzen'),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		storzen_elementor_style_options($this, 'Pagination Controls', '{{WRAPPER}} .szp-pagination button.szp-page-btn', array('style-1'));
		storzen_elementor_style_options($this, 'Pagination Active', '{{WRAPPER}} .szp-pagination button.number.current', array('style-1'));
		storzen_elementor_style_options($this, 'Pagination Active Background', '{{WRAPPER}} .szp-pagination button.number.current', array('style-1'), 'background-color', false, true);
		storzen_elementor_style_options($this, 'Pagination Disabled', '{{WRAPPER}} .szp-pagination span.previous', array('style-1'));

		$this->end_controls_section();
	}

	protected function get_current_page_url()
	{
		if (is_singular() && get_queried_object_id()) {
			return get_permalink(get_queried_object_id());
		}
		global $post;
		if ($post && is_a($post, 'WP_Post')) {
			return get_permalink($post);
		}
		return home_url('/');
	}

	protected function render()
	{
		if (! function_exists('WC') || ! function_exists('storzen_shop_sidebar_build_query_args')) {
			echo '<p>' . esc_html__('WooCommerce and sidebar helpers are required.', 'storzen') . '</p>';
			return;
		}

		$settings = $this->get_settings();
		$paged = is_front_page() ? (int) get_query_var('page') : (int) get_query_var('paged');
		if ($paged < 1) {
			$paged = 1;
		}

		$query_settings = storzen_shop_sidebar_sanitize_settings($settings);
		$filters = array(
			'paged'             => $paged,
			'orderby'           => $query_settings['wc_orderby'],
			'order'             => $query_settings['order'],
			'keyword'           => '',
			'product_cat_slugs' => array(),
			'attribute_slugs'   => array(),
			'stock_states'      => array(),
			'rating_values'     => array(),
		);
		$filters = storzen_shop_sidebar_filters_from_request($filters, $query_settings);

		$args                     = storzen_shop_sidebar_build_query_args($query_settings, $filters);
		$storzen_posts_query = new \WP_Query($args);

		$szp_shop_sidebar_base_url = $this->get_current_page_url();
		$szp_element_id            = $this->get_id();
		$szp_filters               = $filters;

		$szp_inline = array(
			'elementId'      => $szp_element_id,
			'settings'       => $query_settings,
			'initialFilters' => $szp_filters,
			'pageUrl'        => $szp_shop_sidebar_base_url,
		);
		wp_add_inline_script(
			'storzen-shop-sidebar',
			'window.WcommeroProShopSidebarCfg=window.WcommeroProShopSidebarCfg||{};window.WcommeroProShopSidebarCfg[' . wp_json_encode($szp_element_id) . ']=' . wp_json_encode($szp_inline) . ';',
			'after'
		);

		include storzen_get_template('sidebar-filter.php');
	}
}

plugin::instance()->widgets_manager->register(new Storzen_Sidebar_Filter());
