<?php
class WPCTP_Slider_Search_Widget extends WP_Widget {
	protected $defaults;
	public function __construct() {	
		$this->defaults = array(
			'title'		=> __( 'WPCargo Tracking', 'wpctp' ),
			'action'	=> '',
			'text'		=> __( 'Enter the Consignment No.', 'wpctp' ),
			'button_text'	=> __( 'Track Result', 'wpctp' ),
			'search_placeholder'	=> __( 'Ex: 12345', 'wpctp' )
		);
		$widget_ops = array(
			'classname' => 'wpctp-slider-search wpctp-slider-search slider-search',
			'description' => __( 'Displays the WPCargo search form in slider. Design only for home page.', 'wpctp' ),
			'customize_selective_refresh' => true,
		);
		$control_ops = array( 'width' => 500, 'height' => 350 );
		parent::__construct( 'wpctp-slider-search', __( 'WPCargo Slider Search', 'wpctp' ), $widget_ops, $control_ops );
	}
	public function widget( $args, $instance ) {
		global $wp_query;
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		?>
        <div class="tracking-form">
            <div class="wrap">
                <form id="search-tracking" method="post" action="<?php echo home_url().'/track/'; //echo $instance['action']; ?>">
					<?php wp_nonce_field( 'wpcargo_track_shipment_action', 'track_shipment_nonce' ); ?>
                    <?php
						if ( ! empty( $title ) ) {
							printf( '<h3>%s</h3>', $title );
						}					
					?>
                    <div class="search-text"><?php echo wpautop( $instance['text'] ); ?></div>
                    <input type="text" name="wpcargo_tracking_number" value="" autocomplete="off" required="required" placeholder="<?php echo $instance['search_placeholder']; ?>" />
                    <input type="submit" id="submit_wpcargo" name="wpcargo-submit" class="green-bg-btn" value="<?php echo $instance['button_text']; ?>">
                </form>
            </div>
        </div>
 		<?php
	}
	public function update( $new_instance, $old_instance ) {
		$instance  = $old_instance;
		$instance['action'] = $new_instance['action'];
		$instance['text'] = $new_instance['text'];
		$instance['button_text'] = $new_instance['button_text'];
		$instance['search_placeholder'] = $new_instance['search_placeholder'];
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : __( 'WPCargo Tracking', 'wpctp' );
		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( 'action' ); ?>"><?php _e( 'Search results redirection', 'wpctp' ); ?></label><br />
                <input type="text" id="<?php echo $this->get_field_id( 'action' ); ?>" name="<?php echo $this->get_field_name( 'action' ); ?>" value="<?php echo $instance['action']; ?>" style="width: 100%;" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Form Text', 'wpctp' ); ?></label><br />
                <textarea id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" style="width: 100%;"><?php echo $instance['text']; ?></textarea>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'button_text' ); ?>"><?php _e( 'Search button text', 'wpctp' ); ?></label><br />
                <input type="text" id="<?php echo $this->get_field_id( 'button_text' ); ?>" name="<?php echo $this->get_field_name( 'button_text' ); ?>" value="<?php echo $instance['button_text']; ?>" style="width: 100%;" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'search_placeholder' ); ?>"><?php _e( 'Search field placeholder', 'wpctp' ); ?></label><br />
                <input type="text" id="<?php echo $this->get_field_id( 'search_placeholder' ); ?>" name="<?php echo $this->get_field_name( 'search_placeholder' ); ?>" value="<?php echo $instance['search_placeholder']; ?>" style="width: 100%;" />
            </p>
		</div>
      	<div style="height:30px;"></div>
		<?php
	}
}