<?php
class post_photo extends WP_Widget {
	
	function __construct() {
		parent::__construct( false, 'Pastor - اسلایدر عکس' );
	}
	
	function widget( $args, $instance ) {
		extract($args, EXTR_SKIP);
		echo '<div class="sidebar-box">';
		//Display title as stored in this instance of the widget
		echo '<div class="sidebar-title"><h3>'.$instance['news_title'].'</h3></div>'; 
		echo '';
		print_post_photo($instance);
		echo '</div>';
	}
	
                        
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['news_title'] = $new_instance['news_title'];
		$instance['select_news'] = $new_instance['select_news'];
		$instance['news_cat'] = $new_instance['news_cat'];
		$instance['news_count'] = $new_instance['news_count'];
		return $instance;
	}

	function form( $instance ) {
		$defaults = array(
			'news_title' => 'جدیدترین تصاویر',
			'news_count' => '3',
		);
		$instance = wp_parse_args( (array) $instance, $defaults );
		?>
		<br/>
                        <p> 
						<label for="<?php echo $this->get_field_id('news_title'); ?>"> عنوان </label>
						<input type="text" id="<?php echo $this->get_field_id( 'news_title' ); ?>" name="<?php echo $this->get_field_name( 'news_title' ); ?>" value="<?php echo $instance['news_title']; ?>" />
					    </p>
					
					    <p>
					     <label for="<?php echo $this->get_field_id('select_news'); ?>"> نوع نمايش </label>
							<select class="select-type" id="<?php echo $this->get_field_id('select_news'); ?>" name="<?php echo $this->get_field_name('select_news'); ?>" type="text">
								<option value="1">جدیدترین تصاویر</option>
								<option value="2">نمايش عکس ها به صورت تصادفي</option>
								<option value="3">نمايش از دسته خاص</option>
							</select>
						</p>
						
					
					<p class="select-cat" style="display:none;">
						<label for="<?php echo $this->get_field_id('news_cat'); ?>"> انتخاب دسته</label>
							<select id="<?php echo $this->get_field_id('news_cat'); ?>" name="<?php echo $this->get_field_name('news_cat'); ?>" class="widefat">
								<?php foreach(get_terms('photo_category','parent=0&hide_empty=0') as $term) { ?>
								<option <?php selected( $instance['news_cat'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
								<?php } ?>      
							</select>
					</p>
					
					<p>
					<label for="<?php echo $this->get_field_id('news_count'); ?>"> تعداد مطلب</label>
					<input type="text" id="<?php echo $this->get_field_id( 'news_count' ); ?>" name="<?php echo $this->get_field_name( 'news_count' ); ?>" value="<?php echo $instance['news_count']; ?>" />
					</p>
					
		<?php
		
		echo '<script>';
		echo 'document.getElementById("'. $this->get_field_id( 'select_news' ) .'").value = "' . $instance['select_news'] . '";';
		echo 'document.getElementById("'. $this->get_field_id( 'news_title' ) .'").value = "' . $instance['news_title'] . '";';
		echo 'document.getElementById("'. $this->get_field_id( 'news_count' ) .'").value = "' . $instance['news_count'] . '";';
		echo '
			jQuery(function($) {
				$("#'. $this->get_field_id('select_news'). '").change(function(){
					if($("#'. $this->get_field_id('select_news'). '").val() == "3") {
						$(".select-cat").show(); 
					} else {
						$(".select-cat").hide(); 
					} 
				});
			});
		</script>';
	}
	
}
function post_photo_register() {
	register_widget( 'post_photo' );
}
add_action( 'widgets_init', 'post_photo_register' );

function print_post_photo($options) { ?>
<div class="owl-carousel owl-theme">
				<?php
					if($options['select_news']==1){
						$big_query = new WP_Query(array(
						'post_status' =>'publish',
						'post_type' =>'photo',
						'posts_per_page' => $options['news_count']));
						
					} elseif ($options['select_news']==2) {
						
						$big_query = new WP_Query(array(
						'post_status' =>'publish',
						'post_type' =>'photo',
						'orderby' => 'rand',
						'posts_per_page' => $options['news_count']));
						
					} elseif ($options['select_news']==3) {
						
						$big_query = new WP_Query(array(
						'post_status' =>'publish',
						'post_type' =>'photo',
						'tax_query' => array(
							array(
								'taxonomy' => 'photo_category',
								'field'    => 'term_id',
								'terms'    => $options['news_cat'],
							),
						),
						'posts_per_page' => $options['news_count']));
					}
					if($big_query->have_posts()) : while($big_query->have_posts()) : $big_query->the_post();
					?>                        
<article class="item-s">
<div class="thumbnail">
<div class="calendar-small">
<div class="s-date"><?php the_date('d');  ?></div>
<div class="s-month"> <?php the_date('F');  ?></div>
</div>
<a href="<?php the_permalink(); ?>">
<?php if(has_post_thumbnail()){?>
<img src="<?php the_post_thumbnail_url('loop2'); ?>" alt="<?php the_title(); ?>">
<?php }else{ ?>
<img src="<?php bloginfo("template_url"); ?>/img/none.png">
<?php } ?>
</a>
<div class="det">
<?php if(get_post_meta( get_the_ID(), 'number-photo', true )!=null){ ?><span><i class="fa fa-camera"></i> <?php echo get_post_meta( get_the_ID(), 'number-photo', true ); ?></span><?php } ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?>
</a></h3>
</div>
</div>
</article>                                        
<?php endwhile; endif; wp_reset_query(); ?>
 </div>                   
<?php
}
?>