<?php
/**
 * Custom widgets.
 *
 * @package OMag
 */

if ( ! class_exists( 'OMag_News_Style_One' ) ) :

/**
* OMag News Style One widget
*
* @since 1.0.0
*/
class OMag_News_Style_One extends WP_Widget {

	function __construct() {
		$opts = array(
			'classname'   => 'home-news-widget',
			'description' => esc_html__( 'OMag News Style One Widget', 'omag' ),
		);

		parent::__construct( 'omag-news-style-one', esc_html__( 'OMag: News Style One', 'omag' ), $opts );
	}

	function widget( $args, $instance ) {

		$theme_options = omag_theme_options();

		$title 			= apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

		$style_one_cat 	= ! empty( $instance['style_one_cat'] ) ? $instance['style_one_cat'] : 0;

		$view_all_txt	= !empty( $instance['view_all_txt'] ) ? $instance['view_all_txt'] : '';

		$excerpt_length = !empty( $instance['excerpt_length'] ) ? $instance['excerpt_length'] : 20;

		echo $args['before_widget']; ?>

		<div class="article-title-wrap">

			<?php 

			if ( ! empty( $title ) ) {
				echo $args['before_title'] . esc_html( $title ). $args['after_title'];
			} else {
				$default_title = 'Style 1';
				echo $args['before_title'] . esc_html( $default_title ). $args['after_title'];
			}

			if( ! empty( $view_all_txt )  && absint( $style_one_cat ) > 0 ){

				if( absint( $style_one_cat ) > 0 ){

					$cat_link = get_category_link( $style_one_cat );

				}else{

					$cat_link = '';

				} ?>

				<span class="view-all-link"> <a href="<?php echo esc_url( $cat_link ); ?>"><?php echo esc_html( $view_all_txt ); ?></a> </span>

				<?php

			} ?>

		</div>

		<?php

		if(!empty( $style_one_cat )) {

			$style_one_args = array( 
				'cat'             => absint( $style_one_cat ), 
				'post_status'     => 'publish', 
				'posts_per_page'  => 5,
				'ignore_sticky_posts' => 1,
			);

		} else{

			$style_one_args = array( 'post_status' => 'publish', 'posts_per_page' => 5, 'ignore_sticky_posts' => 1, );

		} 

		$style_one_query = new WP_Query( $style_one_args ); 

		if ( $style_one_query->have_posts() ) {
			?>

			<div class="row">

				<?php

				$style_once_count = 1;

				while ( $style_one_query->have_posts() ) : $style_one_query->the_post(); 

					$image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );

					if( 1 === $style_once_count) {

						?>

						<div class="col-lg-6 col-md-6">
							<article class="post type4">
								<?php if(!empty($image_url)) { ?>
								<figure>
									<a href="<?php the_permalink(); ?>" class="news-image">
										<img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php the_title(); ?>">
									</a>
									<?php if( 1 === $theme_options['enable_post_tags']) {
										omag_posts_tag();
									} ?>
								</figure>
								<?php } ?>
								<div class="article-content">
									<div class="entry-header">
										<h4>
											<a href="<?php the_permalink(); ?>">
												<?php the_title(); ?>
											</a>
										</h4>
										<div class="banner-info">
											<?php
											if( 1 === $theme_options['enable_post_author']) {
												omag_posted_by();
											}
											?>
											<?php
											if( 1 === $theme_options['enable_post_category']) { ?>

											<span>-</span>
											<?php omag_posts_category(); ?>

											<?php } ?>
										</div>
										<div class="entry-meta">
											<?php
											if( 1 === $theme_options['enable_post_date']) {
												omag_posted_on();
											}
											?>

											<?php
											if( 1 === $theme_options['enable_comments_count']) {
												echo '<span class="comments-link">';
												echo '<i class="far fa-comments"></i>';
												comments_popup_link( esc_html__( 'No Comments', 'omag' ), esc_html__( '1 Comment', 'omag' ), '% '. esc_html__( 'Comments', 'omag' ), 'post-comments');
												echo '</span>';
											}
											?>
										</div>
									</div>
									<div class="entry-content">
										<?php 

										$style_1_content = omag_get_the_excerpt( absint( $excerpt_length ) );

										echo wp_kses_post($style_1_content) ? wpautop( wp_kses_post($style_1_content) ) : '';

										?>
									</div>
								</div>
							</article>
						</div>

						<?php } else { 

							if( 2 === $style_once_count ) {
								?>

								<div class="col-lg-6 col-md-6">

									<?php } ?>

									<article class="post type4 secondary">
										<?php if(!empty($image_url)) { ?>
										<figure class="float-left">
											<a href="<?php the_permalink(); ?>" class="news-image">
												<img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php the_title(); ?>">
											</a>
										</figure>

										<?php
										}
											$article_mini_cls = '';
											if(empty($image_url)){
												$article_mini_cls = 'article-mini-full';
											}
										?>

										<div class="article-content float-right <?php echo esc_attr( $article_mini_cls ); ?>">
											<div class="entry-header">
												<h4>
													<a href="<?php the_permalink(); ?>">
														<?php the_title(); ?>
													</a>
												</h4>
												<div class="banner-info">
													<?php
								                      if( 1 === $theme_options['enable_post_date']) {
								                        omag_posted_on();
								                      }
								                    ?>
													</div>
												</div>
											</div>
											<div class="clearfix"></div>
										</article>

										<?php if( 5 === $style_once_count ) { ?>
									</div>


									<?php 
								}
							}
							$style_once_count++;
						endwhile;
						wp_reset_postdata(); 
						if( $style_once_count === 3 || $style_once_count === 4 || $style_once_count === 5 ) {
							echo '</div>';
						}
						?>

					</div>

					<?php
				}
				echo $args['after_widget'];

			}

			function update( $new_instance, $old_instance ) {
				$instance                    = $old_instance;
				$instance['title']           = sanitize_text_field( $new_instance['title'] );
				$instance['style_one_cat']    = absint( $new_instance['style_one_cat'] );
				$instance['view_all_txt']   = sanitize_text_field( $new_instance['view_all_txt'] );
				$instance['excerpt_length']  = absint( $new_instance['excerpt_length'] );

				return $instance;
			}

			function form( $instance ) {

				$instance = wp_parse_args( (array) $instance, array(
					'title'         	=> '',
					'style_one_cat'  	=> '',
					'view_all_txt'  	=> '',
					'excerpt_length'    => 20,

				) );
				?>

				<p>
					<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Layout', 'omag' ); ?></label>
					<img src="<?php echo get_template_directory_uri(); ?>/inc/widgets/images/omag-news-style-one.jpg" class="layout-img">
				</p>

				<p>
					<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'omag' ); ?></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( $instance['title'] ); ?>" />
				</p>

				<p>
					<label for="<?php echo  esc_attr( $this->get_field_id( 'style_one_cat' ) ); ?>"><strong><?php esc_html_e( 'Category:', 'omag' ); ?></strong></label>
					<?php
					$cat_args = array(
						'orderby'         => 'name',
						'hide_empty'      => 0,
						'class' 		  => 'widefat',
						'taxonomy'        => 'category',
						'name'            => $this->get_field_name( 'style_one_cat' ),
						'id'              => $this->get_field_id( 'style_one_cat' ),
						'selected'        => absint( $instance['style_one_cat'] ),
						'show_option_all' => esc_html__( 'All Categories','omag' ),
					);
					wp_dropdown_categories( $cat_args );
					?>
				</p>

				<p>
					<label for="<?php echo esc_attr( $this->get_field_id( 'view_all_txt' ) ); ?>"><strong><?php esc_html_e( 'View All Text:', 'omag' ); ?></strong></label>
					<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'view_all_txt' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'view_all_txt' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['view_all_txt'] ); ?>" />
					<small>
						<?php esc_html_e('Leave this field empty if you want to hide it.', 'omag'); ?>	
					</small>
				</p>

				<p>
					<label for="<?php echo esc_attr( $this->get_field_name('excerpt_length') ); ?>">
						<?php esc_html_e('Excerpt Length:', 'omag'); ?>
					</label>
					<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('excerpt_length') ); ?>" name="<?php echo esc_attr( $this->get_field_name('excerpt_length') ); ?>" type="number" value="<?php echo absint( $instance['excerpt_length'] ); ?>" />
				</p>

			<?php
		}

	}

endif;
