<?php
class WPCTP_Services_Widget extends WP_Widget {
	protected $defaults;
	public function __construct() {	
		$this->defaults = array(
			'title'	=> __( 'Our Services', 'wpctp' ),
			'order'	=> '',
			'color'	=> '',
			'posts_num'	=> 4,
			'orderby'	=> '',
            'readmore_text'	=> __( 'Read More', 'wpctp' ),
            'readmore_link'	=> '#',
			'default_bg'	=> WPCTP_IMAGES_URL . '/no-image.png',
			'background_image'	=> '',
			'background_color'	=> '',
			'enable_background_image'	=> false,
			'enable_service_icon'	=> true,
			'content_limit'	=> 300,
		);
		$widget_ops = array(
			'classname' => 'wpctp-services wpctp-services services',
			'description' => __( 'Displays the WPCargo Our Services. Design only for home page.', 'wpctp' ),
			'customize_selective_refresh' => true,
		);
		$control_ops = array( 'width' => 500, 'height' => 350 );
		parent::__construct( 'wpctp-services', __( 'WPCargo Our Services', 'wpctp' ), $widget_ops, $control_ops );
	}
	public function widget( $args, $instance ) {
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		$style	= wpctp_get_css( 'background-image', $instance['background_image'] );
		$style	.= wpctp_get_css( 'background-color', $instance['background_color'] );
		$style 	.= wpctp_get_css( 'color', $instance['color'] );
		printf( '<div class="before-wrap" style="%s"><div class="wrap">', $style );
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		if ( ! empty( $title ) ) {
			printf( '<h2 class="section-title aligncenter animated fadeInUp duration1 eds-on-scroll" style="%s">%s</h2>', $style, $title );
		}
		$query_args = array(
			'post_type'	=> 'wpcargo_service',
			'showposts'	=> $instance['posts_num'],
			'orderby'	=> $instance['orderby'],
			'order' 	=> $instance['order'],
		);
		$service_query = new WP_Query( $query_args );
		if ( $service_query->have_posts() ) :
		echo '<div class="wpctp-column-wrap">';
		while ( $service_query->have_posts() ) : $service_query->the_post();
			$service_icon_type = get_post_meta( get_the_ID(), 'service_icon_type', true );
			$service_icon_image = get_post_meta( get_the_ID(), 'service_icon_image', true );
			$service_icon = get_post_meta( get_the_ID(), 'service_icon', true );
			$icon_url = wp_get_attachment_image_url( $service_icon_image, 'full' );
			?>
            <div class="wpctp-column one-half animated fadeInUp duration1 eds-on-scroll">          
                <?php if( $instance['enable_service_icon'] ): ?>
					<?php if( $service_icon_type == '#service_icon' ): ?>
                 		<i class="service-icon alignleft <?php echo $service_icon; ?>"></i>
               		<?php endif; ?>
					<?php if( $service_icon_type == '#service_icon_image' ): ?>
                 		<img class="service-icon alignleft" src="<?php echo $icon_url; ?>" alt="" />
               		<?php endif; ?>
               	<?php endif; ?>
                <div class="group">
                    <p><strong><?php echo get_the_title(); ?></strong></p>
                 	<?php the_content_limit( $instance['content_limit'], '' ); ?>
                </div>
            </div>
			<?php
	 	endwhile; endif;
		echo '</div>';
		wp_reset_query();
        if( $service_query->found_posts > $instance['posts_num'] ) {
			?>
            <div class="clear"></div>
            <div class="wpctp-readmore aligncenter">
            	<?php printf( '<a class="button rounded-btn read-more-btn" href="%s">%s</a>', $instance['readmore_link'], $instance['readmore_text'] ); ?>
            </div>
			<?php
        }
		echo '</div></div>';
	}
	public function update( $new_instance, $old_instance ) {
		$instance 			= $old_instance;
		$instance['title'] 	= sanitize_text_field( $new_instance['title'] );
		$instance['color'] 	= $new_instance['color'];
		$instance['order'] 	= $new_instance['order'];
		$instance['posts_num'] 	= (int) $new_instance['posts_num'];
		$instance['orderby'] 	= $new_instance['orderby'];
        $instance['readmore_text'] 	= $new_instance['readmore_text'];
        $instance['readmore_link'] 	= $new_instance['readmore_link'];
		$instance['enable_service_icon'] = (bool) $new_instance['enable_service_icon'];
		$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>
		<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 services to show', 'wpctp' ); ?> : </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>
            <p>
                <label for="<?php echo $this->get_field_id( 'readmore_text' ); ?>"><?php _e( 'Read More Text', 'wpctp' ); ?> : </label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'readmore_text' ); ?>" name="<?php echo $this->get_field_name( 'readmore_text' ); ?>" type="text" value="<?php echo esc_attr( sanitize_text_field( $instance['readmore_text'] ) ); ?>" size="" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'readmore_link' ); ?>"><?php _e( 'Read More Link', 'wpctp' ); ?> : </label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'readmore_link' ); ?>" name="<?php echo $this->get_field_name( 'readmore_link' ); ?>" type="text" value="<?php echo esc_attr( sanitize_text_field( $instance['readmore_link'] ) ); ?>" size="" />
   			</p>
            <p>
                <label for="<?php echo $this->get_field_id( 'enable_service_icon' ); ?>">
                    <input class="checkbox" type="checkbox"<?php checked( true, $enable_service_icon ); ?> id="<?php echo $this->get_field_id( 'enable_service_icon' ); ?>" name="<?php echo $this->get_field_name( 'enable_service_icon' ); ?>" value="1" />
                    <?php _e( 'Enable Service Icon', 'wpctp' ); ?>
                </label>
            </p>
		</div>
      	<div style="height:30px;"></div>
		<?php
	}
}