<?php

class PZY_Products_Slider extends \PEZESHKYAR\CORE\PZY_SWIPER
{
    public function get_name()
    {
        return 'pzy_products_slider';
    }

    public function get_title()
    {
        return esc_html__('اسلایدر محصولات ', 'pezeshkyar');
    }

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

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

    public function get_keywords()
    {
        return ['pzy', 'staff', 'staffs', 'slider'];
    }

    public function get_script_depends()
    {
        return ["swiper.js", "theme-script.js","'wc-add-to-cart'"];
    }

    public function get_style_depends()
    {
        return ['swiper.css'];
    }


    protected function register_controls()
    {
        $this->start_controls_section(
            'section_content',
            [
                'label' => esc_html__('فیلتر محصولات', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control('post_count', [
            'label' => esc_html__('تعداد محصول', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::NUMBER,
            'default' => 3,
        ]);

        $this->add_control('order_by', [
            'label' => esc_html__('مرتب کردن بر اساس', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::SELECT,
            'default' => 'date',
            'options' => [
                'date' => esc_html__('تاریخ', 'pezeshkyar'),
                'title' => esc_html__('عنوان', 'pezeshkyar'),
                'comments_count' => esc_html__('تعداد نطرات', 'pezeshkyar'),
                'last_modified' => esc_html__('آخرین ویرایش', 'pezeshkyar'),
                'random' => esc_html__('تصادفی', 'pezeshkyar'),
            ],
        ]);

        $this->add_control('order', [
            'label' => esc_html__('ترتیب', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::SELECT,
            'default' => 'asc',
            'options' => [
                'asc' => esc_html__('صعودی', 'pezeshkyar'),
                'desc' => esc_html__('نزولی', 'pezeshkyar'),
            ],
        ]);

        $this->add_control('date', [
            'label' => esc_html__('تاریخ', 'pezeshkyar'),
            'type' => \Elementor\Controls_Manager::SELECT,
            'default' => 'all',
            'options' => [
                'all' => esc_html__('همه', 'pezeshkyar'),
                'past_day' => esc_html__('روز گذشته', 'pezeshkyar'),
                'past_week' => esc_html__('هفته اخیر', 'pezeshkyar'),
                'past_month' => esc_html__('ماه اخیر', 'pezeshkyar'),
                'past_quarter' => esc_html__('فصل اخیر', 'pezeshkyar'),
                'past_year' => esc_html__('سال اخیر', 'pezeshkyar'),
                'custom' => esc_html__('سفارشی', 'pezeshkyar'),
            ],
        ]);

        $this->add_control(
            'start_time',
            [
                'label' => esc_html__('تاریخ شروع', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DATE_TIME,
                'condition' => [
                    'date' => 'custom',
                ],
            ]
        );

        $this->add_control(
            'end_time',
            [
                'label' => esc_html__('تاریخ پایان', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DATE_TIME,
                'condition' => [
                    'date' => 'custom',
                ],
            ]
        );

        $this->add_control(
            'only_sale',
            [
                'label' => esc_html__('تنها محصولات تخفیف دار نشان داده شود ', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('بله', 'pezeshkyar'),
                'label_off' => esc_html__('خیر', 'pezeshkyar'),
                'return_value' => 'yes',
                'default' => 'no',
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'show_add_to_wishlist',
            [
                'label' => esc_html__('افزودن به علاقه مندی نشان داده شود', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('بله', 'pezeshkyar'),
                'label_off' => esc_html__('خیر', 'pezeshkyar'),
                'return_value' => 'yes',
                'default' => 'no',
                'frontend_available' => true,
            ]
        );

        $this->add_control(
            'show_rating',
            [
                'label' => esc_html__('امتیاز محصول نمایش داده شود', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('بله', 'pezeshkyar'),
                'label_off' => esc_html__('خیر', 'pezeshkyar'),
                'return_value' => 'yes',
                'default' => 'no',
                'frontend_available' => true,
            ]
        );

        $this->end_controls_section();


        $this->start_controls_section(
            'content_style',
            [
                'label' => esc_html__('کارتهای محصول', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' => 'card_border',
                'selector' => '{{WRAPPER}} .pzy-product-card',
            ]
        );
        $this->add_control(
            'product_card_radius',
            [
                'label' => esc_html__('شعاع', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'default' => [
                    'unit' => 'px',
                    'size' => 16,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-card' => 'border-radius: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'bg_color',
            [
                'label' => esc_html__('پس زمینه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-card' => 'background-color: {{VALUE}}',
                ],
            ]
        );

        $this->add_responsive_control(
            'content_align',
            [
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'label' => esc_html__('ترازبندی', 'pezeshkyar'),
                'options' => [
                    'row' => [
                        'title' => esc_html__('Horizontal', 'pezeshkyar'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'column' => [
                        'title' => esc_html__('Vertical', 'pezeshkyar'),
                        'icon' => 'eicon-text-align-center',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-row' => 'display:flex;flex-direction: {{VALUE}};',
                ],]
        );

        $this->add_responsive_control(
            'product_cart_padding',
            [
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'label' => esc_html__('فاصله داخلی', 'pezeshkyar'),
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-card' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

//        $this->add_responsive_control(
//            'slider_padding',
//            [
//                'type' => \Elementor\Controls_Manager::DIMENSIONS,
//                'label' => esc_html__('فاصله داخلی اسلایدر', 'pezeshkyar'),
//                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
//                'selectors' => [
//                    '{{WRAPPER}} .swiper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
//                ],
//            ]
//        );
//        $this->add_responsive_control(
//            'product_image_vertical_shift',
//            [
//                'label' => esc_html__('Vertical Shift', 'pezeshkyar'),
//                'type' => \Elementor\Controls_Manager::SLIDER,
//                'size_units' => ['px'],
//                'default' => [
//                    'unit' => 'px',
//                    'size' => 40,
//                ],
//                'selectors' => [
//                    '{{WRAPPER}} .pzy-product-img:after' => 'margin-top: {{SIZE}}{{UNIT}}',
//                ],
//            ]
//        );
//        $this->add_control(
//            'product_image_mb',
//            [
//                'label' => esc_html__('Image Space From Title', 'pezeshkyar'),
//                'type' => \Elementor\Controls_Manager::SLIDER,
//                'size_units' => ['px'],
//                'default' => [
//                    'unit' => 'px',
//                    'size' => 32,
//                ],
//                'selectors' => [
//                    '{{WRAPPER}} .pzy-product-img' => 'margin-bottom: {{SIZE}}{{UNIT}}',
//                ],
//            ]
//        );

        $this->end_controls_section();

        $this->start_controls_section(
            'image_section_style',
            [
                'label' => esc_html__('تصویر محصول', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_responsive_control(
            'product_image_max_width',
            [
                'label' => esc_html__('عرض', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-img' => 'width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_responsive_control(
            'product_image_padding',
            [
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'label' => esc_html__('فاصله داخلی', 'pezeshkyar'),
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-img' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'show_image_circle',
            [
                'label' => esc_html__('زیر محصول دایره نشان داده شود؟', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('بله', 'pezeshkyar'),
                'label_off' => esc_html__('خیر', 'pezeshkyar'),
                'return_value' => 'yes',
                'default' => '',
            ]
        );
        $this->add_control(
            'product_image_circle_z_index',
            [
                'label' => esc_html__('Z-index', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'condition' => [
                    'show_image_circle' => 'yes',
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => -1,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-img:after' => 'z-index: {{SIZE}};',
                ],
            ]
        );
        $this->add_responsive_control(
            'product_image_circle_position',
            [
                'label' => esc_html__('موقعیت', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'condition' => [
                    'show_image_circle' => 'yes',
                ],
                'default' => [
                    'top' => 27,
                    'right' => 8,
                    'bottom' => -11,
                    'left' => 8,
                    'unit' => 'px',
                    'isLinked' => false,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-img:after' => 'position: absolute; top: {{TOP}}{{UNIT}}; right: {{RIGHT}}{{UNIT}}; bottom: {{BOTTOM}}{{UNIT}}; left: {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_responsive_control(
            'product_image_circle_radius',
            [
                'label' => esc_html__('سعاع', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'condition' => [
                    'show_image_circle' => 'yes',
                ],
                'default' => [
                    'top' => 73,
                    'right' => 73,
                    'bottom' => 73,
                    'left' => 73,
                    'unit' => 'px',
                    'isLinked' => false,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-img:after' => 'content:"";display:inline-block;border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'product_image_circle_color',
            [
                'label' => esc_html__('رنگ پس زمینه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'condition' => [
                    'show_image_circle' => 'yes',
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-img:after' => 'background-color: {{VALUE}}',
                ],
            ]
        );
        $this->end_controls_section();

        $this->start_controls_section(
            'title_section_style',
            [
                'label' => esc_html__('عنوان محصول', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'title',
            [
                'label' => esc_html__('نام محصول', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_responsive_control(
            'product_title_margin',
            [
                'label' => esc_html__('حاشیه خارجی', 'pezeshkyar-core'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'title_text_color',
            [
                'label' => esc_html__('رنگ متن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-card .pzy-product-title,{{WRAPPER}} .pzy-product-card .pzy-product-title a' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'selector' => '{{WRAPPER}} .pzy-product-card .pzy-product-title',
            ]
        );
        $this->add_control(
            'categories',
            [
                'label' => esc_html__('لیست دسته بندی ها', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_responsive_control(
            'product_categories_margin',
            [
                'label' => esc_html__('حاشیه خارجی', 'pezeshkyar-core'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-category' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'categories_text_color',
            [
                'label' => esc_html__('رنگ متن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-card .pzy-product-category' => 'color: {{VALUE}}',
                    '{{WRAPPER}} .pzy-product-card .pzy-product-category a' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'categories_typography',
                'selector' => '{{WRAPPER}} .pzy-product-card .pzy-product-category',
            ]
        );
        $this->end_controls_section();

        $this->start_controls_section(
            'price_section_style',
            [
                'label' => esc_html__('قیمت محصول', 'pezeshkyar'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_responsive_control(
            'price_section_padding',
            [
                'label' => esc_html__('حاشیه خارجی', 'pezeshkyar-core'),
                'type' => \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em', 'rem', 'custom'],
                'selectors' => [
                    '{{WRAPPER}} .pzy-card-bottom' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'discount_badge',
            [
                'label' => esc_html__('درصد تخفیف', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'discount_badge_bg_color',
            [
                'label' => esc_html__('رنگ پس زمینه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-discount' => 'background-color: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'discount_badge_text_color',
            [
                'label' => esc_html__('رنگ متن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-product-discount' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'discount_badge_typography',
                'selector' => '{{WRAPPER}} .pzy-product-discount',
            ]
        );
        $this->add_control(
            'regular_price',
            [
                'label' => esc_html__('قیمت قبل از تخفیف', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'regular_price_display',
            [
                'label' => esc_html__( 'نمایش واحد پولی', 'pezeshkyar' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'none',
                'options' => [
                    'none' => esc_html__( 'مخفی', 'pezeshkyar' ),
                    'inline-block' => esc_html__( 'در متن', 'pezeshkyar' ),
                ],
                'selectors' => [
                    '{{WRAPPER}} .regular-price .woocommerce-Price-currencySymbol' => 'display: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'regular_price_text_color',
            [
                'label' => esc_html__('رنگ متن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .regular-price' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'regular_price_typography',
                'selector' => '{{WRAPPER}} .regular-price',
            ]
        );
        $this->add_control(
            'sale_price',
            [
                'label' => esc_html__('قیمت کنونی', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'sale_price_display',
            [
                'label' => esc_html__( 'نمایش واحد پولی', 'pezeshkyar' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'none',
                'options' => [
                    'none' => esc_html__( 'مخفی', 'pezeshkyar' ),
                    'inline-block' => esc_html__( 'در متن', 'pezeshkyar' ),
                ],
                'selectors' => [
                    '{{WRAPPER}} .sale-price .woocommerce-Price-currencySymbol' => 'display: {{VALUE}};',
                    '{{WRAPPER}} .price-range .woocommerce-Price-currencySymbol' => 'display: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'sale_price_text_color',
            [
                'label' => esc_html__('رنگ متن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sale-price' => 'color: {{VALUE}}',
                    '{{WRAPPER}} .price-range' => 'color: {{VALUE}}',
                ],
            ]
        );
        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'sale_price_typography',
                'selector' => '{{WRAPPER}} .sale-price, {{WRAPPER}} .price-range',
            ]
        );
        $this->add_control(
            'cart_button',
            [
                'label' => esc_html__('دکمه افزودن به سبد خرید', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_control(
            'cart_button_bg_color',
            [
                'label' => esc_html__('رنگ پس زمینه', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-add-to-cart' => 'background-color: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'cart_text_color',
            [
                'label' => esc_html__('رنگ آیکن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .pzy-add-to-cart svg path' => 'stroke: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'size',
            [
                'label' => esc_html__('اندازه آیکن', 'pezeshkyar'),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 30,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 18,
                ],
                'selectors' => [
                    '{{WRAPPER}} .pzy-cart-icon' => 'width: {{SIZE}}{{UNIT}};height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        parent::register_controls();
    }


    protected function render_slides()
    {
        $show_add_to_wishlist = pzy_yith_wc_wishlist_enabled() && $this->get_settings_for_display('show_add_to_wishlist') == 'yes';
        $show_rating = $this->get_settings_for_display('show_rating') == 'yes';

        foreach ($this->items as $product) {
            ?>
            <div class="swiper-slide">
                <article class="pzy-product-card" style="height:100%">
                    <div class="pzy-product-row" style="height:100%">
                        <div class="pzy-product-card-col">
                            <div class="pzy-product-img pzy-position-relative">
                                <?php if ($show_add_to_wishlist): ?>
                                    <div class="pzy-loop-item-wishlist"><?php echo do_shortcode("[yith_wcwl_add_to_wishlist product_id='" . $product->get_id() . "' link_classes='pzy_wishlist']"); ?></div>
                                <?php endif; ?>
                                <a class="pzy-d-block pzy-text-center"
                                   href="<?php echo $product->get_permalink() ?>"><?php echo $product->get_image() ?></a>
                                <?php
                                if ($show_rating && $product->get_rating_count() > 0) {
                                    echo "<div class='pzy-loop-item-rating'><i class='pzy-loop-item-rating-icon' data-feather='star'></i><p class='pzy-loop-item-rating-text'>" . $product->get_average_rating() . "</p></div>";
                                }
                                ?>
                            </div>
                        </div>
                        <div class="pzy-product-card-col" style="flex: 1">
                            <h3 class="pzy-product-title"><a
                                        href="<?php echo $product->get_permalink() ?>"><?php echo $product->get_name(); ?></a>
                            </h3>
                            <div class="pzy-product-category"><?php echo wc_get_product_category_list($product->get_id(), ' | '); ?></div>
                            <div class="pzy-card-bottom">
                                <?php echo get_pzy_price_html($product); ?>
                                <?php
                                    $args = [];
                                    $defaults = array(
                                        'quantity'              => 1,
                                        'class'                 => implode(
                                            ' ',
                                            array_filter(
                                                array(
                                                    'pzy-add-to-cart',
                                                    'button',
                                                    wc_wp_theme_get_element_class_name( 'button' ), // escaped in the template.
                                                    'product_type_' . $product->get_type(),
                                                    $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                                                    $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
                                                )
                                            )
                                        ),
                                        'aria-describedby_text' => $product->add_to_cart_aria_describedby(),
                                        'attributes'            => array(
                                            'data-product_id'  => $product->get_id(),
                                            'data-product_sku' => $product->get_sku(),
                                            'aria-label'       => $product->add_to_cart_description(),
                                            'rel'              => 'nofollow',
                                        ),
                                    );
                            
                                    $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
                            
                                    echo apply_filters(
                                        'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
                                        sprintf(
                                            '<a href="%s" aria-describedby="woocommerce_loop_add_to_cart_link_describedby_%s" data-quantity="%s" class="%s" %s>%s</a>',
                                            esc_url( $product->add_to_cart_url() ),
                                            esc_attr( $product->get_id() ),
                                            esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
                                            esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
                                            isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
                                            '<svg class="pzy-cart-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18"
                                              viewBox="0 0 18 18" fill="none"><path d="M2.25 4.5L4.5 1.5H13.5L15.75 4.5M2.25 4.5V15C2.25 15.3978 2.40804 15.7794 2.68934 16.0607C2.97064 16.342 3.35218 16.5 3.75 16.5H14.25C14.6478 16.5 15.0294 16.342 15.3107 16.0607C15.592 15.7794 15.75 15.3978 15.75 15V4.5M2.25 4.5H15.75M12 7.5C12 8.29565 11.6839 9.05871 11.1213 9.62132C10.5587 10.1839 9.79565 10.5 9 10.5C8.20435 10.5 7.44129 10.1839 6.87868 9.62132C6.31607 9.05871 6 8.29565 6 7.5"
                                              stroke="#F5F6F7" stroke-width="1.5" stroke-linecap="round"
                                              stroke-linejoin="round"/></svg>'
                                        ),
                                        $product,
                                        $args
                                    );
                                ?>
                            </div>
                        </div>
                    </div>
                </article>
            </div>
            <?php
        }
    }

    protected function render()
    {
        global $wpdb;
    
        $settings = $this->get_settings_for_display();
        $only_sale = $this->get_settings_for_display('only_sale') == 'yes';
    
        // Initialize base args
        $args = [
            'post_type'      => 'product',
            'posts_per_page' => $settings['post_count'],
            'orderby'        => $settings['order_by'],
            'order'          => $settings['order'],
            'post_status'    => 'publish', // Only published products
        ];
    
        if ($only_sale) {
            // SQL query to fetch product IDs (simple and variable on sale)
            $query = "
                SELECT DISTINCT p.ID
                FROM {$wpdb->posts} p
                LEFT JOIN {$wpdb->postmeta} pm_simple
                    ON p.ID = pm_simple.post_id
                LEFT JOIN {$wpdb->postmeta} pm_variable
                    ON p.ID = pm_variable.post_id
                LEFT JOIN {$wpdb->posts} v
                    ON v.post_parent = p.ID
                LEFT JOIN {$wpdb->postmeta} vm
                    ON v.ID = vm.post_id
                WHERE p.post_type = 'product'
                    AND p.post_status = 'publish'
                    AND (
                        (pm_simple.meta_key = '_sale_price' AND CAST(pm_simple.meta_value AS DECIMAL) > 0) OR
                        (pm_variable.meta_key = '_min_variation_sale_price' AND CAST(pm_variable.meta_value AS DECIMAL) > 0) OR
                        (v.post_type = 'product_variation' AND vm.meta_key = '_sale_price' AND CAST(vm.meta_value AS DECIMAL) > 0)
                    )
            ";
    
            $on_sale_product_ids = $wpdb->get_col($query);
    
            // Apply filter to include only products on sale
            if (!empty($on_sale_product_ids)) {
                $args['post__in'] = $on_sale_product_ids;
            } else {
                // If no products are on sale, force an empty result
                $args['post__in'] = [0];
            }
        }
    
        // Handle additional filters (e.g., date)
        switch ($settings['date']) {
            case 'past_day':
                $args['date_query'] = ['after' => '1 day ago'];
                break;
            case 'past_week':
                $args['date_query'] = ['after' => '1 week ago'];
                break;
            case 'past_month':
                $args['date_query'] = ['after' => '1 month ago'];
                break;
            case 'past_quarter':
                $args['date_query'] = ['after' => '3 months ago'];
                break;
            case 'past_year':
                $args['date_query'] = ['after' => '1 year ago'];
                break;
            case 'custom':
                $date_query = [];
                if (!empty($settings['start_time'])) {
                    $date_query['after'] = $settings['start_time'];
                }
                if (!empty($settings['end_time'])) {
                    $date_query['before'] = $settings['end_time'];
                }
                if (!empty($date_query)) {
                    $date_query['inclusive'] = true;
                    $args['date_query'] = [$date_query];
                }
                break;
        }
    
        // Adjust 'orderby' parameter
        if ($settings['order_by'] === 'comments_count') {
            $args['orderby'] = 'comment_count';
        } elseif ($settings['order_by'] === 'last_modified') {
            $args['orderby'] = 'modified';
        } elseif ($settings['order_by'] === 'random') {
            $args['orderby'] = 'rand';
        }
    
        // Query posts
        $query = new WP_Query($args);
        $post_ids = wp_list_pluck($query->posts, 'ID');
    
        // Fetch products
        $products = wc_get_products([
            'include' => $post_ids,
            'limit'   => -1,
        ]);
    
        // Sort products to match queried order
        usort($products, function ($a, $b) use ($post_ids) {
            return array_search($a->get_id(), $post_ids) - array_search($b->get_id(), $post_ids);
        });
    
        // Set the products for display
        $this->set_items($products);
    
        if (!empty($products)) {
            ?>
            <div class="pzy-products-slider" style="height:100%">
                <?php parent::render(); ?>
            </div>
            <?php
            wp_reset_postdata();
        }
    }
    
    
}

