<?php
// Register and load the widget
function oro_cat_slider_widget() {
    register_widget( 'oro_cat_slider' );
}
add_action( 'widgets_init', 'oro_cat_slider_widget' );


class oro_cat_slider extends WP_Widget {

    function __construct() {
        parent::__construct(
            'oro_cat_slider',
            esc_html__('ORO - Category Slider', 'oro'),
            array( 'description' => esc_html__( 'Display posts from a particular category in the form of a Slider.', 'oro' ), )
        );
    }

    public function widget( $args, $instance ) {
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
        $post_count = isset( $instance['post_count'] ) ? $instance['post_count'] : 4;
        $category_slider = isset( $instance['category_slider'] ) ? $instance['category_slider'] : 0;
        
        echo $args['before_widget'];
        if ( ! empty( $title ) )
            echo $args['before_title'] . $title . $args['after_title'];
        ?>

        <?php
        $widget_args = array(
            'cat' => $category_slider,
            'ignore_sticky_posts' => true,
            'posts_per_page' => $post_count
        );
        
        $widget_query = new WP_Query($widget_args);
        
        if ( $widget_query->have_posts() ) : ?>
            <div class="swiper-container cat-slider">
                <div class="swiper-wrapper">
                    <?php while ( $widget_query->have_posts() ) : $widget_query->the_post(); ?>
                        <div class="swiper-slide">
                            <div class="slide-image">
                                <?php if ( has_post_thumbnail() ): ?>
                                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('oro_slide'); ?></a>
                                    <div class="overlay"></div> <!-- Add this line for the overlay -->
                                <?php else : ?>
                                    <a href="<?php the_permalink(); ?>"><img class="wp-post-image" src="<?php echo esc_url(get_template_directory_uri() . '/assets/images/ph-slide-cats.svg'); ?>"></a>
                                <?php endif; ?>
                            </div>
                            <div class="slide-title">
                                <?php the_title( '<h3 class="entry-title"><a href="' . esc_url( get_permalink() ) . '">', '</a></h3>' ); ?>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>
                <!-- Add Pagination -->
                <div class="swiper-pagination"></div>
                <!-- Add Navigation -->
                <div class="swiper-button-next"></div>
                <div class="swiper-button-prev"></div>
            </div>
        <?php
        endif;
        wp_reset_postdata();
        
        echo $args['after_widget'];
    }

    public function form( $instance ) {
        // Widget form fields
        // ... (No changes needed here)
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['post_count'] = ( ! empty( $new_instance['post_count'] ) ) ? (int)$new_instance['post_count'] : 4;
        $instance['category_slider'] = ( ! empty( $new_instance['category_slider'] ) ) ? (int)$new_instance['category_slider'] : 0;
        return $instance;
    }
}
