<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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

class product__special__carousel extends Widget_Base
{

    public function get_name()
    {
        return 'product__special__carousel';
    }

    public function get_title()
    {
        return esc_html__('Special Products Slider', 'kavehTheme');
    }

    public function get_icon()
    {
        return 'eicon-archive-posts';
    }

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

    protected function register_controls()
    {
        $this->start_controls_section(
            'contentproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Element Title', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'titleproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Subtitle', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => esc_html__('Best Selling Products', 'kavehTheme'),

            ]
        );
        $this->add_control(
            'title2productscarouselfourthstkaveh',
            [
                'label' => esc_html__('Main Title', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => esc_html__('Sports Clothing', 'kavehTheme'),

            ]
        );
        $this->add_control(
            'tagheadingproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Title Tag', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'H3',
                'options' => [
                    'H1' => esc_html__('H1', 'kavehTheme'),
                    'H2' => esc_html__('H2', 'kavehTheme'),
                    'H3' => esc_html__('H3', 'kavehTheme'),
                    'H4' => esc_html__('H4', 'kavehTheme'),
                    'H5' => esc_html__('H5', 'kavehTheme'),
                    'H6' => esc_html__('H6', 'kavehTheme'),
                    'p' => esc_html__('p', 'kavehTheme'),
                    'div' => esc_html__('div', 'kavehTheme'),
                ],
            ]
        );
        $this->end_controls_section();
        $this->start_controls_section(
            'content2productscarouselfourthstkaveh',
            [
                'label' => esc_html__('Images', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'imgproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Select Your Image', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'selectors' => [
                    '{{WRAPPER}} .new-products-category-title::after' =>
                        'background: url({{URL}})',
                    '{{WRAPPER}} .new-products-category-title::before' =>
                        'background: url({{URL}})',
                ],
            ]
        );

        $this->add_control(
            'imgbgproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Background Image', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'selectors' => [
                    '{{WRAPPER}} .new-products-category-title' =>
                        'background: url({{URL}})',
                ],
            ]
        );

