<?php
// End recent blog with thumlist for post single page



function enqueue_owl_carousel_homepage($atts) {
    wp_enqueue_script('glorious-slider-script', get_stylesheet_directory_uri() . '/elementor/owl-carousel/js/owl.carousel.min.js', array(
        'jquery'
    ));
    wp_enqueue_style('glorious-slider-style', get_stylesheet_directory_uri() . '/elementor/owl-carousel/css/owl.carousel.min.css');
    wp_enqueue_style('owl-slider-home', get_stylesheet_directory_uri() . '/elementor/owl-carousel/css/owl-testimonials-2.css');

}
add_action('wp_footer', 'enqueue_owl_carousel_homepage');

// Register the widget
add_action('elementor/widgets/widgets_registered', function () {
    class Custom_Widget extends \Elementor\Widget_Base {

        public function get_name() {
            return 'owl-slider-testimonials';
        }

        public function get_title() {
            return __('Custom Testimonials 02', 'text-domain');
        }

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

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

        protected function _register_controls() {
            // Repeater control for dynamic content
            $this->start_controls_section(
                'content_section',
                [
                    'label' => __('Content', 'text-domain'),
                    'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
                ]
            );

            $this->add_control(
                'switch_label',
                [
                    'label' => __('Switch Label', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::SWITCHER,
                    'label_on' => __('Show', 'text-domain'),
                    'label_off' => __('Hide', 'text-domain'),
                    'default' => 'yes', // Set the default value
                    'prefix_class' => 'elementor-switch-label-', // Add a class prefix for styling
                ]
            );


            $repeater = new \Elementor\Repeater();

            // Title field
            $repeater->add_control(
                'title',
                [
                    'label' => __('Title', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::TEXT,
                    'default' => __('Title', 'text-domain'),
                    'label_block' => true,
                ]
            );

            $repeater->add_control(
                'possession',
                [
                    'label' => __('possession', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::TEXT,
                    'default' => __('possession', 'text-domain'),
                    'label_block' => true,
                ]
            );


            // Content field
            $repeater->add_control(
                'content',
                [
                    'label' => __('Content', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::WYSIWYG,
                    'default' => __('Content', 'text-domain'),
                    'show_label' => false,
                ]
            );

            // Image field
            $repeater->add_control(
                'image',
                [
                    'label' => __('Image', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::MEDIA,
                ]
            );


            $this->add_control(
                'items',
                [
                    'type' => \Elementor\Controls_Manager::REPEATER,
                    'fields' => $repeater->get_controls(),
                    'default' => [
                        [
                            'title' => __('Default Title', 'text-domain'),
                            'content' => __('Default Content', 'text-domain'),
                            'image' => '',
                            'button_text' => __('Read More', 'text-domain'),
                            'button_url' => ['url' => 'https://example.com'],
                        ],
                    ],
                    'title_field' => '{{{ title }}}',
                ]
            );

            $this->end_controls_section();

           
           
            $this->start_controls_section(
                'slider_setting',
                [
                    'label' => __('Slider Setting', 'text-domain'),
                    'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
                ]
            );

            $this->add_control(
                'margin',
                [
                    'label' => __('Margin', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::NUMBER,
                    'default' => __('0', 'text-domain'),
                    'show_label' => true,
                ]
            );
            $this->add_control(
                'nav',
                [
                    'label' => esc_html__( 'Nav', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::SWITCHER,
                    'label_on' => esc_html__( 'Yes', 'textdomain' ),
                    'label_off' => esc_html__( 'No', 'textdomain' ),
                    'return_value' => 'yes',
                    'default' => 'yes',
                ]
            );
            $this->add_control(
                'dots',
                [
                    'label' => esc_html__( 'Dots', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::SWITCHER,
                    'label_on' => esc_html__( 'Yes', 'textdomain' ),
                    'label_off' => esc_html__( 'No', 'textdomain' ),
                    'return_value' => 'yes',
                    'default' => 'yes',
                ]
            );
    
            $this->add_control(
                'loop',
                [
                    'label' => esc_html__( 'Loop', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::SWITCHER,
                    'label_on' => esc_html__( 'Yes', 'textdomain' ),
                    'label_off' => esc_html__( 'No', 'textdomain' ),
                    'return_value' => 'yes',
                    'default' => 'yes',
                ]
            );

            $this->add_control(
                'autoplay',
                [
                    'label' => esc_html__( 'Autoplay', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::SWITCHER,
                    'label_on' => esc_html__( 'Yes', 'textdomain' ),
                    'label_off' => esc_html__( 'No', 'textdomain' ),
                    'return_value' => 'yes',
                    'default' => 'yes',
                ]
            );
            $this->add_control(
                'autoplayTimeout',
                [
                    'label' => esc_html__( 'AutoplayTimeout', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::NUMBER,
                    'default' => __('1000', 'text-domain'),
                    'show_label' => true,
                ]
            );

            $this->add_control(
                'rdesktop',
                [
                    'label' => esc_html__( 'Desktop', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::NUMBER,
                    'default' => __('1', 'text-domain'),
                    'show_label' => true,
                ]
            );
            $this->add_control(
                'rtablet',
                [
                    'label' => esc_html__( ' Tablet', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::NUMBER,
                    'default' => __('1', 'text-domain'),
                    'show_label' => true,
                ]
            );

            $this->add_control(
                'rmobile',
                [
                    'label' => esc_html__( 'Mobile', 'textdomain' ),
                    'type' => \Elementor\Controls_Manager::NUMBER,
                    'default' => __('1', 'text-domain'),
                    'show_label' => true,
                ]
            );
    
           
            $this->end_controls_section();

            $this->start_controls_section(
                'title_style_section',
                [
                    'label' => __('Title', 'text-domain'),
                    'tab' => \Elementor\Controls_Manager::TAB_STYLE,
                ]
            );
        
            // Title padding control
            $this->add_responsive_control(
                'title_padding',
                [
                    'label' => __('Padding', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::DIMENSIONS,
                    'size_units' => ['px', '%', 'em'],
                    'selectors' => [
                        '{{WRAPPER}} .testi_name' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );
           
            // Title color control
            $this->add_responsive_control(
                'title_color',
                [
                    'label' => __('Color', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .testi_name' => 'color: {{VALUE}};',
                    ],
                ]
            );
        
            $this->add_group_control(
                \Elementor\Group_Control_Typography::get_type(),
                [
                    'name' => 'sliderTitle_typography',
                    'selector' => '{{WRAPPER}} .testi_name',
                ]
            );



            $this->end_controls_section();


            $this->start_controls_section(
                'img_style_section',
                [
                    'label' => __('Image', 'text-domain'),
                    'tab' => \Elementor\Controls_Manager::TAB_STYLE,
                ]
            );

             //  padding control
             $this->add_responsive_control(
                'img_padding',
                [
                    'label' => __('Padding', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::DIMENSIONS,
                    'size_units' => ['px', '%', 'em'],
                    'selectors' => [
                        '{{WRAPPER}} .owl-img' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );

              //  padding control
              $this->add_responsive_control(
                'img_margin',
                [
                    'label' => __('Margin', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::DIMENSIONS,
                    'size_units' => ['px', '%', 'em'],
                    'selectors' => [
                        '{{WRAPPER}} .owl-img' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );



            $this->end_controls_section();

            $this->start_controls_section(
                'content_style_section',
                [
                    'label' => __('Content', 'text-domain'),
                    'tab' => \Elementor\Controls_Manager::TAB_STYLE,
                ]
            );

             //  padding control
             $this->add_responsive_control(
                'content_padding',
                [
                    'label' => __('Padding', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::DIMENSIONS,
                    'size_units' => ['px', '%', 'em'],
                    'selectors' => [
                        '{{WRAPPER}} .testimonials_content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );

              //  padding control
              $this->add_responsive_control(
                'content_margin',
                [
                    'label' => __('Margin', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::DIMENSIONS,
                    'size_units' => ['px', '%', 'em'],
                    'selectors' => [
                        '{{WRAPPER}} .testimonials_content' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                    ],
                ]
            );

            $this->add_responsive_control(
                'content_color',
                [
                    'label' => __('Color', 'text-domain'),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .testimonials_content' => 'color: {{VALUE}};',
                    ],
                ]
            );
        





            $this->end_controls_section();


            
        }

        protected function render() {
            $settings = $this->get_settings_for_display();
            $margin = $settings['margin'];
            $settings["nav"] = ($settings["nav"] == "yes") ? "true" : "false";
            $settings["dots"] = ($settings["dots"] == "yes") ? "true" : "false";
            $settings["autoplay"] = ($settings["autoplay"] == "yes") ? "true" : "false";
            $settings["loop"] = ($settings["loop"] == "yes") ? "true" : "false";
            $autoplayTimeout = $settings['autoplayTimeout'];
            $rdesktop = $settings['rdesktop'];
            $rtablet = $settings['rtablet'];
            $rmobile = $settings['rmobile'];

        global $slider_id;
        $i=1;
        $j=1;


          echo '<div class="owl-carousel gl_slider_testimonials_01" id="_gl_slider_testimonials_01">';
            foreach ($settings['items'] as $item) {
                ?>
              <div class="item" data-hash="slider-<?php echo $i; ?>">
            <div class="item-box">



                <div class="testimonials_inner">
                    <div class="testimonials_content">
                    <p> <?php echo $item['content']; ?> </p>
                    </div>
                </div>
            </div>
        </div>
                <?php
                $i++;
            }
            echo "</div>";
            echo '<div class="SliderNav">';
            $act = 'act';
            foreach ($settings['items'] as $item) {
                ?>
                
                <div class="dotList <?php echo $act;?>">
                        <a class="fulllink url"  href="#slider-<?php echo $j; ?>"></a>
                        <div class="dotList_inner">
                                <?php if (!empty($item['image']['url'])) : ?>
                                <div class="owl-img">
                                    <img src="<?php echo esc_url($item['image']['url']); ?>" alt="<?php echo esc_attr($item['title']); ?>">
                                </div>
                                <?php endif; ?>
                                <div class="dotContent">
                                           <div class="testimonaislTitle"><?php echo $item['title']; ?></div>  
                                            <?php if($item['possession'])
                                            {
                                            echo '<div class="testi_possession"> '.$item['possession'].' </div>';
                                            }
                                            ?>
                                </div>
                            </div>
                         </div>               
                    
                    <?php
                    $j++;
                    $act ="";
            }
         
            echo "</div>";
            
            ?>
            <script>
jQuery(document).ready(function($) {
            var owl = jQuery('#_gl_slider_testimonials_01');
            owl.owlCarousel({
                URLhashListener:true,
            autoplayHoverPause:true,
        startPosition: 'URLHash',
                stagePadding: 0,
                margin: <?php echo  $settings["margin"]; ?>,
                nav:<?php echo  $settings["nav"]; ?>,
                dots: <?php echo  $settings["dots"]; ?>,
                loop: <?php echo  $settings["loop"]; ?>,
                autoplay:<?php echo  $settings["autoplay"]; ?>,
                autoplayTimeout:1000,
                responsive: {
                    0: { items: <?php echo $rmobile  ?>},
                    600: {items:<?php echo $rtablet  ?>},
                    1000: {items: <?php echo $rdesktop  ?>}
                }
            });
            jQuery(document).ready(function() {
                jQuery('.url').click(function() {
    // Remove 'act' class from all links
    jQuery('.url').removeClass('act');
    
    // Add 'act' class to the clicked link
    jQuery(this).addClass('act');
    
    // Remove class from all dotList elements
    jQuery('.dotList').removeClass('act');
    
    // Add class to parent dotList
    jQuery(this).closest('.dotList').addClass('act');
});


          });
           

    });
</script>


            <?php 
      
        }
    }

    \Elementor\Plugin::instance()->widgets_manager->register_widget_type(new Custom_Widget());
    
});




?>