<?php
function aoon_theme_enqueue_styles() {
    $parent_style = 'twentyseventeen-style';

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );

    wp_enqueue_style( 'datetime-style', get_stylesheet_directory_uri() . '/assets/js/jquery.datetimepicker.min.css', array( $parent_style ), wp_get_theme()->get('Version') );

    wp_enqueue_script( 'date-inputs', get_theme_file_uri( '/assets/js/date-inputs.js' ), array('datetimepicker'), '1.0', true );
    wp_enqueue_script( 'datetimepicker', get_theme_file_uri( '/assets/js/jquery.datetimepicker.full.js' ), array(), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'aoon_theme_enqueue_styles' );


function wpb_load_widget() {
    register_widget( 'AddOrderFormWidget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
 
// Creating the widget 
class AddOrderFormWidget extends WP_Widget {
 
    function __construct() {
        parent::__construct(
        
        // Base ID of your widget
        'AddOrderFormWidget', 
        
        // Widget name will appear in UI
        __('Add Order Form', 'AddOrderFormWidget_domain'), 
        
        // Widget description
        array( 'description' => __( 'Form which allows users to add orders', 'AddOrderFormWidget_domain' ), ) 
        );
    }
    
    // Creating widget front-end
    
    public function widget( $args, $instance ) {
        $title = apply_filters( 'widget_title', $instance['title'] );
        
        // before and after widget arguments are defined by themes
        echo $args['before_widget'];
        if ( ! empty( $title ) )
            echo $args['before_title'] . $title . $args['after_title'];
    
        // This is where you run the code and display the output
        echo __($this->render($instance), 'AddOrderFormWidget_domain');
        echo $args['after_widget'];
    }
            
    private function getServiceOptions() {
        $the_query = new WP_Query(array('post_type' => 'service_type'));
        $result = '';

        if ($the_query->have_posts()):
            while ($the_query->have_posts()) : $the_query->the_post();
                $result = $result . '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
            endwhile;

            wp_reset_postdata();
            return $result;
        else:
            return '';
        endif;
    }

    private function getPlaceOptions() {
        $the_query = new WP_Query(array('post_type' => 'available_place'));
        $result = '';

        if ($the_query->have_posts()):
            while ($the_query->have_posts()) : $the_query->the_post();
                $result = $result . '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
            endwhile;

            wp_reset_postdata();
            return $result;
        else:
            return '';
        endif;
    }

    public function render($instance) {
        return '<form name="add_order" data-add_order_form data-step="0" method="post" action="add-order.php">
            <div class="row" data-form_first_step>
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Czas rozpoczęcia
                        <input type="text" name="start_time" data-datetime />
                    </label>
                </div>
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Czas zakończenia
                        <input type="text" name="end_time" data-datetime />
                    </label>
                </div>
                <div class="clearfix"></div>
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Miejsce rozpoczęcia
                        <select name="start_place" id="exampleInput" class="form-control">
                            <option value="" hidden></option>
                            ' . $this->getPlaceOptions() . '
                        </select>
                    </label>
                </div>
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Miejsce zakończenia
                        <select name="end_place" id="exampleInput" class="form-control">
                            <option value="" hidden></option>
                            ' . $this->getPlaceOptions() . '
                        </select>
                    </label>
                </div>
                <div class="clearfix"></div>
                <div class="col-xs-12 center" data-form_step_button>
                    <button type="button">Zamów</button>
                </div>
            </div>

            <div class="row" data-form_second_step style="display: none;">
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Rodzaj usługi
                        <select name="service_type" id="exampleInput" class="form-control">
                            <option value="" hidden></option>
                            ' . $this->getServiceOptions() . '
                        </select>
                    </label>
                </div>
                <div class="col-xs-6"></div>
                <div class="clearfix"></div>
                
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Imię
                        <input type="text" name="first_name" />
                    </label>
                </div>
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Nazwisko
                        <input type="text" name="last_name" />
                    </label>
                </div>
                <div class="clearfix"></div>

                <div class="col-xs-12">
                    <label for="exampleInput">
                        Ulica
                        <input type="text" name="street" />
                    </label>
                </div>

                <div class="col-xs-6">
                    <label for="exampleInput">
                        Kod pocztowy
                        <input type="text" name="postal_code" />
                    </label>
                </div>
                <div class="col-xs-6">
                    <label for="exampleInput">
                        Miasto
                        <input type="text" name="city" />
                    </label>
                </div>
                <div class="clearfix"></div>

                <div class="col-xs-6">
                    <label for="exampleInput">
                        Telefon
                        <input type="text" name="phone" />
                    </label>
                </div>
                <div class="clearfix"></div>

                <div class="col-xs-12 center">
                    <button>Zamów</button>
                </div>
            </div>
            <div class="result-success"></div>
            <div class="result-error"></div>
        </form>';
    }

    // Widget Backend 
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'New title', 'AddOrderFormWidget_domain' );
        }
        // Widget admin form
        ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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( $title ); ?>" />
            </p>
        <?php 
    }
        
    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        return $instance;
    }
} // Class wpb_widget ends here

function renderOrderForm($atts) {
    $w = new AddOrderFormWidget();
    return $w->render($atts);
}

add_shortcode('add_order_form', 'renderOrderForm');

?>