        $this->end_controls_section();
        $this->start_controls_section(
            'content3productscarouselfourthstkaveh',
            [
                'label' => esc_html__('Product Settings', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'kaveh_exclude_out_of_stock',
            [
                'label' => esc_html__('Hide Out of Stock Products', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'yes' => esc_html__('Yes', 'kavehTheme'),
                'no' => esc_html__('No', 'kavehTheme'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        $this->add_control(
            'pproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Display Products', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'all',
                'options' => [
                    'all' => esc_html__('All', 'kavehTheme'),
                    'cat' => esc_html__('Category', 'kavehTheme'),

                ],
            ]
        );
        $this->add_control(
            'psortproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Sort Products By', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'date',
                'options' => [
                    'date' => esc_html__('Date', 'kavehTheme'),
                    '_price' => esc_html__('Price', 'kavehTheme'),
                    '_wc_average_rating' => esc_html__('Most Popular', 'kavehTheme'),
                    'total_sales' => esc_html__('Best Selling', 'kavehTheme'),

                ],
            ]
        );
        $this->add_control(
            'psortiproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Sort Order', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'desc',
                'options' => [
                    'asc' => esc_html__('Ascending', 'kavehTheme'),
                    'desc' => esc_html__('Descending', 'kavehTheme'),
                ],
            ]
        );
        $options4 = array();
        $orderby = 'name';
        $order = 'asc';
        $hide_empty = false;
        $cat_args = array(
            'orderby' => $orderby,
            'order' => $order,
            'hide_empty' => $hide_empty,
        );

        $product_categories = get_terms('product_cat', $cat_args);

        foreach ($product_categories as $key => $category) {
            $options4[$category->term_id] = $category->name;
        }
        $this->add_control(
            'catproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Select Category', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'multiple' => true,
                'options' => $options4,
                'condition' => [
                    'pproductscarouselfourthstkaveh' => 'cat',
                ],
            ]
        );
        $this->end_controls_section();
        $this->start_controls_section(
            'content4productscarouselfourthstkaveh',
            [
                'label' => esc_html__('Carousel Settings', 'kavehTheme'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'showarrowsproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Show Arrows?', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'yes' => esc_html__('Yes', 'kavehTheme'),
                'no' => esc_html__('No', 'kavehTheme'),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        $this->add_control(
            'productnumberproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Number of Products Displayed', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 0,
                'max' => 50,
                'step' => 1,
                'default' => 5,
            ]
        );
        $this->add_control(
            'productcolumnproductscarouselfourthstkaveh',
            [
                'label' => esc_html__('Number of Product Columns on Desktop', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 0,
                'max' => 5,
                'step' => 0.5,
                'default' => 3,

            ]
        );
        $this->add_control(
            'productcolumn2productscarouselfourthstkaveh',
            [
                'label' => esc_html__('Number of Product Columns on Tablet', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 0,
                'max' => 5,
                'step' => 0.5,
                'default' => 2,

            ]
        );
        $this->add_control(
            'productcolumn3productscarouselfourthstkaveh',
            [
                'label' => esc_html__('Number of Product Columns on Mobile', 'kavehTheme'),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 0,
                'max' => 5,
                'step' => 0.5,
                'default' => 1.5,

            ]
        );
        $this->end_controls_section();
    }
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $clsid = $this->get_id();
        ?>

        <!-- Start Discount -->
        <section>
            <!-- Start Carousel -->
            <div class="swiper swiper-discount-product-special position-relative">
                <div class="swiper-wrapper">
                    <?php if ('cat' === $settings['pproductscarouselfourthstkaveh']) { ?>
                        <?php

                        // WP_Query args
                        $q_pc4_query_args = array(
                            'post_type' => array('product'),
'post_status' => array('publish'),
                            'posts_per_page' => $settings['productnumberproductscarouselfourthstkaveh'],
                            'order' => 'DESC',
                            'orderby' => 'date',
                            'tax_query' => array(
                                array(
                                    "taxonomy" => "product_cat",
                                    "field" => "tag_ID",
                                    "terms" => $settings['catproductscarouselfourthstkaveh'],
                                ),
                            ),
                        );
                        if ("yes" === $settings['kaveh_exclude_out_of_stock']) {
                            $q_pc4_query_args['meta_query'] = array(
                                array(
                                    'key' => '_stock_status',
                                    'value' => 'outofstock',
                                    'compare' => 'NOT IN',
                                ),
                            );
                        }

                        // The Query
                        $pc4_query = new \WP_Query($q_pc4_query_args);

                        // The Loop
                        if ($pc4_query->have_posts()) {
                            while ($pc4_query->have_posts()) {
                                $pc4_query->the_post();
                                global $product;
                                $sale_price = $product->get_sale_price();
                                $normal_price = $product->get_regular_price();
                                $pr_id = get_the_ID();
                                $stocka = $product->get_stock_status();
                                ?>
                                <!-- Start Slide -->

                                <div class="swiper-slide">
                                    <div class="product-eight product-eight-offer">
                                        <div class="image full-width">
                                            <a href="<?php the_permalink(); ?>">
                                                <img src="<?php the_post_thumbnail_url('full'); ?>" alt="<?php the_title(); ?>"
                                                    width="266" height="266">
                                            </a>

                                            <div class="offer">
                                                <?php echo $normal_price; ?>
                                            </div>

                                            <?php
                                            if ("instock" != $stocka) { ?>
                                                <div class="status position-absolute top-0 text-center rounded-pill">
                                                    Out of Stock
                                                </div>
                                            <?php } ?>
                                        </div>
                                        <div class="content">
                                            <ul class="tags d-flex align-items-center flex-wrap">
                                                <li>
                                                    <a href="javascript:void(0)" class="d-block rounded-pill"> #Special Sale </a>
                                                </li>
                                                <li>
                                                    <a href="javascript:void(0)" class="d-block rounded-pill"> #Special Sale </a>
                                                </li>
                                                <li>
                                                    <a href="javascript:void(0)" class="d-block rounded-pill"> #Special Sale </a>
                                                </li>
                                            </ul>
                                            <h2 class="product-name">
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php the_title(); ?>
                                                </a>
                                            </h2>
                                            <div class="d-flex align-items-center justify-content-between">
                                                <?php if ("instock" == $stocka) { ?>
                                                    <?php if (!$product->is_type('variable')) { ?>
                                                        <?php if (!empty($sale_price)) { ?>
                                                            <div class="offer">
                                                                <?php echo $normal_price; ?>
                                                            </div>
                                                            <div class="price">
                                                                <?php echo number_format($sale_price, 0, '.', ','); ?>
                                                                <span>
                                                                    <?php echo get_woocommerce_currency_symbol(); ?>
                                                                </span>
                                                            </div>
                                                        <?php } else { ?>
                                                            <div class="price nor10p">
                                                                <?php if (!empty($normal_price)) {
                                                                    echo number_format($normal_price, 0, '.', ',');
                                                                } ?>
                                                                <span>
                                                                    <?php echo get_woocommerce_currency_symbol(); ?>
                                                                </span>
                                                            </div>
                                                        <?php } ?>
                                                    <?php } else { ?>
                                                        <div>
                                                            <?php
                                                            $price_v = $product->get_price_html();
                                                            echo str_replace("&ndash;", "<span class=\"dashvar\"> – </span>", $price_v); ?>
                                                        </div>
                                                        <?php
                                                    }
                                                    ?>
                                                <?php } else {
                                                    ?>
                                                    <div class="mt-p10"></div>
                                                    <?php
                                                } ?>
                                                <?php if (empty($normal_price)) { ?>
                                                    <div class="mt-p101"></div>
                                                <?php } ?>





                                                <a href="javascript:void(0)" class="btn btn-success-2 shadow-none"> + Purchase </a>
                                            </div>
                                            <a href="<?php the_permalink(); ?>" class="btn btn-secondary rounded-pill"> View and Purchase Product
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                <!-- End Slide -->

                                <?php
                                // Reset Original Post Data
                                wp_reset_postdata();
                            }
                        }
                    } elseif ('all' === $settings['pproductscarouselfourthstkaveh']) {
                        // WP_Query args
                        $ordvalue = $settings['psortproductscarouselfourthstkaveh'];
                        $ordi = $settings['psortiproductscarouselfourthstkaveh'];
                        $q_pc4_query_args = array(
                            'post_type' => array('product'),
'post_status' => array('publish'),
                            'posts_per_page' => $settings['productnumberproductscarouselfourthstkaveh'],
                            'order' => $ordi,
                            'orderby' => 'meta_value_num',
                            'meta_key' => $ordvalue,
                        );
                        if ("yes" === $settings['kaveh_exclude_out_of_stock']) {
                            $q_pc4_query_args['meta_query'] = array(
                                array(
                                    'key' => '_stock_status',
                                    'value' => 'outofstock',
                                    'compare' => 'NOT IN',
                                ),
                            );
                        }

                        // The Query
                        $pc4_query = new \WP_Query($q_pc4_query_args);

                        // The Loop
                        if ($pc4_query->have_posts()) {
                            while ($pc4_query->have_posts()) {
                                $pc4_query->the_post();
                                global $product;
                                $sale_price = $product->get_sale_price();
                                $normal_price = $product->get_regular_price();
                                $pr_id = get_the_ID();
                                $stocka = $product->get_stock_status(); ?>
                                <!-- Start Slide -->

                                <div class="swiper-slide">
                                    <div class="product-eight product-eight-offer">
                                        <div class="image full-width">
                                            <a href="<?php the_permalink(); ?>">
                                                <img src="<?php the_post_thumbnail_url('full'); ?>" alt="<?php the_title(); ?>"
                                                    width="266" height="266">
                                            </a>

                                            <div class="offer">
                                                <?php echo $normal_price; ?>
                                            </div>

                                            <?php
                                            if ("instock" != $stocka) { ?>
                                                <div class="status position-absolute top-0 text-center rounded-pill">
                                                    Out of Stock
                                                </div>
                                            <?php } ?>
                                        </div>
                                        <div class="content">
                                            <ul class="tags d-flex align-items-center flex-wrap">
                                                <li>
                                                    <a href="javascript:void(0)" class="d-block rounded-pill"> #Special Sale </a>
                                                </li>
                                                <li>
                                                    <a href="javascript:void(0)" class="d-block rounded-pill"> #Special Sale </a>
                                                </li>
                                                <li>
                                                    <a href="javascript:void(0)" class="d-block rounded-pill"> #Special Sale </a>
                                                </li>
                                            </ul>
                                            <h2 class="product-name">
                                                <a href="<?php the_permalink(); ?>">
                                                    <?php the_title(); ?>
                                                </a>
                                            </h2>
                                            <div class="d-flex align-items-center justify-content-between">
                                                <?php if ("instock" == $stocka) { ?>
                                                    <?php if (!$product->is_type('variable')) { ?>
                                                        <?php if (!empty($sale_price)) { ?>
                                                            <div class="offer">
                                                                <?php echo $normal_price; ?>
                                                            </div>
                                                            <div class="price">
                                                                <?php echo number_format($sale_price, 0, '.', ','); ?>
                                                                <span>
                                                                    <?php echo get_woocommerce_currency_symbol(); ?>
                                                                </span>
                                                            </div>
                                                        <?php } else { ?>
                                                            <div class="price nor10p">
                                                                <?php if (!empty($normal_price)) {
                                                                    echo number_format($normal_price, 0, '.', ',');
                                                                } ?>
                                                                <span>
                                                                    <?php echo get_woocommerce_currency_symbol(); ?>
                                                                </span>
                                                            </div>
                                                        <?php } ?>
                                                    <?php } else { ?>
                                                        <div>
                                                            <?php
                                                            $price_v = $product->get_price_html();
                                                            echo str_replace("&ndash;", "<span class=\"dashvar\"> – </span>", $price_v); ?>
                                                        </div>
                                                        <?php
                                                    }
                                                    ?>
                                                <?php } else {
                                                    ?>
                                                    <div class="mt-p10"></div>
                                                    <?php
                                                } ?>
                                                <?php if (empty($normal_price)) { ?>
                                                    <div class="mt-p101"></div>
                                                <?php } ?>





                                                <a href="javascript:void(0)" class="btn btn-success-2 shadow-none"> + Purchase </a>
                                            </div>
                                            <a href="<?php the_permalink(); ?>" class="btn btn-secondary rounded-pill"> View and Purchase Product
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                <!-- End Slide -->
                                <?php
                                // Reset Original Post Data
                                wp_reset_postdata();
                            }
                        }
                    } ?>

                </div>
                <!-- Start Pagination -->
                <div class="swiper-pagination end-0"></div>
                <!-- End Pagination -->
            </div>
            <!-- End Carousel -->
        </section>
        <!-- End Discount -->
        <style>
            .product-eight {
                height: auto !important;
            }
        </style>
        <?php
    }

}