<?php

if (!class_exists('crum_single_post')){

	class crum_single_post extends WP_Widget{

		function __construct(){
			parent::WP_Widget('crum_single_post', 'Box: Single Post', array('description' => 'Single Post'));
		}

		function widget($args, $instance){
            $selected_post = $instance['single_post'];
			$show_meta = $instance['show_meta'];
			$show_excerpt = $instance['show_excerpt'];

			echo $before_widget;
			?>

			<article <?php post_class('post'); ?>>
					<div class="post-media">

			<?php if (has_post_thumbnail($selected_post)){
				$thumb_url = get_post_thumbnail_id($selected_post);
				$img_url = wp_get_attachment_url($thumb_url, 'full'); //get img URL
				$article_image = aq_resize($img_url, 1200, 500, true);
			}?>

				<div class="entry-thumb">
					<a href="<?php echo get_the_permalink($selected_post);?>" class="more-link">
						<img src="<?php echo $article_image ?>" style="margin:0 0;" alt="<?php echo get_the_title($selected_post);?>" title="<?php echo get_the_title($selected_post);?>">
					</a>
				</div>

			</div>

				<?php if ($show_meta == 'true'):?>

			<time class="updated" datetime="<?php echo get_the_time('c',$selected_post); ?>">
				<span class="day"><?php echo get_the_date('d',$selected_post); ?></span>
				<span class="month"><?php echo get_the_date('M',$selected_post); ?></span>
			</time>

				<?php endif;?>

				<?php
					$query = new WP_Query( 'p='.$selected_post.'' );
					while ($query->have_posts()) : $query->the_post();
				?>

				<?php if ($show_excerpt == 'true'):?>

			<div class="ovh">

				<header class="entry-title">
					<?php if ($show_meta == 'true'):?>
					<?php

					get_template_part('templates/entry-meta');

					?>
					<?php endif;?>
					<h2><a href="<?php echo get_the_permalink($selected_post); ?>"><?php echo get_the_title($selected_post); ?></a></h2>
				</header>

				<div class="entry-content">
					<?php echo get_the_excerpt();?>
				</div>
			</div>

				<?php endif;?>
				<?php
					endwhile;
					wp_reset_query();
				?>
			</article>

		<?php
			echo $after_widget;
		}

		function update($old__instance, $new_instance){
			$instance = $old__instance;
			$instance['single_post'] = $new_instance['single_post'];
			$instance['show_meta'] = $new_instance['show_meta'];
			$instance['show_excerpt'] = $new_instance['show_excerpt'];

			return $instance;
		}

		function form($instance){

			$selected_post = $instance['single_post'];

			?>
			<p>
				<label for="<?php echo $this->get_field_id('single_post'); ?>"><?php _e('Select post:', 'crum'); ?></label>
				<select id="<?php echo $this->get_field_id('single_post'); ?>" name="<?php echo $this->get_field_name('single_post'); ?>">
					<?php
					$products_array = new WP_Query( array(
						'post_type'      => 'post',
						'posts_per_page' => - 1,
						'post_status'    => 'publish'
					) );
					while ( $products_array->have_posts() ) : $products_array->the_post();

						$selected = (get_the_ID() == $selected_post) ? 'selected=selected' : '';

						?>
						<option class="widefat" <?php echo $selected;?> value="<?php echo get_the_ID();?>"><?php echo get_the_title();?></option>
					<?php endwhile;
					?>
				</select>
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'show_meta' ); ?>"><?php _e("Show meta","crum")?></label>
				<input id="<?php echo $this->get_field_id( 'show_meta' ); ?>" name="<?php echo $this->get_field_name( 'show_meta' ); ?>" value="true" <?php if( $instance['show_meta'] ) echo 'checked="checked"'; ?> type="checkbox" />
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'show_excerpt' ); ?>"><?php _e("Show excerpt","crum")?></label>
				<input id="<?php echo $this->get_field_id( 'show_excerpt' ); ?>" name="<?php echo $this->get_field_name( 'show_excerpt' ); ?>" value="true" <?php if( $instance['show_excerpt'] ) echo 'checked="checked"'; ?> type="checkbox" />
			</p>


		<?php
		}

	}

}

if ( class_exists('crum_single_post') ) {
	add_action( 'widgets_init', create_function( '', 'register_widget("crum_single_post");' ) );
}