<?php

namespace Elementor;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class multiblog_elementor_slider_blog extends Widget_Base {
    
    public function get_name() {
        return 'multiblog-slider-blog';
    }
    
    public function get_title() {
        return __( 'Overlay Slider Blog', 'multiblog' );
    }
    
    public function get_icon() {
        return 'eicon-call-to-action';
    }
    
    public function get_categories() {
        return [ 'multiblog-elementor-widget' ];
    }
    
    protected function register_controls() {
        $this->start_controls_section(
            'multiblog_blog_section',
            [
                'label' => esc_html__('Blog Options', 'multiblog'),
            ]
        );
        
        $this->add_control(
            'multiblog_blog_get_category',
            [
                'label' => __('Select Category', 'multiblog'),
                'type' => Controls_Manager::SELECT2,
                'default' => [],
                'options' => multiblog_blog_get_category(),
                'multiple' => true
            ]
        );
        
        $this->add_control(
            'multiblog_blog_post_count',
            [
                'label' => __('No. Of Posts (Max 3 Posts in Free version)', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 3,
                'min' => 1,
                'max' => 3,
            ]
        );

        $this->add_control(
            'multiblog_blog_post_offset',
            [
                'label' => __('Post Offset', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 0,
                'description' => __('Number of posts to skip', 'multiblog'),
            ]
        );

        $this->add_control(
            'blog_height',
            [
                'label' => __('Blog Height (px)', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 450,
                'description' => __('Enter height in pixels (e.g. 450)', 'multiblog'),
                'selectors' => [
                    '{{WRAPPER}} .blog-post' => 'height: {{VALUE}}px;',
                ],
            ]
        );

        $this->add_control(
            'show_title',
            [
                'label' => esc_html__( 'Show Post Excerpt', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_author_meta',
            [
                'label' => esc_html__( 'Show Author Meta', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_date_meta',
            [
                'label' => esc_html__( 'Show Date Meta', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->end_controls_section();
        
        $this->start_controls_section(
            'multiblog_slider_options',
            [
                'label' => esc_html__('Slider Options', 'multiblog'),
            ]
        );
        
        $this->add_control(
            'autoplay',
            [
                'label' => esc_html__( 'Autoplay', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Yes', 'multiblog' ),
                'label_off' => esc_html__( 'No', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        
        $this->add_control(
            'show_arrows',
            [
                'label' => esc_html__( 'Show Arrows', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Yes', 'multiblog' ),
                'label_off' => esc_html__( 'No', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );
        
        $this->add_control(
            'pause_on_hover',
            [
                'label' => esc_html__( 'Pause on Hover', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Yes', 'multiblog' ),
                'label_off' => esc_html__( 'No', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );
        
        $this->add_control(
            'slides_to_show',
            [
                'label' => __('Slides to Show (Desktop)', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 1,
                'min' => 1,
                'max' => 3,
                'step' => 1,
            ]
        );
        
        $this->add_control(
            'slides_to_show_tablet',
            [
                'label' => __('Slides to Show (Tablet)', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 1,
                'min' => 1,
                'max' => 3,
                'step' => 1,
                'separator' => 'before',
            ]
        );
        
        $this->add_control(
            'slides_to_show_mobile',
            [
                'label' => __('Slides to Show (Mobile)', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 1,
                'min' => 1,
                'max' => 3,
                'step' => 1,
            ]
        );
        
        $this->add_control(
            'slider_gap',
            [
                'label' => __('Slider Gap (px)', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 0,
                'min' => 0,
                'max' => 100,
                'step' => 1,
                'description' => __('Space between slides in pixels', 'multiblog'),
                'selectors' => [
                    '{{WRAPPER}} .custom-blog-widget.slider-layout .blog-post' => 'margin-right: {{VALUE}}px;',
                ],
            ]
        );
        
        $this->end_controls_section();

        $this->start_controls_section(
            'multiblog_heading_style',
            [
                'label' => esc_html__('Heading Style', 'multiblog'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'heading_typography',
                'label' => __('Typography', 'multiblog'),
                'selector' => '{{WRAPPER}} .custom-blog-widget h3',
            ]
        );
        
        $this->add_control(
            'heading_color',
            [
                'label' => __('Heading Color', 'multiblog'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-blog-widget h3.entry-title a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'multiblog_excerpt_style',
            [
                'label' => esc_html__('Excerpt Style', 'multiblog'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'excerpt_typography',
                'label' => __('Typography', 'multiblog'),
                'selector' => '{{WRAPPER}} .post-excerpt',
            ]
        );
         
        $this->add_control(
            'excerpt_color',
            [
                'label' => __('Excerpt Color', 'multiblog'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-blog-widget p.post-excerpt' => 'color: {{VALUE}};',
                ],
            ]
        );       

        $this->end_controls_section();
    }
    
    protected function render() {
        $settings = $this->get_settings_for_display();
        
        // Set default values if not exists
        $taxs = isset($settings['multiblog_blog_get_category']) ? $settings['multiblog_blog_get_category'] : [];
        $posts_count = isset($settings['multiblog_blog_post_count']) ? $settings['multiblog_blog_post_count'] : 3;
        $post_offset = isset($settings['multiblog_blog_post_offset']) ? $settings['multiblog_blog_post_offset'] : 0;
        $blog_height = isset($settings['blog_height']) ? $settings['blog_height'] : 450;
        
        // Get slider options with defaults
        $autoplay = isset($settings['autoplay']) && $settings['autoplay'] === 'yes';
        $arrows = isset($settings['show_arrows']) && $settings['show_arrows'] === 'yes';
        $pause_on_hover = isset($settings['pause_on_hover']) && $settings['pause_on_hover'] === 'yes';
        $slides_to_show = isset($settings['slides_to_show']) ? $settings['slides_to_show'] : 1;
        $slides_to_show_tablet = isset($settings['slides_to_show_tablet']) ? $settings['slides_to_show_tablet'] : 1;
        $slides_to_show_mobile = isset($settings['slides_to_show_mobile']) ? $settings['slides_to_show_mobile'] : 1;
        $slider_gap = isset($settings['slider_gap']) ? $settings['slider_gap'] : 0;

        $loop = ($posts_count <= 0) ? 3 : $posts_count;
        
        $args = [
            'post_type' => 'post',
            'posts_per_page' => esc_attr($loop),
            'offset' => esc_attr($post_offset),
            'post_status' => 'publish',
            'orderby' => 'menu_order date',
            'order' => 'desc',
        ];

        if (!empty($taxs)) {
            $args['tax_query'] = [
                [
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $taxs,
                ]
            ];
        }

        $query = new \WP_Query($args);

        if ($query->have_posts()): ?>
            <div class="custom-blog-widget list-layout slider-layout overlay-blog">
                <?php while ($query->have_posts()) : $query->the_post();
                    global $post;
                    $post_thumbnail_id = get_post_thumbnail_id(get_the_ID());
                    $image = wp_get_attachment_image_src($post_thumbnail_id, 'large');
                    $image_style = !empty($image) ? "style='background-image:url(" . esc_url($image[0]) . ")'" : '';
                    
                    if($loop >= 1) : ?>
                        <article class="blog-post" <?php echo wp_kses_post($image_style); ?>>
                            <div class="post-content">
                                <h3 class="entry-title"><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php the_title(); ?></a></h3>
                                <?php if (isset($settings['show_title']) && $settings['show_title'] === 'yes') : ?>
                                    <p class="post-excerpt"><?php echo wp_kses_post(multiblog_get_excerpt(get_the_ID(), 125)); ?></p>
                                <?php endif; ?>                    

                                <?php
                                if (isset($settings['show_author_meta']) && $settings['show_author_meta'] === 'yes') {
                                    $archive_year = get_the_time('Y');
                                    $archive_month = get_the_time('m');
                                    echo '<div class="entry-meta"><div class="author">
                                    <a href="'.esc_url(get_author_posts_url(get_the_author_meta('ID'))).'" title="'.esc_attr(get_the_author()).'">
                                    <span>'.get_the_author().'</span></a></div>';
                                    
                                    if (isset($settings['show_date_meta']) && $settings['show_date_meta'] === 'yes') { 
                                        echo '<div class="date"><a href="'.esc_url(get_month_link($archive_year,$archive_month)).'">
                                        <span>'.esc_html(get_the_date()).'</span></a></div></div>'; 
                                    } else {
                                        echo '</div>';
                                    }
                                }
                                ?>
                            </div>
                        </article>
                    <?php 
                    $loop--;
                    endif; 
                endwhile;
                wp_reset_postdata(); ?>
            </div>

            <script type="text/javascript">
                jQuery(document).ready(function($) {
                    $('.custom-blog-widget.slider-layout').slick({
                        infinite: true,
                        autoplay: <?php echo $autoplay ? 'true' : 'false'; ?>,
                        arrows: <?php echo $arrows ? 'true' : 'false'; ?>,
                        slidesToShow: <?php echo esc_js($slides_to_show); ?>,
                        pauseOnHover: <?php echo $pause_on_hover ? 'true' : 'false'; ?>,
                        centerMode: false,
                        slidesToScroll: 1,
                        cssEase: 'ease',
                        speed: 500,
                        autoplaySpeed: 4000,
                        variableWidth: false,
                        adaptiveHeight: true,
                        responsive: [
                            {
                                breakpoint: 1024,
                                settings: {
                                    slidesToShow: <?php echo esc_js($slides_to_show_tablet); ?>,
                                    slidesToScroll: 1
                                }
                            },
                            {
                                breakpoint: 767,
                                settings: {
                                    slidesToShow: <?php echo esc_js($slides_to_show_mobile); ?>,
                                    slidesToScroll: 1
                                }
                            }
                        ]
                    });
                });
            </script> 
        <?php endif;
    }
}

Plugin::instance()->widgets_manager->register_widget_type(new multiblog_elementor_slider_blog());