<?php
/*
 * Element: Products Card
 */
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;

class PLS_Elementor_ProductsCard extends PLS_Elementor_Widget_Base {
	
	public $exclude_product = [];
	
	/**
	 * Retrieve the widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
        return 'pls-products-card';
    }

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

    /**
     * Get widget icon.
     *
     * Retrieve Product Card widget icon.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon() {
        return 'pls-icon eicon-post-list';
    }
	
	/**
	 * 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 widget', 'card', 'products' ];
	}
	
	/**
     * Register Products Card 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(
            'section_content_general',
            [
                'label' => esc_html__( 'General', 'modave-core' ),
            ]
        );
		$this->add_control(
            'heading',
            [
                'label' 	=> esc_html__( 'Heading', 'modave-core' ),
                'type' 		=> Controls_Manager::TEXT,
				'default' 	=> esc_html__( 'Recent Products', 'modave-core' ),
            ]
        );
		$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', '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' ),
                'description'	=> esc_html__( 'Select specific categories.', '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,
				'condition'		=> [
					'data_source!'	=> 'products',
				],
            ]
        );
		$this->add_control(
            'exclude',
            [
                'label'			=> esc_html__('Exclude Products', 'modave-core'),
				'description' 	=> esc_html__( 'Exclude some products which you do not want to display.', '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,
            ]
        );
		$this->add_control(
            'limit',
            [
                'label' 	=> esc_html__('Number Of Products', 'modave-core'),
                'type' 		=> Controls_Manager::NUMBER,
				'default' 	=> 6,
            ]
        );
		$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->add_control(
			'show_rating',
			[
				'label' 	=> esc_html__( 'Show Rating?', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 'yes',
			]
		);
		$this->add_control(
			'show_cart_button',
			[
				'label' 	=> esc_html__( 'Show Cart Button?', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 'yes',
			]
		);		
		$this->add_control(
			'product_border',
			[
				'label' 	=> esc_html__( 'Show Product Border', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 0,
			]
		);
		$this->add_responsive_control(
			'grid_columns',
			[
				'label'			=> esc_html__( 'Columns', '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' ),
				],
				'default' 			=> '3',
				'tablet_default' 	=> '2',
				'mobile_default' 	=> '1',
			]
		);
		$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 Products',
				'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(
			'heading_style_section',
			[
				'label' => esc_html__( 'Heading', 'modave-core' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'heading_typography',
				'label'    => esc_html__( 'Typography', 'modave-core' ),
				'selector' => '{{WRAPPER}} .section-heading h2',
			]
		);
		
		$this->add_responsive_control(
			'heading_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, [ 
			'grid_columns_tablet'	=> 2,
			'grid_columns_mobile'	=> 1,
		]);
		extract( $settings );
		$settings['id'] 		= pls_uniqid( 'pls-products-card-' );
		$class					= array( 'pls-element', 'widget', 'pls-products-card', 'woocommerce' );
		$class[]	 			= ( $product_border ) ? 'pls-product-border' : '';
		$settings['class'] 		= implode(' ', array_filter( $class ) ) ;		
		$this->exclude_product 	= $settings['exclude'];
		$shortcodestr 			= pls_core_get_products_shortcode_attr( $settings );
		$settings['shortcodestr'] 	= $shortcodestr;
		
		$settings['show_view_more_button'] 	= $show_view_more_button ? true : false;
		$settings['view_all_btn']['url']	= 'href="'.wc_get_page_permalink( 'shop' ).'"';
		
		$link_url = '';		
		if( ! empty( $settings['view_more_button_link']['url'] ) ) {
			$link_url = pls_core_elementor_get_url_data( $settings['view_more_button_link'] );			
		}
		
		if ( ! empty( $settings['categories'] ) ) {	
			$term_link = get_term_link( (int)trim($categories[0]), 'product_cat' );
			if ( ! is_wp_error( $term_link ) ) {
				$settings['view_all_btn']['url'] 	= 'href="'.$term_link.'"';		
			}					
		}
		
		if( ! empty( $link_url['url'] ) ){
			$settings['view_all_btn'] 	= $link_url;
		}
		
		pls_set_loop_prop( 'products-columns', $grid_columns );
		pls_set_loop_prop( 'products-columns-tablet', $grid_columns_tablet );
		pls_set_loop_prop( 'products-columns-mobile', $grid_columns_mobile );
		wc_set_loop_prop( 'columns', $grid_columns );
			
		pls_set_loop_prop( 'is_shortcode', true );
		pls_set_loop_prop( 'type', 'product_shortcode' );
		pls_set_loop_prop( 'products_view', 'grid-view' );
		pls_set_loop_prop( 'product-style', 'products-card' );
		pls_set_loop_prop( 'product_rows',1 );
		pls_set_loop_prop( 'count', 0 );
		pls_set_loop_prop( 'show_rating', $show_rating );
		pls_set_loop_prop( 'show_cart_button', $show_cart_button );
		
		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-card', $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_ProductsCard() );