<?php
/*
Element: Product With Banner
*/
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
class PLS_Elementor_Product_Width_Banner extends PLS_Elementor_Widget_Base {
	
	public $exclude_product = [];
	
	/**
	 * Retrieve the widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
        return 'pls-products-with-banner';
    }

	/**
     * Get widget title.
     *
     * Retrieve Product With Banner widget title.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title() {
        return esc_html__( 'Products With Banner', 'modave-core' );
    }

    /**
     * Get widget icon.
     *
     * Retrieve Product With Banner widget icon.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon() {
        return 'pls-icon eicon-products';
    }
	
	/**
	 * Get widget keywords.
	 *
	 * Retrieve the list of keywords the list widget belongs to.
	 *
	 * @since 1.0.0
	 * @access public
	 * @return array Widget keywords.
	 */
	public function get_keywords() {
		return [ 'woocommerce', 'product', 'products', 'banner' ];
	}
	
	/**
     * Register Product With Banner widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since 1.0.0
     * @access protected
     */
    protected function register_controls() {
		
		$this->start_controls_section(
            'content_section',
            [
                'label' => esc_html__( 'General', 'modave-core' ),
            ]
        );
		$this->add_control(
            'title',
            [
                'label' 	=> esc_html__('Title', 'modave-core'),
                'type' 		=> Controls_Manager::TEXT,
				'default' 	=> '',
            ]
        );
		$this->add_control(
            'layout',
            [
                'label' 	=> esc_html__( 'Layout', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'banner-left'	=> esc_html__( 'Products With Banner Left', 'modave-core' ),
					'banner-right'	=> esc_html__( 'Products With Banner Right', 'modave-core' ),
				],
                'default' 		=> 'banner-left',
                'description' 	=> esc_html__( 'Select element layout.', 'modave-core' ),
            ]
        );
		$this->add_control(
			'banner_image',
			[
				'label'     => esc_html__( 'Choose image', 'modave-core' ),
				'type'      => Controls_Manager::MEDIA,
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
			]
		);
		$this->add_control(
            'product_style',
            [
                'label' 	=> esc_html__( 'Products Hover Style', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'default'			=> esc_html__( 'Default', 'modave-core' ),
					'product-style-1'	=> esc_html__( 'Products Hover Style 1', 'modave-core' ),
					'product-style-2'	=> esc_html__( 'Products Hover Style 2', 'modave-core' ),
					'product-style-3'	=> esc_html__( 'Products Hover Style 3', 'modave-core' ),
					'product-style-4'	=> esc_html__( 'Products Hover Style 4', 'modave-core' ),
					'product-style-5'	=> esc_html__( 'Products Hover Style 5', 'modave-core' ),
					'product-style-6'	=> esc_html__( 'Products Hover Style 6', 'modave-core' ),
				],
                'default' 		=> 'default',
				'description' 	=> esc_html__( 'Select product hover style.', 'modave-core' ),
            ]
        );
		$this->add_control(
			'show_view_more_button',
			[
				'label' 	=> esc_html__( 'Show View All Button', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 'yes',
			]
		);
		$this->add_control(
			'view_more_button_text',
			[
				'label' 	=> esc_html__( 'View All Button Text', 'modave-core' ),
				'type' 		=> Controls_Manager::TEXT,
				'default' 	=> 'View All',
				'condition' => [
					'show_view_more_button' => 'yes'
				],
			]
		);
		$this->add_control(
			'view_more_button_link',
			[
				'label' 	=> esc_html__( 'View More Button Link', 'modave-core' ),
				'type' 		=> Controls_Manager::URL,				
				'condition' => [
					'show_view_more_button' => 'yes'
				],
			]
		);
		$this->end_controls_section();
		
		$this->start_controls_section(
			'section_content_product_query',
			array(
				'label'     => esc_html__( 'Products Query', 'modave-core' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
            'data_source',
            [
                'label' 	=> esc_html__( 'Data source', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'recent_products'		=> esc_html__( 'Recent Products', 'modave-core' ),
					'featured_products'		=> esc_html__( 'Featured Products', 'modave-core' ),
					'sale_products'			=> esc_html__( 'On Sale Products', 'modave-core' ),
					'best_seller_products'	=> esc_html__( 'Best Seller Products', 'modave-core' ),
					'top_rated_products'	=> esc_html__( 'Top Rated Products', 'modave-core' ),
					'products'				=> esc_html__( 'List of Products', 'modave-core' ),
				],
                'default' 		=> 'recent_products',
				'description' 	=> esc_html__( 'Select data source for your product grid', 'modave-core' ),
            ]
        );
		$this->add_control(
            'product_ids',
            [
                'label'			=> esc_html__('Include Specific Products', 'modave-core'),
                'type'			=> 'pls_autocomplete',
				'search'		=> 'pls_core_elementor_search_post',
				'render'		=> 'pls_core_elementor_render_post',
				'post_type'		=> 'product',
				'multiple'		=> true,
				'label_block'	=> true,
				'condition'		=> [
					'data_source'	=> [ 'products' ],
				],
				'description' 	=> esc_html__( 'Add product by title.', 'modave-core' ),
            ]
        );
		$this->add_control(
            'categories',
            [
                'label'			=> esc_html__( 'Specific Category', 'modave-core' ),
                'type'			=> 'pls_autocomplete',
				'search'		=> 'pls_core_elementor_search_taxonomies',
				'render'		=> 'pls_core_elementor_render_taxonomies',
				'taxonomy'		=> array('product_cat'),
				'multiple'		=> true,
				'label_block'	=> true,
                'description'	=> esc_html__( 'Select specific categories.', 'modave-core' ),
				'condition'		=> [
					'data_source!'	=> 'products',
				],
            ]
        );
		$this->add_control(
            'exclude',
            [
                'label'			=> esc_html__('Exclude Products', 'modave-core'),
                'type'			=> 'pls_autocomplete',
				'search'		=> 'pls_core_elementor_search_post',
				'render'		=> 'pls_core_elementor_render_post',
				'post_type'		=> 'product',
				'multiple'		=> true,
				'label_block'	=> true,
				'description' 	=> esc_html__( 'Exclude some products which you do not want to display.', 'modave-core' ),
            ]
        );
		$this->add_control(
            'limit',
            [
                'label' 	=> esc_html__('Number Of Products', 'modave-core'),
                'type' 		=> Controls_Manager::NUMBER,
				'default' 	=> 8,
            ]
        );
		$this->add_control(
            'orderby',
            [
                'label' 	=> esc_html__( 'Order By', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'date'	=> esc_html__( 'Date', 'modave-core' ),
					'title'	=> esc_html__( 'Title', 'modave-core' ),
					'name'	=> esc_html__( 'Name(Slug)', 'modave-core' ),
					'rand'	=> esc_html__( 'Random', 'modave-core' ),
					'ID'	=> esc_html__( 'ID', 'modave-core' ),
				],
				'default' 	=> 'date',
            ]
        );
		$this->add_control(
            'order',
            [
                'label' 	=> esc_html__( 'Sort By', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'DESC'	=> esc_html__( 'Descending', 'modave-core' ),
					'ASC'	=> esc_html__( 'Ascending', 'modave-core' ),
				],
				'default' 	=> 'DESC',
            ]
        );		
		$this->end_controls_section();
		
		$this->start_controls_section(
			'section_content_slider',
			array(
				'label'     => esc_html__( 'Slider Settings', 'modave-core' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_responsive_control(
			'slides_to_show',
			[
				'label'			=> esc_html__( 'Slides to Show', 'modave-core' ),
				'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'1'		=> esc_html__( '1', 'modave-core' ),
					'2'		=> esc_html__( '2', 'modave-core' ),
					'3'		=> esc_html__( '3', 'modave-core' ),
					'4'		=> esc_html__( '4', 'modave-core' ),
					'5'		=> esc_html__( '5', 'modave-core' ),
					'6'		=> esc_html__( '6', 'modave-core' ),
				],
				'default' 			=> '3',
				'tablet_default' 	=> '2',
				'mobile_default' 	=> '2',
			]
		);
		$this->add_control(
			'slider_autoplay',
			[
				'label'     => esc_html__( 'Autoplay', 'modave-core' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 0,
			]
		);
		$this->add_control(
			'slider_pause_on_hover',
			[
				'label'     => esc_html__( 'Pause On Hover', 'modave-core' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 0,
				'condition' => [
					'slider_autoplay' => [ 'yes' ],
				],
			]
		);
		$this->add_control(
			'slider_loop',
			[
				'label'     => esc_html__( 'Loop', 'modave-core' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 0,
			]
		);
		$this->add_control(
			'slider_navigation',
			[
				'label'     => esc_html__( 'Nav', 'modave-core' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
			]
		);
		$this->add_control(
			'slider_dots',
			[
				'label'     => esc_html__( 'Dots', 'modave-core' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 0,
			]
		);
		$this->end_controls_section();
		
		$this->start_controls_section(
			'title_style_section',
			[
				'label' => esc_html__( 'Title', 'modave-core' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'title_typography',
				'label'    => esc_html__( 'Typography', 'modave-core' ),
				'selector' => '{{WRAPPER}} .section-heading h2',
			]
		);
		
		$this->add_responsive_control(
			'title_margin',
			[
				'label'			=> esc_html__( 'Margin Bottom', 'modave-core' ),
				'type'			=> Controls_Manager::SLIDER,
				'size_units' 	=> [ 'px' ],
				'range' 		=> [
					'px' 	=> [ 'min' => 10, 'max' => 100 ],
				],
				'selectors'		=> [
					'{{WRAPPER}} .section-heading' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				],
			]
		);		
		$this->end_controls_section();
	}
	
	protected function render() {
		
		$settings = $this->get_settings();
		$settings = wp_parse_args( $settings, [
			'slides_to_show_tablet' => 2,
			'slides_to_show_mobile' => 2
		]);
		extract( $settings );
		$default_atts		= $settings;
		$settings['id'] 	= pls_uniqid( 'pls-products-with-banner-' );
		$class				= array( 'pls-element', 'pls-products-with-banner', '	' );
		$class[]			= $layout;
		$this->exclude_product 	= $settings['exclude'];
		$settings['show_view_more_button'] 	= $show_view_more_button ? true : false;		
		$settings['banner_layout']			= '';
		if($layout == 'banner-right'){
			$settings['banner_layout']	= 'flex-row-reverse';
		}
		$shop_page_id 	= wc_get_page_id( 'shop' );
		$shop_page_url 	= $shop_page_id ? get_permalink( $shop_page_id ) : '#';
		$settings['view_all_btn']['url']	= 'href="'.$shop_page_url.'"';		
		$settings['banner_link'] = $shop_page_url;		
		$shortcodestr 			= pls_core_get_products_shortcode_attr( $settings );
		
		if( ! empty( $categories ) ){
			$banner_link 	= get_term_link( (int) trim( $categories[0] ), 'product_cat' );
			if(  ! is_wp_error( $banner_link ) ){
				$settings['banner_link'] 	= $banner_link;
				$settings['view_all_btn']['url'] 	= 'href="'.$banner_link.'"';
			}
			
		}
				
		$link_url = '';		
		if( !empty( $settings['view_more_button_link']['url'] ) ) {
			$link_url = pls_core_elementor_get_url_data($settings['view_more_button_link']);
			$settings['banner_link']	= $settings['view_more_button_link']['url'];
		}
		
		if(!empty($link_url['url'])){
			$settings['view_all_btn'] 	= $link_url;			
		}
		
		if( $product_style != 'default' ){
			pls_set_loop_prop( 'product-style', $product_style );
		}
		
		pls_set_loop_prop( 'is_shortcode', true );
		pls_set_loop_prop( 'type', 'products_shortcode' );
		
		pls_set_loop_prop( 'name', 'pls-slider');
		pls_set_loop_prop( 'products-element', 'products-with-banner' );
		pls_set_loop_prop( 'products_view', 'grid-view' );
		$unique_id 		= pls_uniqid( 'section-' );
		$class[]		= 'products-slider';
		pls_set_loop_prop( 'products-columns', $slides_to_show );
		pls_set_loop_prop( 'slider_navigation', $slider_navigation );
		pls_set_loop_prop( 'slider_dots', $slider_dots );
		pls_set_loop_prop( 'slides_to_show', $slides_to_show );
		pls_set_loop_prop( 'slides_to_show_tablet', $slides_to_show_tablet );
		pls_set_loop_prop( 'slides_to_show_mobile', $slides_to_show_mobile );
		pls_set_loop_prop( 'unique_id', $unique_id );
		pls_set_loop_prop( 'slider_options', pls_slider_attributes( $settings) );
		pls_set_loop_prop( 'count', 0 );
		$settings['class'] 		= implode(' ', array_filter( $class ) );
		$settings['shortcodestr'] 	= $shortcodestr;
		pls_init_woocommerce_loop_hook();
		
		if( ! empty( $this->exclude_product ) ){
			add_filter('woocommerce_shortcode_products_query', [$this, 'exclude_products_query'], 10, 3 );
		}
		pls_core_get_templates( 'elements-widgets/woo-products-with-banner', $settings );
		if( ! empty( $this->exclude_product ) ){
			remove_filter('woocommerce_shortcode_products_query', [$this, 'exclude_products_query'], 10, 3 );
		}
	}
	
	function exclude_products_query($query_args, $atts, $loop){
		
		$query_args['post__not_in'] = $this->exclude_product;
		return $query_args;
	}
}

$widgets_manager->register( new PLS_Elementor_Product_Width_Banner() );