<?php

namespace Elementor;

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}


/**
 *
 * storzen elementor Contact widget.
 *
 * @since 1.0
 */
class Storzen_List extends Widget_Base
{

    public function get_name()
    {
        return 'storzen-list-style';
    }

    public function get_title()
    {
        return esc_html__('List Style', 'storzen');
    }

    public function get_icon()
    {
        return 'eicon-post-list';
    }

    public function get_categories()
    {
        return ['storzen'];
    }

    protected function register_controls()
    {

        // general settings
        $this->start_controls_section(
            'ele_style_one_settings',
            [
                'label' => esc_html__('General Settings', 'storzen'),
            ]
        );


        $this->add_control(
            'style',
            [
                'label'   => esc_html__('Select Style', 'storzen'),
                'type'    => Controls_Manager::SELECT2,
                'options' => array(
                    'style-1' => esc_html__('Style 1', 'storzen'),
                ),
                'default' => 'style-1'

            ]
        );


        $this->add_control(
            'ppr',
            [
                'label'   => esc_html__('Amount of post to display', 'storzen'),
                'type'    => Controls_Manager::TEXT,
                'default' => 6
            ]
        );


        $this->end_controls_section(); // End general settings

        $this->start_controls_section(
            'Post_filter_settings',
            [
                'label' => esc_html__('Post Options', 'storzen'),
            ]
        );


        $this->add_control(
            'select_cat',
            [
                'label'    => esc_html__('Select Category', 'storzen'),
                'type'     => Controls_Manager::SELECT2,
                'description' => esc_html__('Keep Blank To Display All Categories Job', 'storzen'),
                'multiple' => true,
                'options'  => storzen_post_category(),

            ]
        );


        $this->add_control(
            'exclude_cat',
            [
                'label'    => esc_html__('Exclude Category', 'storzen'),
                'type'     => Controls_Manager::SELECT2,
                'multiple' => true,
                'options'  => storzen_post_category(),
            ]
        );

        $this->add_control(
            'orderby',
            [
                'label'   => esc_html__('Order by', 'storzen'),
                'type'    => Controls_Manager::SELECT2,
                'options' => array(
                    'author' => esc_html__('Author', 'storzen'),
                    'title'  => esc_html__('Title', 'storzen'),
                    'date'   => esc_html__('Date', 'storzen'),
                    'rand'   => esc_html__('Random', 'storzen'),
                ),
                'default' => 'date'

            ]
        );

        $this->add_control(
            'order',
            [
                'label'   => esc_html__('Order', 'storzen'),
                'type'    => Controls_Manager::SELECT2,
                'options' => array(
                    'desc' => esc_html__('DESC', 'storzen'),
                    'asc'  => esc_html__('ASC', 'storzen'),
                ),
                'default' => 'desc'

            ]
        );


        $this->end_controls_section(); // End Filter

        //title settings
        $this->start_controls_section(
            'title_settings',
            [
                'label' => esc_html__('Title Settings', 'storzen'),
            ]
        );

        $this->add_control(
            'dtitle',
            [
                'label'   => esc_html__('Display', 'storzen'),
                'type'    => Controls_Manager::SELECT2,
                'options' => array(
                    'full' => esc_html__('Full Title', 'storzen'),
                    'excerpt'  => esc_html__('Excerpt', 'storzen'),
                ),
                'default' => 'full'

            ]
        );

        $this->add_control(
            'title_excerpt_length',
            [
                'label'   => esc_html__('Title Excerpt Length', 'storzen'),
                'type'    => Controls_Manager::TEXT,
                'default' => 3,
                'condition' => [
                    'dtitle' => 'excerpt'
                ]
            ]
        );

        $this->add_control(
            'titltag',
            [
                'label'   => esc_html__('Title Tag', 'storzen'),
                'type'    => Controls_Manager::SELECT,
                'options' => array(
                    'h1' => 'H1',
                    'h2' => 'H2',
                    'h3' => 'H3',
                    'h4' => 'H4',
                    'h5' => 'H5',
                    'h6' => 'H6',
                ),
                'default' => 'h5'

            ]
        );

        $this->end_controls_section(); // End title


        // other 
        $this->start_controls_section(
            'other_settings',
            [
                'label' => esc_html__('Other Settings', 'storzen'),
            ]
        );

        $this->add_control(
            'enable_pagination',
            [
                'label' => esc_html__('Enable Pagination?', 'storzen'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Yes', 'storzen'),
                'label_off' => esc_html__('no', 'storzen'),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );


        $this->add_control(
            'open-url',
            [
                'label'   => esc_html__('Where Open Product Post?', 'storzen'),
                'type'    => Controls_Manager::SELECT,
                'options' => array(
                    'same-window' => esc_html__('Same Window', 'storzen'),
                    'new-window' => esc_html__('New Window', 'storzen'),
                ),
                'default' => 'same-window',
                'label_block' => true
            ]
        );


        $this->end_controls_section(); // End sub title

        //Content style
        $this->start_controls_section(
            'content_style',
            [
                'label' => esc_html__('Content Style', 'storzen'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        storzen_elementor_style_options($this, 'Title', '{{WRAPPER}} .content .woo-title', ['style-1']);
        storzen_elementor_style_options($this, 'Pricing', '{{WRAPPER}} .prices span', ['style-1']);
        storzen_elementor_style_options($this, 'Content', '{{WRAPPER}} .content p', ['style-1']);
        storzen_elementor_style_options($this, 'Icon', '{{WRAPPER}} .cart-btns i', ['style-1']);

        $this->end_controls_section();

		$this->start_controls_section(
			'modules_section',
			array( 'label' => esc_html__( 'Modules', 'storzen' ) )
		);
		$this->add_control( 'modules_info', array(
			'type'            => \Elementor\Controls_Manager::RAW_HTML,
			'raw'             => sprintf(
				wp_kses(
					/* translators: %s: URL to module settings page */
					__( 'Each toggle below overrides the global module setting <strong>for this widget only</strong>. A module must be enabled on the <a href="%s" target="_blank">Storzen Modules page</a> before it can be activated here.', 'storzen' ),
					array( 'strong' => array(), 'a' => array( 'href' => array(), 'target' => array() ) ) ),
				esc_url( admin_url( 'admin.php?page=storzen-modules' ) )
			),
			'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
		) );
		foreach ( array(
			'module_sale_badge'         => array( 'Sale Badge',             'Show Hot, New, and Sale % badges on each product card.' ),
			'module_quick_view'         => array( 'Quick View',             'Show a Quick View eye icon on each product card.' ),
			'module_wishlist'           => array( 'Wishlist',               'Show a Wishlist heart icon on each product card.' ),
			'module_compare'            => array( 'Compare Products',       'Show a Compare icon on each product card.' ),
			'module_flash_countdown'    => array( 'Flash Sale Countdown',   'Show a sale countdown timer below the price on each product card.' ),
			'module_stock_scarcity'     => array( 'Stock Scarcity',         'Show "Only X left!" urgency messages below the price on each product card.' ),
			'module_variation_swatches' => array( 'Variation Swatches',    'Show color / image / button swatches on variable product cards.' ),
			'module_preorder'           => array( 'Pre-Order',              'Show the Pre-Order badge on each product card. (Pro)' ),
			'module_quick_checkout'     => array( 'Quick Checkout',         'Show the Quick Checkout button on each product card. (Pro)' ),
		) as $key => $info ) {
			$this->add_control( $key, array(
				'label'        => esc_html__( $info[0], 'storzen' ), // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
				'description'  => esc_html__( $info[1], 'storzen' ), // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			) );
		}
		$this->end_controls_section();
    }

    protected function render()
    {

        $settings = $this->get_settings_for_display();

        if (is_front_page()) {
            $paged = (get_query_var('page')) ? get_query_var('page') : 1;
        } else {
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        }

        $args  = array(
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'ignore_sticky_posts' => 1,
            'posts_per_page'      => $settings['ppr'],
            'paged' => $paged,
        );

        $args['orderby'] = $settings['orderby'];
        $args['order']   = $settings['order'];
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public filter query args, read-only.
        $args['s']       = isset($_GET['keyword']) ? sanitize_text_field(wp_unslash($_GET['keyword'])) : '';

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public filter query args, read-only.
        if (! empty($_GET['product_cat']) && -1 != $_GET['product_cat']) {
            $args['tax_query'][] = array(
                'taxonomy' => 'product_cat',
                'field'    => 'slug',
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public filter, value sanitized.
                'terms'    => sanitize_text_field(wp_unslash($_GET['product_cat']))
            );
        } elseif (!empty($settings['select_cat'])) {
            $args['tax_query'][] = array(
                'taxonomy' => 'product_cat',
                'field'    => 'id',
                'terms'    => array_values($settings['select_cat'])
            );
        }


        if (!empty($settings['exclude_cat'])) {
            $args['tax_query'][] = array(
                'taxonomy' => 'product_cat',
                'field'    => 'id',
                'terms'    => array_values($settings['exclude_cat']),
                'operator' => 'NOT IN'
            );
        }

        $storzen_posts_query = new \WP_Query($args);

        \Elementor\Storzen_Archive_Grid::set_active_module_settings( $settings );
        include storzen_get_template('list-one.php');
        \Elementor\Storzen_Archive_Grid::clear_active_module_settings();
    }
}

plugin::instance()->widgets_manager->register(new Storzen_List());
