<?php
class WPCTP_Testimonials_Widget extends WP_Widget {
	protected $defaults;
	public function __construct() {	
		$this->defaults = array(
			'title' 	=> __( 'What Customers Are Saying About Us', 'wpctp' ),
			'posts_num'	=> 6,
			'orderby' 	=> '',
			'order' 	=> '',
		);
		$widget_ops = array(
			'classname' => 'wpctp-testimonials-slider wpctp-testimonials testimonials',
			'description' => __( 'Displays the WPCargo Testimonials slider. Design only for home page.', 'wpctp' ),
			'customize_selective_refresh' => true,
		);
		$control_ops = array( 'width' => 500, 'height' => 350 );
		parent::__construct( 'wpctp-testimonials-slider', __( 'WPCargo Testimonial Slider', 'wpctp' ), $widget_ops, $control_ops );
	}
	public function widget( $args, $instance ) {
		global $wp_query;
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		echo '<div class="wpctp-testimonials before-wrap white-text"><div class="wrap">';
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		if ( ! empty( $title ) ) {
			printf( '<h2 class="section-title white-text">%s</h2>', $title );
		}
		$query_args = array(
			'post_type'	=> 'wpcargo_testimonial',
			'showposts'	=> $instance['posts_num'],
			'orderby'	=> $instance['orderby'],
			'order' 	=> $instance['order'],
		);
		$wp_query = new WP_Query( $query_args );
		if ( have_posts() ) :
		echo '<div class="testimonial-slider">';
		while ( have_posts() ) : the_post();
			?>
            <div class="slider-item">
            	<div class="slider-cover-image">
					<div class="testimonial-message"><?php the_content(); ?></div>
					<div class="testimonial-title"><strong><?php echo get_the_title(); ?></strong></div>
					<div class="testimonial-meta">
					<?php echo get_post_meta( get_the_ID(), 'position', true ); ?>
                    <?php echo get_post_meta( get_the_ID(), 'country', true ) ? ' / ' : ''; ?>
					<?php echo get_post_meta( get_the_ID(), 'country', true ); ?>
                  	</div>
                </div>
            </div>
			<?php
	 	endwhile; endif;
		echo '</div>';
		wp_reset_query();
		echo '</div></div>';
	}
	public function update( $new_instance, $old_instance ) {
		$instance 			= $old_instance;
		$instance['title'] 	= sanitize_text_field( $new_instance['title'] );
		$instance['posts_num'] 	= (int) $new_instance['posts_num'];
		$instance['orderby'] 	= $new_instance['orderby'];
		$instance['order'] 	= $new_instance['order'];
		return $instance;
	}
	public function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		?>
		<p>
        	<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'wpctp' ); ?> : </label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( sanitize_text_field( $instance['title'] ) ); ?>" />
   		</p>
		<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 testimonials 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>
       	</div>
      	<div style="height:30px;"></div>
		<?php
	}
}