<?php
class WPCTP_Sliders_Widget extends WP_Widget {
	protected $defaults;
	public function __construct() {	
		$this->defaults = array(
			'posts_num'	=> 4,
			'orderby' 	=> '',
			'order' 	=> '',
			'slider_height'	=> 600,
		);
		$widget_ops = array(
			'classname' => 'wpctp-sliders wpctp-sliders',
			'description' => __( 'Displays the WPCargo Sliders. Design only for home page.', 'wpctp' ),
			'customize_selective_refresh' => true,
		);
		$control_ops = array( 'width' => 500, 'height' => 350 );
		parent::__construct( 'wpctp-sliders', __( 'WPCargo Sliders', 'wpctp' ), $widget_ops, $control_ops );
	}
	public function widget( $args, $instance ) {
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		$query_args = array(
			'post_type'	=> 'wpcargo_slider',
			'showposts'	=> $instance['posts_num'],
			'orderby'	=> $instance['orderby'],
			'order' 	=> $instance['order'],
		);
		$sliders = new WP_Query( $query_args );
		echo '<div id="wpctp-slider-wrap" class="slider-wrap">';
		if ( $sliders->have_posts() && $instance['posts_num'] > 0 ) :
		echo '<ul id="wpctp-sliders" class="wpctp-sliders" style="margin-left: 0!important;opacity:0;">';
		while ( $sliders->have_posts() ) : $sliders->the_post();		
			$pid = get_the_ID();
			$img_url= wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
			if( has_post_thumbnail() ) {	
				$img_url= $img_url[0]; 
			}else{ 
				$img_url = home_url().'/wp-content/uploads/2015/08/admin.jpg';
			}	
			?>
			<li class="wpslide-boxes wpslide-box<?php echo $sliders->current_post; ?>" style="height:<?php echo $instance['slider_height'] ?>px;background-image: url(<?php echo $img_url; ?>);">
                <div class="wpslide-box-content">
                    <div class="wrap">
                        <div class="content-box">
                            <div class="slider-context">
                                <?php the_content(); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </li>
			<?php
	 	endwhile; 
		echo '</ul>';
		wp_reset_query();
		endif;
		echo '</div>';
	}
	public function update( $new_instance, $old_instance ) {
		$instance			= $old_instance;
		$instance['posts_num']	= (int) $new_instance['posts_num'];
		$instance['orderby']	= $new_instance['orderby'];
		$instance['order']	= $new_instance['order'];
		$instance['slider_height']	= (int) $new_instance['slider_height'];
		return $instance;
	}
	public function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		?>
		<div style="height:30px;"></div>
		<div class="genesis-widget-column-box genesis-widget-column-box-top">
            <p>
                <label for="<?php echo $this->get_field_id( 'posts_num' ); ?>"><?php _e( 'Number of slides to show' ); ?> : </label>
                <input class="tiny-text" id="<?php echo $this->get_field_id( 'posts_num' ); ?>" name="<?php echo $this->get_field_name( 'posts_num' ); ?>" type="number" step="1" min="1" value="<?php echo absint( $instance['posts_num'] ); ?>" size="3" />
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php _e( 'Order By', 'wpctp' ); ?> : </label>
                <select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
                    <option value="date" <?php selected( 'date', $instance['orderby'] ); ?>><?php _e( 'Date', 'wpctp' ); ?></option>
                    <option value="title" <?php selected( 'title', $instance['orderby'] ); ?>><?php _e( 'Title', 'wpctp' ); ?></option>
                    <option value="ID" <?php selected( 'ID', $instance['orderby'] ); ?>><?php _e( 'ID', 'wpctp' ); ?></option>
                    <option value="rand" <?php selected( 'rand', $instance['orderby'] ); ?>><?php _e( 'Random', 'wpctp' ); ?></option>
                </select>
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"><?php _e( 'Sort Order', 'wpctp' ); ?> : </label>
                <select id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
                    <option value="DESC" <?php selected( 'DESC', $instance['order'] ); ?>><?php _e( 'Descending (3, 2, 1)', 'wpctp' ); ?></option>
                    <option value="ASC" <?php selected( 'ASC', $instance['order'] ); ?>><?php _e( 'Ascending (1, 2, 3)', 'wpctp' ); ?></option>
                </select>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'slider_height' ); ?>"><?php _e( 'Slider Height' ); ?> : </label>
                <input id="<?php echo $this->get_field_id( 'slider_height' ); ?>" name="<?php echo $this->get_field_name( 'slider_height' ); ?>" type="number" step="1" min="1" value="<?php echo absint( $instance['slider_height'] ); ?>" size="20" />
            </p>
		</div>
      	<div style="height:30px;"></div>
		<?php
	}
}