<?php

/**
 * Class searchIO_Live_Search_Widget
 *
 * The searchIO Live Ajax Search Widget
 *
 * @since 1.0
 */
//class searchIO_Live_Search_Widget extends WP_Widget {
class IO_Search extends WP_Widget {

	/**
	 * Register the Widget with WordPress
	 */
	function __construct() {
		parent::__construct(
			'searchio_live_search',
			__( 'Inflatable Office Live Search', 'searchio-live-ajax-search' ),
			array( 'description' => __( 'Inflatable Office Live Search', 'searchio-live-ajax-search' ), )
		);
	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {
		//$destination   = empty( $instance['destination'] ) ? '' : $instance['destination'];
        $destination   = get_home_url();
		$placeholder   = empty( $instance['placeholder'] ) ? __( 'Search for...', 'searchio-live-ajax-search' ) : $instance['placeholder'];
		$engine        = empty( $instance['engine'] ) ? 'default' : $instance['engine'];
        $hide_submit   = empty( $instance['hide_submit'] ) ? '' : $instance['hide_submit'];
		$config        = empty( $instance['config'] ) ? 'default' : $instance['config'];
        $widget_width  = empty( $instance['width'] ) ? '220': $instance['width'];
        $widget_width.='px';

		echo wp_kses_post( $args['before_widget'] );
		do_action( 'searchio_live_search_before_widget' );

		?>
			<?php do_action( 'searchio_live_search_widget_before_form' ); ?>
			<form role="search" method="get" class="searchio-live-search-widget-search-form" action="<?php echo esc_url( $destination ); ?>">
				<?php do_action( 'searchio_live_search_widget_before_field' ); ?>
				<label>
					<span class="screen-reader-text"><?php esc_html_e( 'Search for:', 'searchio-live-ajax-search' ); ?></span>
					<input type="search" class="search-field" style="width: <?php echo $widget_width ?>" placeholder="<?php echo esc_attr( $placeholder ); ?>" value="" name="s" data-swplive="true" data-swpengine="<?php echo esc_attr( $engine ); ?>" data-swpconfig="<?php echo esc_attr( $config ); ?>" autocomplete="off">
				</label>
				<?php do_action( 'searchio_live_search_widget_after_field' ); ?>
				<input type="submit" class="search-submit" <?php if($hide_submit==='1') {  echo 'style="display:none;"'; } ?> value="<?php esc_html_e( 'Search', 'searchio-live-ajax-search' ); ?>">
				<?php do_action( 'searchio_live_search_widget_after_submit' ); ?>
			</form>
			<?php do_action( 'searchio_live_search_widget_after_form' ); ?>
		<?php

		echo wp_kses_post( $args['after_widget'] );
		do_action( 'searchio_live_search_after_widget' );
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 *
	 * @return string|void
	 */
	public function form( $instance ) {

		$widget_placeholder = isset( $instance['placeholder'] ) ? $instance['placeholder'] : __( 'Search for...', 'searchio-live-ajax-search' );
		$hide_submit        = isset( $instance['$hide_submit'] ) ? $instance['$hide_submit'] : '1';
		$widget_width       = isset( $instance['width'] ) ? $instance['width'] : '220';
		$results_width      = isset( $instance['results_width'] ) ? $instance['results_width'] : '400';
		$results_align      = isset( $instance['results_align'] ) ? $instance['results_align'] : 'Left';
		$same_width         = isset( $instance['same_width'] ) ? $instance['same_width'] : '1';

		$same_width_check = '';
		if(!empty($same_width)){
		    $same_width_check = 'checked="checked"';
        }

        $hide_submit_check = '';
        if(!empty($hide_submit)){
            $hide_submit_check = 'checked="checked"';
        }

		$alignments = array('Left', 'Right');

		// We'll piggyback searchIO itself to pull a list of search engines.
		$widget_engine = isset( $instance['engine'] ) ? $instance['engine'] : 'default';
		$engines = array();
		if ( class_exists( 'searchIO' ) ) {
			if ( class_exists( '\\searchIO\\Settings' ) ) {
				$searchio_engines = \searchIO\Settings::get_engines();
				foreach ( $searchio_engines as $engine => $engine_settings ) {
					$engines[ $engine ] = $engine_settings->get_label();
				}
			} else if ( method_exists( 'searchIO', 'instance' ) ) {
				$engines['default'] = 'Default';
				$searchio = searchIO::instance();
				$searchio_engines = $searchio->settings['engines'];
				foreach ( $searchio_engines as $engine => $engine_settings ) {
					if ( isset( $engine_settings['searchio_engine_label'] ) ) {
						$engines[ $engine ] = $engine_settings['searchio_engine_label'];
					}
				}
			}
		}

		// we're going to utilize searchIO_Live_Search_Form to populate the config dropdown
		$widget_config = isset( $instance['config'] ) ? $instance['config'] : 'default';
		if ( ! class_exists( 'searchIO_Live_Search_Form' ) ) {
			include_once( dirname( __FILE__ ) . '/class-form.php' );
		}
		$form = new searchIO_Live_Search_Form();
		$form->setup();
		?>
		<?php $swpuniqid = uniqid( 'io' ); ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>"><?php esc_html_e( 'Placeholder:', 'searchio-live-ajax-search' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'placeholder' ) ); ?>" type="placeholder" value="<?php echo esc_attr( $widget_placeholder ); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>"><?php esc_html_e( 'Width:', 'searchio-live-ajax-search' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'width' ) ); ?>" type="number" value="<?php echo $widget_width; ?>">
        </p>
        <p>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('hide_submit')); ?>"
                   name="<?php echo esc_attr($this->get_field_name('hide_submit')); ?>" type="checkbox"
                   value="1" <?php echo esc_attr($hide_submit_check); ?>/>
            <label for="<?php echo esc_attr($this->get_field_id('hide_submit')); ?>"><?php _e('Hide submit button', 'searchio-live-ajax-search'); ?></label>
        </p>
        <p>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('same_width')); ?>"
                   name="<?php echo esc_attr($this->get_field_name('same_width')); ?>" type="checkbox"
                   value="1" <?php echo esc_attr($same_width_check); ?>/>
            <label for="<?php echo esc_attr($this->get_field_id('same_width')); ?>"><?php _e('Make search results have different width', 'searchio-live-ajax-search'); ?></label>
        </p>
        <div id="search-results-settings" <?php if($same_width == '') { ?> style="display:none; <?php } ?>">
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'results_width' ) ); ?>"><?php esc_html_e( 'Results width:', 'searchio-live-ajax-search' ); ?></label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'results_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'results_width' ) ); ?>" type="number" value="<?php echo $results_width; ?>">
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'results_align' ) ); ?>"><?php esc_html_e( 'Results alignment:', 'searchio-live-ajax-search' ); ?></label>
                <select name="<?php echo esc_attr( $this->get_field_name( 'results_align' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'results_align' ) ); ?>">
                    <?php foreach ( $alignments as $alignment ) : ?>
                        <option value="<?php echo esc_attr( strtolower($alignment) ); ?>" <?php selected( $results_align, $alignment ); ?>><?php echo esc_html( $alignment ); ?></option>
                    <?php endforeach; ?>
                </select>
            </p>
        </div>
        <script type="text/javascript">
            jQuery(document).ready(function($){
                $('#<?php echo esc_attr($this->get_field_id('same_width')); ?>').click(function(){
                    var $results_settings = $(this).parents().find('#search-results-settings');
                    if($(this).prop("checked") == true){
                        $results_settings.show();
                    }
                    else if($(this).prop("checked") == false){
                        $results_settings.hide();
                    }
                });
            });
        </script>
	<?php
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['placeholder']   = ( ! empty( $new_instance['placeholder'] ) )   ? strip_tags( $new_instance['placeholder'] ) : '';
		$instance['engine']        = ( ! empty( $new_instance['engine'] ) )        ? strip_tags( $new_instance['engine'] ) : '';
        $instance['hide_submit']   = ( ! empty( $new_instance['hide_submit'] ) )   ? strip_tags( $new_instance['hide_submit'] ) : '';
        $instance['width']         = ( ! empty( $new_instance['width'] ) )         ? strip_tags( $new_instance['width'] ) : '';
        $instance['results_width'] = ( ! empty( $new_instance['results_width'] ) ) ? strip_tags( $new_instance['results_width'] ) : '';
        $instance['results_align'] = ( ! empty( $new_instance['results_align'] ) ) ? strip_tags( $new_instance['results_align'] ) : '';
        $instance['same_width']    = ( ! empty( $new_instance['same_width'] ) )    ? strip_tags( $new_instance['same_width'] ) : '';

        update_option('io_search_results_same_width', $new_instance['same_width']);
        update_option('io_search_results_width', $new_instance['results_width']);
        update_option('io_search_results_align', $new_instance['results_align']);

		return $instance;
	}
}

