<?php
if ( ! class_exists( 'Mindful_Blog_Trending_Posts_Widget' ) ) {
	/**
	 * Adds Mindful_Blog_Trending_Posts_Widget Widget.
	 */
	class Mindful_Blog_Trending_Posts_Widget extends WP_Widget {

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$mindful_blog_trending_posts_widget_ops = array(
				'classname'   => 'ascendoor-widget mindful-blog-trending-carousel-section',
				'description' => __( 'Retrive Trending Posts Widgets', 'mindful-blog' ),
			);
			parent::__construct(
				'mindful_blog_trending_posts_carousel_widget',
				__( 'Ascendoor Trending Posts Widget', 'mindful-blog' ),
				$mindful_blog_trending_posts_widget_ops
			);
		}

		/**
		 * Front-end display of widget.
		 *
		 * @see WP_Widget::widget()
		 *
		 * @param array $args     Widget arguments.
		 * @param array $instance Saved values from database.
		 */
		public function widget( $args, $instance ) {
			if ( ! isset( $args['widget_id'] ) ) {
				$args['widget_id'] = $this->id;
			}
			$mindful_blog_trending_posts_title    = ( ! empty( $instance['title'] ) ) ? ( $instance['title'] ) : '';
			$mindful_blog_trending_posts_title    = apply_filters( 'widget_title', $mindful_blog_trending_posts_title, $instance, $this->id_base );
			$mindful_blog_trending_posts_offset   = isset( $instance['offset'] ) ? absint( $instance['offset'] ) : '';
			$mindful_blog_trending_posts_category = isset( $instance['category'] ) ? absint( $instance['category'] ) : '';

			echo $args['before_widget'];
			?>
			<?php if ( ! empty( $mindful_blog_trending_posts_title ) ) { ?>	
				<div class="section-header">
					<?php
					echo $args['before_title'] . esc_html( $mindful_blog_trending_posts_title ) . $args['after_title'];
					?>
				</div>
			<?php } ?>
			<div class="mindful-blog-section-body">
				<div class="mindful-blog-trending-carousel-section-wrapper trending-carousel">
					<?php
					$mindful_blog_trending_posts_widgets_args = array(
						'post_type'      => 'post',
						'posts_per_page' => absint( 4 ),
						'offset'         => absint( $mindful_blog_trending_posts_offset ),
						'cat'            => absint( $mindful_blog_trending_posts_category ),
					);

					$mindful_blog_query = new WP_Query( $mindful_blog_trending_posts_widgets_args );
					if ( $mindful_blog_query->have_posts() ) :
						while ( $mindful_blog_query->have_posts() ) :
							$mindful_blog_query->the_post();
							?>
							<div class="carousel-item">
								<div class="mag-post-single has-image list-design">
									<?php if ( has_post_thumbnail() ) { ?>
										<div class="mag-post-img">
											<a href="<?php the_permalink(); ?>">
												<?php the_post_thumbnail(); ?>
											</a>
										</div>
									<?php } ?>
									<div class="mag-post-detail">
										<h3 class="mag-post-title">
											<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
										</h3>
										<div class="mag-post-meta">
											<span class="post-author">
												<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><i class="fas fa-user"></i><?php echo esc_html( get_the_author() ); ?></a>
											</span>
											<span class="post-date">
												<a href="<?php the_permalink(); ?>"><i class="far fa-clock"></i><?php echo esc_html( get_the_date() ); ?></a>
											</span>
										</div>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
					endif;
					?>
				</div>
			</div>
			<?php
			echo $args['after_widget'];
		}

		/**
		 * Back-end widget form.
		 *
		 * @see WP_Widget::form()
		 *
		 * @param array $instance Previously saved values from database.
		 */
		public function form( $instance ) {
			$mindful_blog_trending_posts_title    = isset( $instance['title'] ) ? ( $instance['title'] ) : '';
			$mindful_blog_trending_posts_offset   = isset( $instance['offset'] ) ? absint( $instance['offset'] ) : '';
			$mindful_blog_trending_posts_category = isset( $instance['category'] ) ? absint( $instance['category'] ) : '';
			?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Section Title:', 'mindful-blog' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $mindful_blog_trending_posts_title ); ?>" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>"><?php esc_html_e( 'Number of posts to displace or pass over:', 'mindful-blog' ); ?></label>
				<input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'offset' ) ); ?>" type="number" step="1" min="0" value="<?php echo absint( $mindful_blog_trending_posts_offset ); ?>" size="3" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>"><?php esc_html_e( 'Select the category to show posts:', 'mindful-blog' ); ?></label>
				<select id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'category' ) ); ?>" class="widefat" style="width:100%;">
					<?php
					$mindful_blog_categories = mindful_blog_get_post_cat_choices();
					foreach ( $mindful_blog_categories as $mindful_blog_category => $mindful_blog_value ) {
						?>
						<option value="<?php echo absint( $mindful_blog_category ); ?>" <?php selected( $mindful_blog_trending_posts_category, $mindful_blog_category ); ?>><?php echo esc_html( $mindful_blog_value ); ?></option>
						<?php
					}
					?>
				</select>
			</p>
			<?php
		}

		/**
		 * Sanitize widget form values as they are saved.
		 *
		 * @see WP_Widget::update()
		 *
		 * @param array $new_instance Values just sent to be saved.
		 * @param array $old_instance Previously saved values from database.
		 *
		 * @return array Updated safe values to be saved.
		 */
		public function update( $new_instance, $old_instance ) {
			$instance             = $old_instance;
			$instance['title']    = sanitize_text_field( $new_instance['title'] );
			$instance['offset']   = (int) $new_instance['offset'];
			$instance['category'] = (int) $new_instance['category'];
			return $instance;
		}

	}
}
