<?php
/*
Element: Product Categories Thumbnail
*/
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;

class PLS_Elementor_ProductCategoriesThumbnail extends PLS_Elementor_Widget_Base {
	/**
	 * Retrieve the widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
        return 'pls-product-categories-thumbnail';
    }

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

    /**
     * Get widget icon.
     *
     * Retrieve Product Categories Thumbnail widget icon.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon() {
        return 'pls-icon eicon-product-categories';
    }
	
	/**
	 * Get widget keywords.
	 *
	 * Retrieve Product Categories Thumbnail of keywords the list widget belongs to.
	 *
	 * @since 1.0.0
	 * @access public
	 * @return array Widget keywords.
	 */
	public function get_keywords() {
		return [ 'woocommerce', 'categories', 'thumbnail', 'product categories thumbnail'];
	}
	
	/**
     * Register Product Categories Thumbnail 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() {
		
		$image_sizes 			= pls_core_get_all_image_sizes(true);
		$product_cats = pls_core_elementor_get_terms('product_cat',true);
		
		$this->start_controls_section(
            'content_section',
            [
                'label' => esc_html__( 'General', 'modave-core' ),
            ]
        );
		$this->add_control(
            'heading',
            [
                'label' 	=> esc_html__('Heading', 'modave-core'),
                'type' 		=> Controls_Manager::TEXT,
				 'default' 	=> '',
            ]
        );
		$this->add_control(
            'layout',
            [
                'label' 	=> esc_html__( 'Layout', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'slider'	=> esc_html__( 'Slider', 'modave-core' ),
					'grid'		=> esc_html__( 'Grid', 'modave-core' ),
				],
                'default' 	=> 'slider',
            ]
        );
		$this->add_control(
            'style',
            [
                'label' 	=> esc_html__( 'Style', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'rounded'	=> esc_html__( 'Rounded', 'modave-core' ),
					'circle'	=> esc_html__( 'Circle', 'modave-core' ),
				],
                'default' 	=> 'rounded',
            ]
        );
		$this->add_control(
            'image_type',
            [
                'label' 	=> esc_html__( 'Image Type', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'default'	=> esc_html__( 'Default', 'modave-core' ),
					'icon'		=> esc_html__( 'Icon', 'modave-core' ),
				],
                'default' 	=> 'default',
            ]
        );
		$this->add_control(
            'image_size',
            [
                'label' 	=> esc_html__( 'Image Size', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> $image_sizes,
                'default' 	=> 'medium',
            ]
        );
		$this->add_responsive_control(
			'image_width',
			[
				'label'     	=> esc_html__( 'Image Width', 'modave-core' ),
				'label_block' 	=> true,
				'type'      	=> Controls_Manager::SLIDER,
				'range'			=> [ 'px' => [ 'min' => 0, 'max' => 500 ] ],
				'selectors' 	=> [
					'{{WRAPPER}} .pls-product-categories-thumbnails.pls-category-thumb-circle .pls-category-image' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}}; border-radius: {{SIZE}}{{UNIT}}'
				],
				'condition' 	=> [
					'style' => 'circle'
				],
			]
		);
		$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' ),
            ]
        );
		$this->add_control(
            'parent_category',
            [
                'label' 	=> esc_html__( 'Parent Category', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
				'default' 	=> '',
                'options' 	=> $product_cats,				
                'description' => esc_html__( 'Each category item will be a sub category of this category. This option is available when the specific Categories option is empty.', 'modave-core' ),
            ]
        );
		$this->add_control(
            'exclude_categories',
            [
                'label'			=> esc_html__( 'Exclude 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__( 'Exclude specific categories.', 'modave-core' ),
            ]
        ); 
		$this->add_control(
            'number',
            [
                'label' 	=> esc_html__('Number of Categories', 'modave-core'),
                'type' 		=> Controls_Manager::NUMBER,
				'default' 	=> '8',
            ]
        );
		$this->add_control(
            'orderby',
            [
                'label' 	=> esc_html__( 'Order By', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'name'			=> esc_html__( 'Name', 'modave-core' ),
					'slug'			=> esc_html__( 'Slug', 'modave-core' ),
					'ID'			=> esc_html__( 'ID', 'modave-core' ),
					'menu_order'	=> esc_html__( 'Sort Order', 'modave-core' ),
				],
				'default' 	=> 'ID',
            ]
        );
		$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_child_of',
			[
				'label' 	=> esc_html__( 'Child Of', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 0,
			]
		);
		$this->add_control(
			'hide_empty_categories',
			[
				'label' 	=> esc_html__( 'Hide Empty Categories', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 'yes',
			]
		);
		$this->add_control(
			'show_title',
			[
				'label' 	=> esc_html__( 'Show Title', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 'yes',
			]
		);
		$this->add_control(
			'show_count',
			[
				'label' 	=> esc_html__( 'Show Count', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 0,
			]
		);
		$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_grid',
			array(
				'label'     => esc_html__( 'Grid Settings', 'modave-core' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'layout' => 'grid',
				),
			)
		);
		$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' ),
					'5'		=> esc_html__( '5', 'modave-core' ),
					'6'		=> esc_html__( '6', 'modave-core' ),
					'7'		=> esc_html__( '7', 'modave-core' ),
					'8'		=> esc_html__( '8', 'modave-core' ),
					'9'		=> esc_html__( '9', 'modave-core' ),
					'10'	=> esc_html__( '10', 'modave-core' ),
				],
				'default' 			=> '6',
				'tablet_default' 	=> '4',
				'mobile_default' 	=> '2',
			]
		);
		$this->end_controls_section();
		
		$this->start_controls_section(
			'section_content_slider',
			array(
				'label'     => esc_html__( 'Slider Settings', 'modave-core' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'layout' => 'slider',
				),
			)
		);
		$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' ),
					'7'		=> esc_html__( '7', 'modave-core' ),
					'8'		=> esc_html__( '8', 'modave-core' ),
					'9'		=> esc_html__( '9', 'modave-core' ),
					'10'	=> esc_html__( '10', 'modave-core' ),
				],
				'default' 			=> '6',
				'tablet_default' 	=> '4',
				'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->add_control(
			'slider_extend',
			[
				'label'     => esc_html__( 'Slider Extend Right', 'modave-core' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 0,
			]
		);
		$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();
		
		$this->start_controls_section(
			'categories_style',
			[
				'label'	=> esc_html__( 'Categories', 'modave-core' ),
				'tab'	=> Controls_Manager::TAB_STYLE,
			]
		);
		
		$this->add_responsive_control(
			'grid_gap',
			[
				'label'			=> esc_html__( 'Grid Gap', 'modave-core' ),
				'type'			=> Controls_Manager::SLIDER,
				'size_units' 	=> [ 'px' ],
				'range' 		=> [
					'px' 	=> [ 'min' => 0, 'max' => 30 ],
				],
				'selectors'		=> [
					'{{WRAPPER}} .products .product-category' => 'padding-left: {{SIZE}}{{UNIT}}; padding-right: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .section-content.row' => 'margin-left: -{{SIZE}}{{UNIT}}; margin-right: -{{SIZE}}{{UNIT}};',
				],
			]
		);
		$this->add_responsive_control(
			'border_radius',
			[
				'label'			=> esc_html__( 'Image Radius', 'modave-core' ),
				'type'			=> Controls_Manager::DIMENSIONS,
				'size_units'	=> [ 'px', '%', 'rem' ],
				'selectors'		=> [
					'{{WRAPPER}} .pls-product-categories-thumbnails .pls-category-image' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'title_typography',
				'label'    => esc_html__( 'Title Typography', 'modave-core' ),
				'selector' => '{{WRAPPER}} .pls-product-categories-thumbnails .category-title',
			]
		);
		
		$this->end_controls_section();		
	}
	
	protected function render() {
		
		$settings 	= $this-> get_settings();
		$settings = wp_parse_args( $settings, [ 
			'slides_to_show_tablet' => 4,
			'slides_to_show_mobile' => 2,
			'grid_columns_tablet' => 4,
			'grid_columns_mobile' => 2,
		]);
		extract( $settings );
		$settings['id'] 			= pls_uniqid( 'pls-product-cate-thumb-' );
		$class						= array( 'pls-element', 'pls-product-categories-thumbnails' );	 
		$class[]					= 'pls-category-thumb-'.$style;
		$class[]					= 'layout-'.$layout;
		$class[]					= ( $slider_extend ) ? 'pls-slider-extend-right' : '';
		$settings['class'] 			= implode( ' ', array_filter( $class ) );
		$settings['section_class'] 	= '';
		$column_class				= array();
		$grid_class					= array();
		$settings['show_view_more_button'] 	= $show_view_more_button ? true : false;
		$settings['view_all_btn']['url']	= 'href="'.wc_get_page_permalink( 'shop' ).'"';
		
		$query_args = array(
			'taxonomy'  	=> 'product_cat',
			'number'    	=> $settings['number'],
			'orderby'    	=> $settings['orderby'],
			'order'      	=> $settings['order'],
			'hide_empty' 	=> $hide_empty_categories,
		);
		$settings['args']		= $query_args; // Query for inner sub categories
		
		if( empty( $categories ) && empty( $parent_category ) ){
			if( $show_child_of ){
				$query_args['child_of'] = (int)$parent_category;
			}else{
				$query_args['parent'] = 0;	
			}
		}
		
		$ids = array();
		if ( !empty( $parent_category ) && empty( $categories ) ) {
			
			if($show_child_of){
				$query_args['child_of'] = (int)$parent_category;
			}else{
				$query_args['parent'] = (int)$parent_category;
			}
		}		
		
		$link_url = array();			
		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'] ) ) {
			$query_args['include'] 	= $categories;
		}
		
		if ( ! empty( $settings['exclude_categories'] ) ) {				
			$query_args['exclude'] = $exclude_categories;			
		}
		
		if( !empty( $link_url['url'] ) ){
			$settings['view_all_btn'] 	= $link_url;
		}
		
		$slider_data 					= array();
		$product_categories 			= get_terms( $query_args );		
		$settings['product_categories'] = $product_categories;
		$column_class 	= array();
		
		if( 'grid' == $layout ){
			$grid_class[] 	= 'row';
			$grid_class[] 	= 'grid-col-lg-'. $grid_columns;
			$grid_class[] 	= 'grid-col-md-'. $grid_columns_tablet;
			$grid_class[] 	= 'grid-col-'. $grid_columns_mobile;
		}else{			
			$settings['section_class'] 	= ' pls-slider swiper row';
			$settings['slider_options'] = pls_slider_attributes( $settings);
			$grid_class[] = 'swiper-wrapper';
			$grid_class[] = 'slider-col-lg-'.$slides_to_show;
			$grid_class[] = 'slider-col-md-'.$slides_to_show_tablet;
			$grid_class[] = 'slider-col-'.$slides_to_show_mobile;
			$column_class[] = ' swiper-slide';
		}
		
		$settings['column_class']	= implode( ' ', array_filter( $column_class ) );
		$settings['grid_class']	= implode( ' ', array_filter( $grid_class ) );
		
		pls_core_get_templates( 'elements-widgets/woo-product-categories-thumbnail', $settings );
	}
}

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