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

if ( ! class_exists( 'OMag_News_Style_Two' ) ) :

/**
* OMag News Style Two widget
*
* @since 1.0.0
*/
class OMag_News_Style_Two extends WP_Widget {

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

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


	function widget( $args, $instance ) {

		$theme_options = omag_theme_options();

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

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

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

		$second_col_cat    = ! empty( $instance['second_col_cat'] ) ? $instance['second_col_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="row">

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

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

					<?php 

					if ( $first_col_title ) {
						echo $args['before_title'] . esc_html( $first_col_title ) . $args['after_title'];
					} else {
						$default_title = 'Style 2';
						echo $args['before_title'] . esc_html( $default_title ). $args['after_title'];
					} 

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

						if( absint( $first_col_cat ) > 0 ){

							$cat_link = get_category_link( $first_col_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( $first_col_cat )) {

					$style_two_primary_args = array( 
						'cat'             => absint( $first_col_cat ), 
						'post_status'     => 'publish', 
						'posts_per_page'  => 4,
						'ignore_sticky_posts' => 1,
					);

				} else{

					$style_two_primary_args = array( 'post_status' => 'publish', 'posts_per_page' => 4, 'ignore_sticky_posts' => 1, );

				} 

				$style_two_primary_query = new WP_Query( $style_two_primary_args ); 

				if ( $style_two_primary_query->have_posts() ) {

					$style_two_primary_count = 1;

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

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

						if( 1 === $style_two_primary_count) {

							?>

							<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_2_primary = omag_get_the_excerpt( absint( $excerpt_length ) );

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

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

							<?php } else { ?>

							<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
							}
							$style_two_primary_count++;
						endwhile;
						wp_reset_postdata(); 

					}

					?>

				</div>

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

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

						<?php 

						if ( $second_col_title ) {
							echo $args['before_title'] . esc_html( $second_col_title ) . $args['after_title'];
						} else {
							$default_title = 'Style 2';
							echo $args['before_title'] . esc_html( $default_title ). $args['after_title'];
						} 

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

							if( absint( $second_col_cat ) > 0 ){

								$cat_link = get_category_link( $second_col_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( $second_col_cat )) {

						$style_two_secondary_args = array( 
							'cat'             => absint( $second_col_cat ), 
							'post_status'     => 'publish', 
							'posts_per_page'  => 4,
							'ignore_sticky_posts' => 1,
						);

					} else{

						$style_two_secondary_args = array( 'post_status' => 'publish', 'posts_per_page' => 4, 'ignore_sticky_posts' => 1, );

					} 

					$style_two_secondary_query = new WP_Query( $style_two_secondary_args ); 

					if ( $style_two_secondary_query->have_posts() ) {

						$style_two_secondary_count = 1;

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

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

							if( 1 === $style_two_secondary_count) {

								?>

								<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_2_secondary = omag_get_the_excerpt( absint( $excerpt_length ) );

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

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

								<?php } else { ?>

								<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
								}
								$style_two_secondary_count++;
							endwhile;
							wp_reset_postdata(); 

						}

						?>

					</div>

				</div>	        

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

			}

			function update( $new_instance, $old_instance ) {
				$instance = $old_instance;
				$instance['first_col_title'] 	= sanitize_text_field( $new_instance['first_col_title'] );
				$instance['first_col_cat'] = absint( $new_instance['first_col_cat'] );
				$instance['second_col_title'] 	= sanitize_text_field( $new_instance['second_col_title'] );
				$instance['second_col_cat']= absint( $new_instance['second_col_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(
					'first_col_title' 			=> '',
					'first_col_cat'  		=> '',
					'second_col_title' 			=> '',
					'second_col_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-two.jpg" class="layout-img">
				</p>

				<p>
					<label for="<?php echo esc_attr( $this->get_field_id( 'first_col_title' ) ); ?>"><strong><?php esc_html_e( 'First Column Title:', 'omag' ); ?></strong></label>
					<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'first_col_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'first_col_title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['first_col_title'] ); ?>" />
				</p>
				<p>
					<label for="<?php echo  esc_attr( $this->get_field_id( 'first_col_cat' ) ); ?>"><strong><?php esc_html_e( 'First Column Category:', 'omag' ); ?></strong></label>
					<?php
					$cat_args = array(
						'orderby'         => 'name',
						'hide_empty'      => 0,
						'class' 		  => 'widefat',
						'taxonomy'        => 'category',
						'name'            => $this->get_field_name( 'first_col_cat' ),
						'id'              => $this->get_field_id( 'first_col_cat' ),
						'selected'        => absint( $instance['first_col_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( 'second_col_title' ) ); ?>"><strong><?php esc_html_e( 'Second Column Title:', 'omag' ); ?></strong></label>
					<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'second_col_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'second_col_title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['second_col_title'] ); ?>" />
				</p>
				<p>
					<label for="<?php echo  esc_attr( $this->get_field_id( 'second_col_cat' ) ); ?>"><strong><?php esc_html_e( 'Second Column Category:', 'omag' ); ?></strong></label>
					<?php
					$cat_args = array(
						'orderby'         => 'name',
						'hide_empty'      => 0,
						'class' 		  => 'widefat',
						'taxonomy'        => 'category',
						'name'            => $this->get_field_name( 'second_col_cat' ),
						'id'              => $this->get_field_id( 'second_col_cat' ),
						'selected'        => absint( $instance['second_col_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;
