<?php
/*
 * Element: Products Video(Tiktok)
 */
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Icons_Manager;
use Elementor\Utils;
class PLS_Elementor_ProductsVideo extends PLS_Elementor_Widget_Base {
	/**
	 * Retrieve the widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
        return 'pls-products-video';
    }

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

    /**
     * Get widget icon.
     *
     * Retrieve Products video 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(
            '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_responsive_control(
			'product_height',
			[
				'label'			=> esc_html__( 'Product Height', 'modave-core' ),
				'description' 	=> esc_html__( 'Set product height.', 'modave-core' ),
				'type'			=> Controls_Manager::SLIDER, 
				'size_units' 	=> [ 'px' ],
				'range'       	=> [
					'px'  => [
						'step' => 1,
						'min'  => 0,
						'max'  => 800,
					]
				],
				'selectors'		=> [
					'{{WRAPPER}} .pls-product-video-img' => 'max-height: {{SIZE}}{{UNIT}}',
				],
			]
		);
		$this->add_control(
			'show_follow_button',
			[
				'label' 	=> esc_html__( 'Show Follow Us Button', 'modave-core' ),
				'type' 		=> Controls_Manager::SWITCHER,
			]
		);
		$this->add_control(
			'follow_button_text',
			[
				'label' 	=> esc_html__( 'Follow Button Text', 'modave-core' ),
				'type' 		=> Controls_Manager::TEXT,
				'default' 	=> 'Follow Us',
				'condition' => [
					'show_follow_button' => 'yes'
				],
			]
		);
		$this->add_control(
			'follow_button_link',
			[
				'label' 	=> esc_html__( 'Follow Button Link', 'modave-core' ),
				'type' 		=> Controls_Manager::URL,				
				'condition' => [
					'show_follow_button' => 'yes'
				],
			]
		);
		$this->end_controls_section();
		
		$this->start_controls_section(
			'section_custom_products',
			array(
				'label'     => esc_html__( 'Products', 'modave-core' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
			)
		);
		
		$repeater = new Repeater();
		$repeater->add_control(
            'product_id',
            [
                'label'			=> esc_html__( 'Select Product', 'modave-core' ),
                'type'			=> 'pls_autocomplete',
				'search'		=> 'pls_core_elementor_search_post',
				'render'		=> 'pls_core_elementor_render_post',
				'post_type'		=> 'product',
				'label_block'	=> true,
				'description' 	=> esc_html__( 'Select specific product.', 'modave-core' ),
            ]
        );
		$repeater->add_control(
            'product_video_url',
            [
                'label' 	=> esc_html__( 'Product Video URL', 'modave-core' ),
                'type' 		=> Controls_Manager::TEXT,
            ]
        );
		$repeater->add_control(
			'product_image',
			[
				'label'     => esc_html__( 'Product Image', 'modave-core' ),
				'type'      => Controls_Manager::MEDIA,
				'description'	=> esc_html__( 'Upload product image.', 'modave-core' ),
				'default' => [
					'url' => Utils::get_placeholder_image_src(),
				],
			]
		);	
		$this->add_control(
            'products_list',
            [
                'label' 	=> esc_html__( 'Product', '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' ),
				],
				'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' ),
				],
				'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->end_controls_section();
			
		$this->start_controls_section(
			'style_section',
			[
				'label' => esc_html__( 'Content', 'modave-core' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control(
            'title_color',
            [
                'label'		=> esc_html__( 'Conent Color', 'modave-core' ),
                'type'		=> Controls_Manager::COLOR,                
                'selectors'	=> [
                    '{{WRAPPER}} .pls-products-video .product-title a, {{WRAPPER}} .pls-products-video div.product .price, {{WRAPPER}} .pls-products-video div.product .price ins, {{WRAPPER}} .pls-products-video div.product .price del' => 'color: {{VALUE}};'
                ],
            ]
        );
		$this->add_control(
            'content_bg_color',
            [
                'label'		=> esc_html__( 'Conent Background', 'modave-core' ),
                'type'		=> Controls_Manager::COLOR,                
                'selectors'	=> [
                    '{{WRAPPER}} .pls-products-video .pls-product-content' => 'background-color: {{VALUE}};'
                ],
            ]
        );
		$this->end_controls_section();
	}
	
	protected function render() {
		
		$settings = $this->get_settings();
		if ( empty( $settings['products_list'] ) ) {
			return;
		}
		$settings = wp_parse_args( $settings, [ 
			'slides_to_show_tablet' => 3,
			'slides_to_show_mobile' => 1,
			'grid_columns_tablet' => 3,
			'grid_columns_mobile' => 1
		]);
		extract( $settings );
		$settings['id'] 			= pls_uniqid( 'pls-products-video-' );
		$class						= array( 'pls-element', 'pls-products-video', 'woocommerce' );
		$settings['class'] 			= implode( ' ', array_filter( $class ) );
		$column_class				= array();
		$grid_class					= array( 'pls-layouts-'.$layout );
		$settings['section_class'] 	= '';		
		$settings['banner_layout'] 	= '';
		
		$settings['show_follow_button'] 	= $show_follow_button ? true : false;
		$settings['follow_btn_link']['url']	= 'href="#"';		
		$link_url = '';		
		if( ! empty( $settings['follow_button_link']['url'] ) ) {
			$link_url = pls_core_elementor_get_url_data( $settings['follow_button_link'] );			
		}
		if( ! empty( $link_url['url'] ) ){
			$settings['follow_btn_link'] 	= $link_url;
		}
		
		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['products_list'] as $product_data ){
			$data	= array();
			$data['has_product'] = false;
			if( ! empty( $product_data['product_id'] ) ){
			$product 	= wc_get_product( $product_data['product_id']  );
				if( !empty( $product ) ) {
					$data['has_product'] 		= true;
					$data['product_id']			= $product_data['product_id'];
					$data['product_link']		= get_permalink( $product_data['product_id'] );
					$data['product_thumbnail']	= $product->get_image('thumbnail');
					$data['product_title']		= $product->get_title();
					$data['product_price']		= $product->get_price_html();
				}
			}
			$data['image_thumb_html'] 			= pls_core_print_image_html( $product_data, 'product_image', $image_size );
			$data['video_link']					= $product_data['product_video_url'];			
			$results[] = $data;
		}
		$settings['products_data'] = $results;		
		pls_core_get_templates( 'elements-widgets/woo-products-video', $settings );
	}
}

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