<?php
class WPCTP_News_Widget extends WP_Widget {
	protected $defaults;
	public function __construct() {	
		$this->defaults = array(
			'title' 	=> __( 'NEWS', 'wpctp' ),
			'posts_cat'	=> -1,
			'posts_num'	=> 3,
			'orderby' 	=> '',
			'order' 	=> '',
			'subtitle' 	=> __( 'STAY TUNED', 'wpctp' ),
			'view_all' 	=> __( 'VIEW ALL', 'wpctp' ),
			'read_more' 	=> __( 'READ MORE', 'wpctp' ),
			'background_image'	=> '',
			'default_bg'		=> WPCTP_IMAGES_URL . '/no-image.png',
			'enable_background_image'	=> false,
			'content_limit'	=> 200,
		);
		$widget_ops = array(
			'classname' => 'wpctp-news news',
			'description' => __( 'Displays the WPCargo News. Design only for home page.', 'wpctp' ),
			'customize_selective_refresh' => true,
		);
		$control_ops = array( 'width' => 500, 'height' => 350 );
		parent::__construct( 'wpctp-news', __( 'WPCargo News', 'wpctp' ), $widget_ops, $control_ops );
	}
	public function widget( $args, $instance ) {
		global $wp_query;
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		echo '<div class="before-wrap"><div class="wrap"><div class="wpctp-column-wrap">';
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
        $subtitle = apply_filters( 'widget_title', empty( $instance['subtitle'] ) ? '' : $instance['subtitle'], $instance, $this->id_base );
        $view_all = apply_filters( 'widget_title', empty( $instance['view_all'] ) ? '' : $instance['view_all'], $instance, $this->id_base );
        $read_more = apply_filters( 'widget_title', empty( $instance['read_more'] ) ? '' : $instance['read_more'], $instance, $this->id_base );
		echo '<div class="wpctp-column one-fourth wpctp-column-main animated fadeInUp duration1 eds-on-scroll">';
		if ( ! empty( $title ) ) {
			printf( '<div class="wpctp-news-title">%s</div>', $title );
		}
		if ( ! empty( $subtitle ) ) {
			printf( '<div class="wpctp-news-subtitle">%s</div>',  $subtitle );
		}
		if ( ! empty( $view_all ) ) {
			printf( '<p class="wpctp-news-view-all"><a class="button rounded-btn" href="%s">%s <i class="fa fa-angle-right"></i></a></p>', get_permalink(), $view_all );
		}
		echo '</div>';
		$query_args = array(
			'cat'       => $instance['posts_cat'],
			'post_type'	=> 'post',
			'showposts'	=> $instance['posts_num'],
			'orderby'	=> $instance['orderby'],
			'order' 	=> $instance['order'],
		);
		$wp_query = new WP_Query( $query_args );
		if ( have_posts() ) :
		echo '<div class="news-wrap">';
		while ( have_posts() ) : the_post();
			?>
            <div class="wpctp-column one-fourth animated animated fadeInUp duration1 eds-on-scroll">
                <h3><?php echo get_the_title(); ?></h3>
                <?php the_content_limit( $instance['content_limit'], '' ); ?>
                <p><a class="read-more" href="<?php the_permalink(); ?>"><?php echo $read_more; ?>  <i class="fa fa-angle-right"></i></a></p>
            </div>
			<?php
	 	endwhile; endif;
		echo '</div></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['subtitle'] 		= sanitize_text_field( $new_instance['subtitle'] );
        $instance['view_all'] 		= sanitize_text_field( $new_instance['view_all'] );
        $instance['read_more'] 		= sanitize_text_field( $new_instance['read_more'] );
		$instance['posts_cat'] 	= (int) $new_instance['posts_cat'];
		$instance['posts_num'] 	= (int) $new_instance['posts_num'];
		$instance['orderby'] 	= $new_instance['orderby'];
		$instance['order'] 	= $new_instance['order'];
		$instance['content_limit'] = (int) $new_instance['content_limit'];
		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>
        <p>
        	<label for="<?php echo $this->get_field_id( 'subtitle' ); ?>"><?php _e( 'Subtitle Text', 'wpctp' ); ?> : </label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'subtitle' ); ?>" name="<?php echo $this->get_field_name( 'subtitle' ); ?>" type="text" value="<?php echo esc_attr( sanitize_text_field( $instance['subtitle'] ) ); ?>" />
   		</p>
        <p>
        	<label for="<?php echo $this->get_field_id( 'view_all' ); ?>"><?php _e( 'View All Text', 'wpctp' ); ?> : </label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'view_all' ); ?>" name="<?php echo $this->get_field_name( 'view_all' ); ?>" type="text" value="<?php echo esc_attr( sanitize_text_field( $instance['view_all'] ) ); ?>" />
   		</p>
        <p>
        	<label for="<?php echo $this->get_field_id( 'read_more' ); ?>"><?php _e( 'Read More Text', 'wpctp' ); ?> : </label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'read_more' ); ?>" name="<?php echo $this->get_field_name( 'read_more' ); ?>" type="text" value="<?php echo esc_attr( sanitize_text_field( $instance['read_more'] ) ); ?>" />
   		</p>
		<div class="genesis-widget-column-box genesis-widget-column-box-top">
            <p>
                <label for="<?php echo $this->get_field_id( 'posts_cat' ); ?>"><?php _e( 'Category', 'wpctp' ); ?>:</label>
                <?php
                $categories_args = array(
                    'name'            => $this->get_field_name( 'posts_cat' ),
                    'id'              => $this->get_field_id( 'posts_cat' ),
                    'selected'        => $instance['posts_cat'],
                    'orderby'         => 'Name',
                    'hierarchical'    => 1,
                    'show_option_all' => __( 'All Categories', 'wpctp' ),
                    'hide_empty'      => '0',
                );
                wp_dropdown_categories( $categories_args ); ?>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'posts_num' ); ?>"><?php _e( 'Number of news 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 esc_attr( $this->get_field_id( 'content_limit' ) ); ?>"><?php _e( 'Content Limit', 'wpctp' ); ?> : </label>
                <input id="<?php echo $this->get_field_id( 'content_limit' ); ?>" name="<?php echo $this->get_field_name( 'content_limit' ); ?>" type="number" min="1" value="<?php echo absint( $instance['content_limit'] ); ?>" />
            </p>
		</div>
      	<div style="height:30px;"></div>
		<?php
	}
}