<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}
global $product;
/**
 * Elementor oEmbed Widget.
 *
 * Elementor widget that inserts an embbedable content into the page, from any given URL.
 *
 * @since 1.0.0
 */
class Elementor_special_product_offer_2 extends \Elementor\Widget_Base {

/**
 * Get widget name.
 *
 * Retrieve oEmbed widget name.
 *
 * @return string Widget name.
 * @since 1.0.0
 * @access public
 */
public function get_name()
{
    return 'hyperkala_special_product_offer_2';
}

/**
 * Get widget title.
 *
 * Retrieve oEmbed widget title.
 *
 * @return string Widget title.
 * @since 1.0.0
 * @access public
 */
public function get_title()
{
    return 'المان پیشنهاد شگفت انگیز';
}

/**
 * Get widget icon.
 *
 * Retrieve oEmbed widget icon.
 *
 * @return string Widget icon.
 * @since 1.0.0
 * @access public
 */
public function get_icon()
{
    return 'eicon-product-upsell';
}

/**
 * Get custom help URL.
 *
 * Retrieve a URL where the user can get more information about the widget.
 *
 * @return string Widget help URL.
 * @since 1.0.0
 * @access public
 */
public function get_custom_help_url()
{
    return 'https://developers.elementor.com/docs/widgets/';
}

/**
 * Get widget categories.
 *
 * Retrieve the list of categories the oEmbed widget belongs to.
 *
 * @return array Widget categories.
 * @since 1.0.0
 * @access public
 */
public function get_categories()
{
    return ['hyperkala'];
}

/**
 * Get widget keywords.
 *
 * Retrieve the list of keywords the oEmbed widget belongs to.
 *
 * @return array Widget keywords.
 * @since 1.0.0
 * @access public
 */
public function get_keywords()
{
    return ['oembed', 'url', 'link'];
}

/**
 * Register oEmbed widget controls.
 *
 * Add input fields to allow the user to customize the widget settings.
 *
 * @since 1.0.0
 * @access protected
 */
    function get_product_cat(){
        $terms = get_terms( array(
            'taxonomy' => 'product_cat',
            'hide_empty' => true,
        ));

        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $options[ $term->term_id ] = $term->name;
            }
            return $options;
        }
    }

    protected function register_controls()
    {
//        $this->start_controls_section(
//            'title_section',
//            [
//                'label' => 'تنظیمات عنوان',
//                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
//            ]
//        );
//        $this->add_control(
//            'product_slider_content_title',
//            [
//                'label' => 'عنوان بخش',
//                'type' => \Elementor\Controls_Manager::TEXT,
//                'default' => '',
//                'placeholder' => 'عنوان بخش را وارد کنید',
//            ]
//        );
//        $this->add_control(
//            'product_slider_icon',
//            [
//                'label' => 'آيکن',
//                'type' => \Elementor\Controls_Manager::ICONS,
//                'default' => [
//                    'value' => 'fas fa-circle',
//                    'library' => 'fa-solid',
//                ],
//            ]
//        );
//        $this->add_control(
//            'product_slider_more_title',
//            [
//                'label' => 'متن دکمه',
//                'type' => \Elementor\Controls_Manager::TEXT,
//                'default' => '',
//                'placeholder' => 'متن روی دکمه مشاهده بیشتر را وارد کنید',
//            ]
//        );
//        $this->add_control(
//            'product_slider_more_link',
//            [
//                'label' => 'لینک دکمه',
//                'type' => \Elementor\Controls_Manager::TEXT,
//                'default' => '',
//                'placeholder' => 'https://your-link.ir',
//            ]
//        );
//        $this->end_controls_section();
        $this->start_controls_section(
            'content_section',
            [
                'label' => 'تنظیمات محتوا',
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'all_categories',
            [
                'label' => 'در نظر گرفتن همه دسته‌ها',
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => 'روشن',
                'label_off' => 'خاموش',
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        $this->add_control(
            'product_slider_select_category',
            [
                'label' => __( 'دسته بندی', 'plugin-domain' ),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'multiple' => true,
                'options' => $this->get_product_cat(),
                'description' =>'اگر چیزی انتخاب نشده باشد المان نمایش داده نمی‌شود'
            ]
        );
        $this->add_control(
            'slider_product_words_title',
            [
                'label' => 'تعداد کلمات عنوان',
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 3,
                'max' => 20,
                'step' => 1,
                'default' => 12,
            ]
        );
        $this->add_control(
            'spo2_width',
            [
                'label' => 'عرض جعبه نمایش',
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 300,
                'max' => 1280,
                'step' => 100,
                'default' => 300,
            ]
        );
        $this->end_controls_section();
        $this->start_controls_section(
            'product_slider_settings_section',
            [
                'label' => 'تنظیمات اسلایدر',
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'slider_product_count_mobile',
            [
                'label' => 'تعداد نمایش اسلاید در گوشی',
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 4,
                'step' => 1,
                'default' => 1,
            ]
        );
        $this->add_control(
            'slider_product_count_tablet',
            [
                'label' => 'تعداد نمایش اسلاید در تبلت',
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 4,
                'step' => 1,
                'default' => 1,
            ]
        );
        $this->add_control(
            'slider_product_count_desktop',
            [
                'label' => 'تعداد نمایش اسلاید در دسکتاپ',
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 6,
                'step' => 1,
                'default' => 1,
            ]
        );
        $this->add_control(
            'slider_product_count_desktop',
            [
                'label' => 'تعداد نمایش اسلاید در دسکتاپ',
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 2,
                'max' => 8,
                'step' => 1,
                'default' => 4,
            ]
        );
        $this->add_control(
            'slider_product_loop',
            [
                'label' => 'چرخش حلقه',
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => 'روشن',
                'label_off' => 'خاموش',
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        $this->add_control(
            'slider_product_autoplay',
            [
                'label' => 'حرکت خودکار',
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => 'روشن',
                'label_off' => 'خاموش',
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        $this->add_control(
            'slider_product_dots',
            [
                'label' => 'نقطه‌ها',
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => 'روشن',
                'label_off' => 'خاموش',
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        $this->add_control(
            'product_slide_speed',
            [
                'label' => __( 'سرعت حرکت اسلایدها', 'plugin-domain' ),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'min' => 200,
                'max' => 10000,
                'step' => 1,
                'default' => 5000,
            ]
        );

        $this->end_controls_section();
        $this->start_controls_section(
            'product_slider_style_section',
            [
                'label' => 'تنظیمات کلی',
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );
        $this->add_control(
            'special-box1-color',
            [
                'label' => 'رنگ پس زمینه جعبه CTA (طرح ۱)',
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-element-model1 a' => 'background-color: {{VALUE}}',
                ],
            ]
        );
        $this->add_control(
            'special-box-color',
            [
                'label' => 'رنگ پس زمینه جعبه CTA (طرح ۲)',
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .special-box ul li' => 'background-color: {{VALUE}}',
                ],
            ]
        );
        $this->end_controls_section();
    }

    /**
     * Render oEmbed widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since 1.0.0
     * @access protected
     */

    protected function render() {
        $settings = $this->get_settings_for_display();

        ?>
            <style>
                .spo2-offer-width{
                    max-width:<?php echo $settings['spo2_width'];?>px;
                }
            </style>
        <script>
            $(function (){
                $('.special-product-offer2-element').owlCarousel({
                    loop:<?php if ($settings['slider_product_loop']) {echo "true"; } else {echo "false"; } ?>,
                    margin:10,
                    nav:true,
                    dots:<?php if ($settings['slider_product_dots']) {echo "true"; } else {echo "false"; } ?>,
                    autoplay:<?php if ($settings['slider_product_autoplay']) {echo "true"; } else {echo "false"; } ?>,
                    autoplayTimeout:<?php echo $settings['product_slide_speed']; ?>,
                    autoplayHoverPause: true,
                    responsive:{
                        0:{
                            items:<?php echo $settings['slider_product_count_mobile']; ?>
                        },
                        600:{
                            items:<?php echo $settings['slider_product_count_tablet']; ?>
                        },
                        1000:{
                            items:<?php echo $settings['slider_product_count_desktop']; ?>
                        }
                    }
                });
            });
        </script>
            <div class="special-product-offer2 spo2-offer-width">
                <?php if ($settings['product_slider_content_title']){?>
                    <div class="title-section">
                        <div class="title-item">
                            <a href="<?php echo $settings['product_slider_more_link'];?>"><i class="fas fa-arrow-left"></i>
                                <span><?php echo $settings['product_slider_more_title'];?></span></a>
                        </div>
                        <div class="title-item">
                            <span><?php echo $settings['product_slider_content_title'];?></span>
                            <?php \Elementor\Icons_Manager::render_icon( $settings['product_slider_icon'], [ 'aria-hidden' => 'true' ] ); ?>
                        </div>
                    </div>
                <?php }?>
                <div id="special-product-offer2-element" class="owl-carousel owl-theme special-product-offer2-element">
                    <?php
                    if ($settings['all_categories']){
                        $pro = new WP_Query(array(
                            'post_type' => 'product',
                            'posts_per_page' => 8,
                            'meta_query' => array(
                                array(
                                    'key' => '_stock_status',
                                    'value' => 'instock'
                                ),
                                'meta_query' => WC()->query->get_meta_query(),
                                'post__in' => array_merge(array(0), wc_get_product_ids_on_sale())
                            )
                        ));
                    }else{
                        $pro = new WP_Query(array(
                            'post_type' => 'product',
                            'posts_per_page' => 8,
                            'tax_query' => [[
                                'taxonomy'=> 'product_cat',
                                'field' => 'term_id',
                                'terms'=> $settings['product_slider_select_category'],
                            ]],
                            'meta_query' => array(
                                array(
                                    'key' => '_stock_status',
                                    'value' => 'instock'
                                ),
                                'meta_query' => WC()->query->get_meta_query(),
                                'post__in' => array_merge(array(0), wc_get_product_ids_on_sale())
                            )
                        ));
                    }
                    if($pro->have_posts()) {
                        while ($pro->have_posts()) : $pro->the_post(); ?>
                            <?php global $product;
                            global $theme_options;
                            $terms = get_the_terms($product->ID, 'product_cat');
                            if ( $product->is_on_sale() ) {

                                if ( ! $product->is_type( 'variable' ) ) {

                                    $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;

                                } else {

                                    $max_percentage = 0;

                                    foreach ( $product->get_children() as $child_id ) {
                                        $variation = wc_get_product( $child_id );
                                        $price = $variation->get_regular_price();
                                        $sale = $variation->get_sale_price();
                                        if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
                                        if ( $percentage > $max_percentage ) {
                                            $max_percentage = $percentage;
                                        }
                                    }

                                }}
                            $slugs = array();
                            foreach ($terms as $termsit){
                                $slugs[] = $termsit->slug;
                            }
                            $product_slug = $slugs[0];
                            ?>
                            <div class="item">
                                <div class="spo2-offer-show-box"><p class="spo2-offer-show">پیشنهاد شگفت انگیز</p></div>
                                <a href="<?php echo get_the_permalink();?>" class="spo2-offer-img"><?php the_post_thumbnail('index-main-pro-slider');?></a>
                                <a href="<?php echo get_the_permalink();?>"><p class="spo2-title"><?php echo wp_trim_words(get_the_title(),$settings['slider_product_words_title']);?></p></a>
                                    <?php if($product->is_in_stock()) {?>
                                        <?php if ($product->is_on_sale()){?>
                                            <div class="spo2-price-range">
<!--                                                --><?php //if($max_percentage==0){}elseif($max_percentage>0){ echo "<span class='sale-discount-percent'>" . round($max_percentage) . "%</span>";} ?>
                                                <?php echo $product->get_price_html(); ?>
                                            </div>
                                        <?php }?>
                                    <?php }?>
                            </div>
                        <?php
                        endwhile;
                    }
                    wp_reset_postdata();
                    ?>
                </div>
            </div>

        <?php
    }

}
