<?php
/*
 * Element: Single Product
 */
use Elementor\Controls_Manager;

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

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

    /**
     * Get widget icon.
     *
     * Retrieve Hot Deal Products widget icon.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon() {
        return 'pls-icon eicon-single-product';
    }
	
	/**
	 * 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', 'feature', 'products' ];
	}
	
	/**
     * Register Hot Deal Products 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(
            'content_section',
            [
                'label' => esc_html__( 'General', 'modave-core' ),
            ]
        );
		$this->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' ),
            ]
        );
		$this->add_control(
            'product_thumbnail_style',
            [
                'label' 	=> esc_html__( 'Product Thumbnail Style', 'modave-core' ),
                'type' 		=> Controls_Manager::SELECT,
                'options' 	=> [
					'product-thumbnail-none'	=> esc_html__( 'No Thumbnail', 'modave-core' ),
					'product-thumbnail-left'	=> esc_html__( 'Thumbnail Left', 'modave-core' ),
					'product-thumbnail-bottom'	=> esc_html__( 'Thumbnail Bottom', 'modave-core' ),
				],
                'default' 		=> 'product-thumbnail-left',
				'description' 	=> esc_html__( 'Select product thumbnail style.', 'modave-core' ),
            ]
        );
		$this->end_controls_section();		
	}
	
	protected function render() {
		
		$settings = $this->get_settings();
		
		extract( $settings );
		
		if( empty( $product_id ) ){
			return;
		}
		
		global $post, $product;
		$global_post    = $post;
		$global_product = $product;
		
		$product	= wc_get_product( $product_id );
		if( ! $product ){
			return;
		}
		$post             		= get_post( $product_id );
		$settings['id'] 		= pls_uniqid( 'pls-single-product-' );
		$settings['product'] 	= $product;
		$classes				= array( 'pls-element', 'pls-single-product', 'pls-product-content-style-1' );
		$classes[]				= $product_thumbnail_style;	
		$settings['classes'] 	= $classes;
		pls_set_loop_prop( 'product-gallery-style', $product_thumbnail_style );
		remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
		pls_core_get_templates( 'elements-widgets/woo-single-product', $settings );
		$post    = $global_post;
		$product = $global_product;
	}
}

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