<?php
/**
 * Register Tab Widgets.
 *
 * @package Mag_Lite
 */

function mag_lite_action_home_tab() {

	register_widget( 'mag_lite_home_tab' );

}
add_action( 'widgets_init', 'mag_lite_action_home_tab' );

class mag_lite_home_tab extends WP_Widget
{

	function __construct() {

		global $control_ops;

		$widget_ops = array(
			'classname'   => 'mag-lite-home-tab',
			'description' => esc_html__( 'Add Widget to Display Tab Section .', 'mag-lite' ),
            'customize_selective_refresh' => true,
			);

		parent::__construct( 'mag_lite_home_tab',esc_html__( 'ML: Home Tab Section', 'mag-lite' ), $widget_ops, $control_ops );
	}

	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array( 
			'category'         => '',     
			'number'           => 2,
			'category_news'    => '',     
			'number_news'      => 2,
			'show_post_meta'	 => true, 
			) );
		$title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
		$category = isset( $instance['category'] ) ? absint( $instance['category'] ) : 0;
		$number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 2;
		$category_news = isset( $instance['category_news'] ) ? absint( $instance['category_news'] ) : 0;
		$number_news    = isset( $instance['number_news'] ) ? absint( $instance['number_news'] ) : 2; 
		$show_post_meta = isset( $instance['show_post_meta'] ) ? (bool) $instance['show_post_meta'] : true;		    
		?>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>">
				<?php echo esc_html__( 'Title:', 'mag-lite' ); ?>				
			</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($title); ?>" />
		</p>	

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>">
				<?php esc_html_e( 'Category:', 'mag-lite' ); ?>			
			</label>

			<?php
			wp_dropdown_categories(array(
				'show_option_none' => '',
				'class'			   => 'widefat',
				'show_option_all'  => esc_html__('Choose Options','mag-lite'),
				'name'             => esc_attr($this->get_field_name( 'category' )),
				'selected'         => absint( $category ),          
				) );
				?>
			</p>
			<p>
				<label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>">
					<?php echo esc_html__( 'Choose Number', 'mag-lite' );?>    		
				</label>

				<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id( 'number' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number' )); ?>" type="number" step="1" min="1" value="<?php echo esc_attr($number); ?>" max="4" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'category_news' ) ); ?>">
					<?php esc_html_e( 'Category:', 'mag-lite' ); ?>			
				</label>

				<?php
				wp_dropdown_categories(array(
					'show_option_none' => '',
					'class'			   => 'widefat',
					'show_option_all'  => esc_html__('Choose Options','mag-lite'),
					'name'             => esc_attr($this->get_field_name( 'category_news' )),
					'selected'         => absint( $category_news ),          
					) );
					?>
				</p>
				<p>
					<label for="<?php echo esc_attr($this->get_field_id( 'number_news' )); ?>">
						<?php echo esc_html__( 'Choose Number', 'mag-lite' );?>    		
					</label>

					<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id( 'number_news' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number_news' )); ?>" type="number" step="1" min="1" value="<?php echo esc_attr($number_news); ?>" max="4" />
				</p>
				<p><input class="checkbox" type="checkbox"<?php checked( $show_post_meta ); ?> id="<?php echo esc_attr($this->get_field_id( 'show_post_meta' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'show_post_meta' )); ?>" />
					<label for="<?php echo esc_attr($this->get_field_id( 'show_post_meta' )); ?>"><?php echo esc_html__( 'Enable Post Meta', 'mag-lite' ); ?></label></p>   

					<?php
				}

				function update( $new_instance, $old_instance ) {
					$instance = $old_instance;

					$instance['title'] = sanitize_text_field( $new_instance['title'] );
					$instance['category'] = absint( $new_instance['category'] );
					$instance['number'] = (int) $new_instance['number'];	
					$instance['category_news'] = absint( $new_instance['category_news'] );
					$instance['number_news'] = (int) $new_instance['number_news'];	
					$instance['show_post_meta'] = (bool) $new_instance['show_post_meta']; 		   

					return $instance;
				}

				function widget( $args, $instance ) {

					extract( $args );

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

					$category  = isset( $instance[ 'category' ] ) ? $instance[ 'category' ] : '';

					$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 2;

					$category_news  = isset( $instance[ 'category_news' ] ) ? $instance[ 'category_news' ] : '';

					$number_news = ( ! empty( $instance['number_news'] ) ) ? absint( $instance['number_news'] ) : 2;

					$show_post_meta = isset( $instance['show_post_meta'] ) ? $instance['show_post_meta'] : true;


					$total_number = $number_news + $number;

					if( $total_number > 2){
						$total_number = 2;
					} 

					$category_title = get_cat_name($category);
					$category_news_title = get_cat_name($category_news);  

					if( !empty( $category ) || !empty( $category_news ) ):
						echo $before_widget; ?>

					<section class="news-section section-news-title mag-lite-home-tab">

						<div class="tabs">
							<ul class="tab-links">
								<?php if( !empty( $title ) ):?>
									<header class="entry-header heading">
										<h2 class="entry-title"><span><?php echo esc_html( $title);?></span></h2>
									</header>
								<?php endif;?>
								<?php if( !empty( $category) && !empty( $category_news) ) { ?>
								<li class="active"><a href="#viewed-tab0"><?php echo esc_html__( 'All','mag-lite');?></a></li>
								<?php } ?>
								<?php if( !empty( $category) ) { ?>
								<li><a href="#viewed-tab1"><?php echo esc_html( $category_title);?></a></li>
								<?php } ?>

								<?php if( !empty( $category_news) ) { ?>
								<li><a href="#viewed-tab2"><?php echo esc_html( $category_news_title);?></a></li>
								<?php } ?>

							</ul>
							<div class="tab-content">
								<?php if( !empty( $category) && !empty( $category_news) ) { ?>
								<div id="viewed-tab0" class="tab active">
									<?php $all_args = array(
										'posts_per_page' => absint( $total_number ),
										'post_type' => 'post',
										'post_status' => 'publish',      
										);

									if ( absint( $category ) > 0 ||  absint( $category_news ) > 0 ) {
										$all_args['cat'] = array( absint( $category ), absint( $category_news ) );
									}
									$all_query = new WP_Query( $all_args ); 


									if ($all_query->have_posts()) : $cn = 0;
									while ( $all_query->have_posts() ) : $all_query->the_post(); $cn++;?>

									<?php $post_class = ''; 
									if( $cn%2 == 0){
										$post_class = 'opp';
									}?>

									<?php 
									$image_class= '';
									if( !has_post_thumbnail() ){
										$image_class= 'no-image';
									} 
									?>

									<div class="post flexible-post <?php echo esc_attr( $image_class);?> <?php echo esc_attr( $post_class );?>">
										<?php if( has_post_thumbnail() ): ?>

											<figure class="featured-image">
												<?php the_post_thumbnail( 'mag-lite-home-tab' );?>
											</figure>

										<?php endif;?>

										<div class="post-content">
											<header class="entry-header">
												<?php mag_lite_entry_categories();?>
												<h3 class="entry-title">
													<a href="<?php the_permalink();?>"><?php the_title();?></a>
												</h3>
											</header>
											<div class="entry-content">
												<?php
												$excerpt = mag_lite_the_excerpt(20);
												echo wp_kses_post( wpautop( $excerpt ) );
												?>
											</div>

											<?php if( true== $show_post_meta): ?>

												<div class="entry-meta">

													<?php mag_lite_posted_on(); 
													mag_lite_posted_by(); 

													?>

												</div>

											<?php endif; ?>

										</div>
									</div>

								<?php endwhile;
								wp_reset_postdata();?>

							<?php endif;?>
						</div>	   
						<?php } ?>             	
						<?php if( !empty( $category) ) { ?>
						<div id="viewed-tab1" class="tab">
							<?php $category_args = array(
								'posts_per_page' => absint( $number ),
								'post_type' => 'post',
								'post_status' => 'publish',      
								);

							if ( absint( $category ) > 0 ) {
								$category_args['cat'] = absint( $category );
							}
							$the_query = new WP_Query( $category_args );    

							if ($the_query->have_posts()) : $cn = 0;
							while ( $the_query->have_posts() ) : $the_query->the_post(); $cn++;?>

							<?php $post_class = ''; 
							if( $cn%2 == 0){
								$post_class = 'opp';
							}?>

							<?php 
							$image_class= '';
							if( !has_post_thumbnail() ){
								$image_class= 'no-image';
							} 
							?>

							<div class="post flexible-post <?php echo esc_attr( $image_class);?> <?php echo esc_attr( $post_class );?>">
								<?php if( has_post_thumbnail() ): ?>

									<figure class="featured-image">
										<?php the_post_thumbnail( 'mag-lite-home-tab' );?>
									</figure>

								<?php endif;?>

								<div class="post-content">
									<header class="entry-header">
										<?php mag_lite_entry_categories();?>
										<h3 class="entry-title">
											<a href="<?php the_permalink();?>"><?php the_title();?></a>
										</h3>
									</header>
									<div class="entry-content">
										<?php
										$excerpt = mag_lite_the_excerpt(20);
										echo wp_kses_post( wpautop( $excerpt ) );
										?>
									</div>

									<?php if( true== $show_post_meta): ?>

										<div class="entry-meta">

											<?php mag_lite_posted_on(); 
											mag_lite_posted_by(); 

											?>

										</div>

									<?php endif; ?>

								</div>
							</div>

						<?php endwhile;
						wp_reset_postdata();?>

					<?php endif;?>
				</div>
				<?php } ?>
				<?php if( !empty( $category_news) ) { ?>

				<div id="viewed-tab2" class="tab">
					<?php $category_args = array(
						'posts_per_page' => absint( $number_news ),
						'post_type' => 'post',
						'post_status' => 'publish',      
						);

					if ( absint( $category_news ) > 0 ) {
						$category_args['cat'] = absint( $category_news );
					}
					$the_query = new WP_Query( $category_args );    

					if ($the_query->have_posts()) : $cn = 0;
					while ( $the_query->have_posts() ) : $the_query->the_post(); $cn++;?>

					<?php $post_class = ''; 
					if( $cn%2 == 1){
						$post_class = 'opp';
					}?>

					<?php 
					$image_class= '';
					if( !has_post_thumbnail() ){
						$image_class= 'no-image';
					} 
					?>

					<div class="post flexible-post <?php echo esc_attr( $image_class);?> <?php echo esc_attr( $post_class );?>">
						<?php if( has_post_thumbnail() ): ?>

							<figure class="featured-image">
								<?php the_post_thumbnail( 'mag-lite--home-tab' );?>
							</figure>

						<?php endif;?>

						<div class="post-content">
							<header class="entry-header">
								<?php mag_lite_entry_categories();?>
								<h3 class="entry-title">
									<a href="<?php the_permalink();?>"><?php the_title();?></a>
								</h3>
							</header>
							<div class="entry-content">
								<?php
								$excerpt = mag_lite_the_excerpt(30);
								echo wp_kses_post( wpautop( $excerpt ) );
								?>
							</div>
							<?php if( true== $show_post_meta): ?>

								<div class="entry-meta">

									<?php mag_lite_posted_on(); 
									mag_lite_posted_by(); 

									?>

								</div>

							<?php endif; ?>
						</div>
					</div>

				<?php endwhile;
				wp_reset_postdata();?>

			<?php endif;?>
		</div> 

		<?php } ?>                    

	</div>
</div>

</section>

<?php echo $after_widget; 
endif;

} 

}