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

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

    /**
     * Get widget icon.
     *
     * Retrieve Product Custom Categories widget icon.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon() {
        return 'pls-icon eicon-product-categories';
    }
	
	/**
     * Register Product Custom Categories 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(
            'image_size',
            [
                'label' 	=> esc_html__( 'Image Size', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> $image_sizes,
                'default' 	=> 'full',
            ]
        );
		$this->add_control(
            'style',
            [
                'label' 	=> esc_html__( 'Category Style', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'1'	=> esc_html__( 'Style 1', 'modave-core' ),
					'2'	=> esc_html__( 'Style 2', 'modave-core' ),
					'3'	=> esc_html__( 'Style 3', 'modave-core' ),
				],
                'default' 	=> '1',
            ]
        );
		$this->add_control(
            'title_position',
            [
                'label'		=> esc_html__( 'Title Position', 'modave-core' ),
				'type'		=> Controls_Manager::CHOOSE,
				'options' 	=> [
					'top' => [
						'title' => esc_html__( 'Top', 'modave-core' ),
						'icon' => 'eicon-v-align-top',
					],
					'middle' => [
						'title' => esc_html__( 'Middle', 'modave-core' ),
						'icon' => 'eicon-v-align-middle',
					],
					'bottom' => [
						'title' => esc_html__( 'Bottom', 'modave-core' ),
						'icon' => 'eicon-v-align-bottom',
					],
				],
                'default' => 'bottom',
				'condition' => [
					'style' => [ '1', '2' ]
				],
            ]
        );
		$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_control(
            'categories-query',
            [
                'label'		=> esc_html__( 'Categories Query', 'modave-core' ),
				'type' 		=> Controls_Manager::HEADING,
                'separator' 	=> 'before',
            ]
        );
		$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',
				'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_custom_categories',
			array(
				'label'     => esc_html__( 'Custom Categories', 'modave-core' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
			)
		);
		
		$repeater = new Repeater();
		$repeater->add_control(
			'custom_title',
			[
				'label' 		=> esc_html__( 'Custom Title', 'modave-core' ),
				'type' 			=> Controls_Manager::TEXT,
				'placeholder'	=> esc_html__( 'Cat Title', 'modave-core' ),
			]
		);
		$repeater->add_control(
			'category',
			[
				'label' 		=> esc_html__( 'Select Category', 'modave-core' ),
                'type' 			=> Controls_Manager::SELECT,
                'options'		=> $product_cats,
                'default' 		=> '',
				'description' 	=> esc_html__( 'Select category', 'modave-core' ),
			]
		);
		$repeater->add_control(
			'cat_image',
			[
				'label'     => esc_html__( 'Image', 'modave-core' ),
				'type'      => Controls_Manager::MEDIA,
				'description'	=> esc_html__( 'Upload category image.', 'modave-core' ),
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
			]
		);		
		$this->add_control(
            'category_list',
            [
                'label' 	=> esc_html__('Category','modave-core'),
                'type' 		=> Controls_Manager::REPEATER,
                'fields' 	=> $repeater->get_controls(),
                'title_field' => '{{{ custom_title }}}',
            ]
        );
		
		$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' ),
					'11' 	=> esc_html__( '11', 'modave-core' ),
					'12' 	=> esc_html__( '12', 'modave-core' ),
				],
				'default' 			=> '4',
				'tablet_default' 	=> '3',
				'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' ),
					'11' 	=> esc_html__( '11', 'modave-core' ),
					'12' 	=> esc_html__( '12', 'modave-core' ),
				],
				'default' 			=> '4',
				'tablet_default' 	=> '3',
				'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_control(
            'heading_color',
            [
                'label'		=> esc_html__( 'Color', 'modave-core' ),
                'type'		=> Controls_Manager::COLOR,                
                'selectors'	=> [
                    '{{WRAPPER}} .section-heading h2' => 'color: {{VALUE}};'
                ],
            ]
        );
		
		$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(
			'style_section',
			[
				'label'	=> esc_html__( 'Title Style', 'modave-core' ),
				'tab'	=> Controls_Manager::TAB_STYLE,
			]
		);
		
		$this->add_responsive_control(
			'border_radius',
			[
				'label'			=> esc_html__( 'Image Radius', 'modave-core' ),
				'type'			=> Controls_Manager::DIMENSIONS,
				'size_units'	=> [ 'px', '%', 'rem' ],
				'selectors'		=> [
					'{{WRAPPER}} .product-category .pls-category-image' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->add_responsive_control(
			'title_padding',
			[
				'label'			=> esc_html__( 'Title Padding', 'modave-core' ),
				'type'			=> Controls_Manager::DIMENSIONS,
				'size_units'	=> [ 'px', '%', 'rem' ],
				'selectors'		=> [
					'{{WRAPPER}} .pls-category-content > a, {{WRAPPER}} .pls-category-thumb-content' => 'padding: {{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}} .product-category .pls-category-title, {{WRAPPER}} .product-category .category-title',
			]
		);
		$this->add_responsive_control(
			'title_wdth',
			[
				'label' => esc_html__( 'Title Width', 'modave-core' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ '%', 'px' ],
				'range' => [
					'%' => [
						'max' => 100,
					],
					'px' => [
						'max' => 500,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .pls-category-content' => 'width: {{SIZE}}{{UNIT}};',
				],
			]
		);
		
		$this->start_controls_tabs( 'title_colors' );

        $this->start_controls_tab(
            'title_color_normal',
            [
                'label' => esc_html__( 'Normal', 'modave-core' ),                
            ]
        );
		$this->add_control(
            'title_text_color',
            [
                'label'		=> esc_html__('Color', 'modave-core'),
                'type' 		=> Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pls-category-content > a, {{WRAPPER}} .product-category .product-count, {{WRAPPER}} .pls-category-thumb-content, {{WRAPPER}} .pls-category-thumb-content a' => 'color: {{VALUE}};',
                ],
            ]
        );
		$this->end_controls_tab();

        $this->start_controls_tab(
            'title_hover_color',
            [
                'label' 	=> esc_html__( 'Hover', 'modave-core' ),
            ]
        );		
		$this->add_control(
            'title_text_hover_color',
            [
                'label' 	=> esc_html__('Color', 'modave-core'),
				'type'		=> Controls_Manager::COLOR,
                'selectors' => [				
					'{{WRAPPER}} .pls-category-content > a:hover, {{WRAPPER}} .pls-category-thumb-content a:hover'	=> 'color: {{VALUE}};',
                ],
            ]
        );
		$this->end_controls_tab();

        $this->end_controls_tabs();
		
		$this->end_controls_section();
	}
	
	protected function render() {
		
		$settings = $this->get_settings();
		if ( empty( $settings['category_list'] ) ) {
			return;
		}
		$settings = wp_parse_args( $settings, [ 
			'slides_to_show_tablet' => 3,
			'slides_to_show_mobile' => 2,
			'grid_columns_tablet' => 3,
			'grid_columns_mobile' => 2
		]);
		extract( $settings );
		$settings['id'] 			= pls_uniqid( 'pls-custom-cat-' );
		$class						= array( 'pls-element', 'pls-product-custom-categories' );
		$class[]					= 'layout-'.$layout;
		$class[]					= 'style-'.$style;
		$class[]					= ( '3' != $style ) ? 'title-position-'.$title_position : '';
		$class[]					= ( $slider_extend ) ? 'pls-slider-extend-right' : '';
		$settings['class'] 			= implode(' ', array_filter( $class ) );
		$column_class = $grid_class	= array();
		$settings['section_class'] 	= '';		
		$settings['banner_layout'] 	= '';
		$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( $link_url['url'] ) ){
			$settings['view_all_btn'] 	= $link_url;
		}
		
		$grid_class[]	 = ( $show_count ) ? 'pls-has-product-count' : '';	
		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 ) );
		$results = array();
		foreach( $settings['category_list'] as $cat_data ){
			$data= array();			
			$cat = get_term_by( 'id', $cat_data['category'], 'product_cat');			
			if ( ! $cat ) {
				continue;
			}
			if($hide_empty_categories && $cat->count == 0 ){
				continue;
			}
			$thumbnail_id 				= get_term_meta( $cat->term_id, 'thumbnail_id', true );
			$data['category_title'] 	= $cat->name;
			$data['category_count'] 	= $cat->count;
			$data['category_thumbnail'] = wp_get_attachment_image_src( $thumbnail_id, $image_size );
			$data['category_link'] 		= get_term_link( $cat );
			if( ! empty( $cat_data['custom_title'] ) ){
				$data['category_title'] = $cat_data['custom_title'];				
			}
			$data['category_image'] = '';
			if( $cat_data['cat_image']['id'] ){
				$data['category_image'] = wp_get_attachment_image_src( $cat_data['cat_image']['id'] , $image_size );
				if( ! empty( $data['category_image'] )){
					$data['category_image'] = $data['category_image'][0];
				}
			}elseif(! empty( $data['category_thumbnail'] ) ){
				$data['category_image'] = $data['category_thumbnail'][0];
			}
			elseif( $cat_data['cat_image']['url'] ){
				$data['category_image'] = $cat_data['cat_image']['url'];
			}
			
			$results[] = $data;
		}
		$settings['cutom_category'] = $results;		
		pls_core_get_templates( 'elements-widgets/woo-product-cutstom-category', $settings );
	}
}

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