<?php
/*
Element: Categories Collections
*/
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
class PLS_Elementor_CategoriesCollections extends PLS_Elementor_Widget_Base {
	/**
	 * Retrieve the widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
        return 'pls-categories-collections';
    }

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

    /**
     * Get widget icon.
     *
     * Retrieve Categories Collections 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 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', 'categories', 'product categories'];
	}
	
	/**
     * Register Categories Collections 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(
			'show_category_image',
			[
				'label' 	=> esc_html__( 'Show Category Image', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 'yes',
			]
		);
		$this->add_control(
            'image_size',
            [
                'label' 	=> esc_html__( 'Image Size', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> $image_sizes,
                'default' 	=> 'thumbnail',
				'condition' => [
					'show_category_image' => 'yes'
				],
            ]
        );
		$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' 	=> '6',
            ]
        );
		$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_count',
			[
				'label' 	=> esc_html__( 'Show Count', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
				'default' 	=> 'yes',
			]
		);
		$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 Collection',
				'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(
			'style_section',
			[
				'label'	=> esc_html__( 'Style', 'modave-core' ),
				'tab'	=> Controls_Manager::TAB_STYLE,
			]
		);		
		$this->add_responsive_control(
			'grid_gap',
			[
				'label'			=> esc_html__( 'Grid Gap', 'modave-core' ),
				'description' 	=> esc_html__( 'Space between categories grid gap.', 'modave-core' ),
				'type'			=> Controls_Manager::SLIDER, 
				'size_units' 	=> [ 'px', 'rem', '%', 'em', 'custom' ],
				'range'       	=> [
					'px'  => [
						'step' => 1,
						'min'  => 0,
						'max'  => 200,
					],
					'rem' => array(
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					),
					'%'   => [
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					],
				],
				'selectors'		=> [
					'{{WRAPPER}} .pls-categories-collections-lists' => 'gap: {{SIZE}}{{UNIT}}',
				],
			]
		);
		$this->add_responsive_control(
			'image_width',
			[
				'label'			=> esc_html__( 'Image Width', 'modave-core' ),
				'description' 	=> esc_html__( 'Set category image width.', 'modave-core' ),
				'type'			=> Controls_Manager::SLIDER, 
				'size_units' 	=> [ 'px', 'rem', '%', 'em', 'custom' ],
				'range'       	=> [
					'px'  => [
						'step' => 1,
						'min'  => 0,
						'max'  => 400,
					],
					'rem' => array(
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					),
					'%'   => [
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					],
				],
				'selectors'		=> [
					'{{WRAPPER}} .pls-cl-category .pls-cl-category-image' => 'width: {{SIZE}}{{UNIT}}',
				],
			]
		);
		$this->add_responsive_control(
			'border_radius',
			[
				'label'			=> esc_html__( 'Image Radius', 'modave-core' ),
				'description' 	=> esc_html__( 'Set category image radius.', 'modave-core' ),
				'type'			=> Controls_Manager::DIMENSIONS,
				'size_units' 	=> [ 'px', 'rem', '%', 'em', 'custom' ],
				'range'       	=> [
					'px'  => [
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					],
					'rem' => array(
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					),
					'%'   => [
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					],
				],
				'selectors'		=> [
					'{{WRAPPER}} .pls-cl-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-cl-category-title',
			] 
		);
		$this->add_responsive_control(
			'image_title_gap',
			[
				'label'			=> esc_html__( 'Image & Title Gap', 'modave-core' ),
				'description' 	=> esc_html__( 'Space between category image & title.', 'modave-core' ),
				'type'			=> Controls_Manager::SLIDER, 
				'size_units' 	=> [ 'px', 'rem', '%', 'em', 'custom' ],
				'range'       	=> [
					'px'  => [
						'step' => 1,
						'min'  => 0,
						'max'  => 200,
					],
					'rem' => array(
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					),
					'%'   => [
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					],
				],
				'selectors'		=> [
					'{{WRAPPER}} .pls-cl-category' => 'gap: {{SIZE}}{{UNIT}}',
				],
			]
		);
		$this->add_responsive_control(
			'button_gap',
			[
				'label'			=> esc_html__( 'Button Gap', 'modave-core' ),
				'description' 	=> esc_html__( 'Space between categories & button.', 'modave-core' ),
				'type'			=> Controls_Manager::SLIDER, 
				'size_units' 	=> [ 'px', 'rem', '%', 'em', 'custom' ],
				'range'       	=> [
					'px'  => [
						'step' => 1,
						'min'  => 0,
						'max'  => 200,
					],
					'rem' => array(
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					),
					'%'   => [
						'step' => 1,
						'min'  => 0,
						'max'  => 100,
					],
				],
				'selectors'		=> [
					'{{WRAPPER}} .pls-categories-collections .view-all-btn' => 'margin-top: {{SIZE}}{{UNIT}}',
				],
			]
		);		
		$this->end_controls_section();
	}
	
	protected function render() {
		
		$settings = $this->get_settings();
		extract( $settings );
		
		$settings['id'] 			= pls_uniqid( 'pls-categories-collections-' );
		$class						= array( 'pls-element', 'pls-categories-collections' );
		$settings['class'] 			= implode(' ',array_filter( $class ) );
		$settings['show_count'] 			= $show_count ? true : false;
		$settings['show_view_more_button'] 	= $show_view_more_button ? true : false;
		$settings['view_all_btn']['url']	= 'href="'.wc_get_page_permalink( 'shop' ).'"';
		$settings['section_class'] 			= '';
		
		$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 ) ){
			$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;
			}				
			
			$term = get_term_by( 'id',(int)$parent_category, 'product_cat' );
			if( ! empty( $term ) ):
				$settings['view_all_btn']['url'] = 'href="'.get_term_link($term).'"';
			endif;
		}		
		
		if ( ! empty( $settings['exclude_categories'] ) ) {
			$query_args['exclude'] = $settings['exclude_categories'];			
		} 		
		
		$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'] ) ) {			
			$query_args['include'] 		= $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;
		}
		
		$product_categories 			= get_terms( $query_args );		
		$settings['product_categories'] = $product_categories;
		
		pls_core_get_templates( 'elements-widgets/woo-categories-collections', $settings );
	}
}

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