<?php
if (!class_exists( 'Sewa_Welcome_Msg_Widget' ) ) {
    
    class Sewa_Welcome_Msg_Widget extends WP_Widget
    
    {
        private function defaults()
        {
            $defaults             = array(
                'page_id'         => 0,
                'character_limit' => 25,
                'sub_title'       => esc_html__('About Sewa', 'sewa'),
                'read_more'       => esc_html__('Read More', 'sewa'),
                'image'           => '',
                'image-alignment' => 'right',
            );

            return $defaults;
        }

        public function __construct()
        {
            parent::__construct(
                'sewa-welcome-msg-widget',
                esc_html__( 'Sewa Welcome Message Widget', 'sewa' ),
                array( 'description' => esc_html__( 'Sewa Welcome Message', 'sewa' ) )
            );
        }

        public function widget( $args, $instance )
        {

            if ( !empty( $instance ) ) {
                $instance        = wp_parse_args( (array )$instance, $this->defaults() );
                $page_id         = absint($instance['page_id']);
                $limit_character = absint( $instance['character_limit'] );
                $sub_title       = esc_html( $instance['sub_title'] );
                $read_more       = esc_html( $instance['read_more'] );
                $image           = esc_html( $instance['image'] );
                $image_alignment = esc_attr($instance['image-alignment']);
                echo $args['before_widget'];
                if ( !empty( $page_id ) ) {
                    $sewa_page_args     = array(
                        'page_id'        => $page_id,
                        'posts_per_page' => 1,
                        'post_type'      => 'page',
                        'no_found_rows'  => true,
                        'post_status'    => 'publish'
                    );

                  $welcome_query = new WP_Query( $sewa_page_args );
                    if ($welcome_query->have_posts()):
                        while ($welcome_query->have_posts()):$welcome_query->the_post();

                         if (has_post_thumbnail()) {
                        $image_id   = get_post_thumbnail_id();
                        $image_url  = wp_get_attachment_image_src($image_id, 'full', true);
                        $image_path = $image_url[0];
                        }
                         ?>
                            <section id="section2" class="wel" style="background: url(<?php echo $image; ?>) no-repeat center/ cover;">
                                <div class="container">
                                    <div class="row">
                                        <div class="col-sm-7 col-md-7 <?php echo esc_attr($image_alignment) ?>">
                                            <div class="section-2-box-left section-margine">
                                                <div class="sec-title">
                                                    <?php if(!empty($sub_title)){ ?> 
                                                    <h5><?php echo esc_html($sub_title); ?>
                                                    </h5>
                                                    <?php } ?>
                                                    <h4><?php the_title(); ?></h4>
                                                </div>
                                                <p><?php echo esc_html( wp_trim_words(get_the_content(), $limit_character)); ?></p>
                                                <?php if(!empty($read_more)){ ?>
                                                <a href="<?php echo get_permalink(); ?>" class="btn btn-primary"><?php echo esc_html($read_more); ?>
                                                </a>
                                                <?php } ?>
                                            </div>
                                        </div>
                                        <div class="col-sm-5 col-md-5">
                                            <div class="section-2-box-right text-center">
                                               
                                                <img src="<?php echo $image_path; ?>" alt="<?php the_title(); ?>">
                                              
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </section>
                            <?php
                        endwhile;
                    endif;
                    wp_reset_postdata();
                    echo $args['after_widget'];
                }
            }
        }

        public function update( $new_instance, $old_instance )
        {
            $instance                    = $old_instance;
            $instance['page_id']         = absint($new_instance['page_id']);
            $instance['character_limit'] = absint( $new_instance['character_limit'] );
            $instance['sub_title']       = sanitize_text_field( $new_instance['sub_title'] );
            $instance['read_more']       = sanitize_text_field( $new_instance['read_more'] );
            $instance['image']           = sanitize_text_field( $new_instance['image'] );
            $instance['image-alignment'] = sanitize_text_field($new_instance['image-alignment']);

            return $instance;
        }

        public function form( $instance )
        
        {
            $instance        = wp_parse_args((array )$instance, $this->defaults() );
            $page_id         = absint($instance['page_id']);
            $limit_character = absint( $instance['character_limit'] );
            $sub_title       = esc_attr( $instance['sub_title'] );
            $read_more       = esc_attr( $instance['read_more'] );
            $image           = esc_attr( $instance['image'] );
            $image_alignment = esc_attr($instance['image-alignment']);

            ?>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('sub_title')); ?>"><?php esc_html_e('Subtitle', 'sewa'); ?></label><br/>
                <input type="text" name="<?php echo esc_attr( $this->get_field_name('sub_title')); ?>" class="sewa-cons" id="<?php echo esc_attr($this->get_field_id('sub_title')); ?>" value="<?php echo $sub_title ?>">
            </p>

            <p>
                <label for="<?php echo esc_attr($this->get_field_id('page_id')); ?>"><?php esc_html_e('Select Page', 'sewa'); ?></label><br/>

                <?php
                /* see more here https://codex.wordpress.org/Function_Reference/wp_dropdown_pages*/
                $args = array(
                    'selected'         => $page_id,
                    'name'             => esc_attr( $this->get_field_name('page_id') ),
                    'id'               => esc_attr( $this->get_field_id('page_id') ),
                    'class'            => 'widefat',
                    'show_option_none' => esc_html__( 'Select Page', 'sewa' ),
                );
                wp_dropdown_pages($args);
                ?>
            </p>
            <hr>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('character_limit')); ?>"><?php esc_html_e('Character Limit', 'sewa'); ?></label><br/>
                <input type="number" name="<?php echo esc_attr( $this->get_field_name('character_limit')); ?>" class="sewa-cons" id="<?php echo esc_attr($this->get_field_id('character_limit')); ?>" value="<?php echo $limit_character ?>">
            </p>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('read_more')); ?>"><?php esc_html_e('Read More', 'sewa'); ?></label><br/>
                <input type="text" name="<?php echo esc_attr( $this->get_field_name('read_more')); ?>" class="sewa-cons" id="<?php echo esc_attr($this->get_field_id('read_more')); ?>" value="<?php echo $read_more ?>">
            </p>
             <p>
                <label for="<?php echo esc_attr( $this->get_field_id('image') ); ?>">
                   <?php echo esc_html__(' Background Image', 'sewa'); ?>
                </label>
                <br/>
                <?php
                    echo '<img class="custom_media_image" src="' . esc_url($instance['image']) . '" style="margin:0;padding:0;max-width:100px;float:left;display:inline-block" /><br />';
                 ?>

                <input type="text" class="widefat custom_media_url " name="<?php echo esc_attr($this->get_field_name('image')); ?>" id="<?php echo esc_attr($this->get_field_id('image')); ?>" value="<?php echo $image; ?>"/>
                <input type="button" class="button button-primary media-image-upload custom_media_button widefat" id="custom_media_button" name="<?php echo esc_attr($this->get_field_name('image')); ?>" value="<?php esc_attr_e('Upload Image', 'sewa') ?>"/> 


            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('image-alignment')); ?>"><strong><?php esc_html_e('Image Alignment:', 'sewa'); ?></strong></label>
                <?php
                $this->dropdown_image_alignment(array(
                        'id' => esc_attr($this->get_field_id('image-alignment')),
                        'name' => esc_attr($this->get_field_name('image-alignment')),
                        'selected' => $image_alignment
                    )
                );
                ?>
            </p>
            <?php
        }
          function dropdown_image_alignment($args)
        {
            $defaults = array(
                                'id'       => '',
                                'name'     => '',
                                'selected' => 0,
                              );

            $r = wp_parse_args($args, $defaults);
            $output = '';

            $choices = array(
                            'left'  => __('Left', 'sewa'),
                            'right' => __('Right', 'sewa')
                            );

            if (!empty($choices))
            {

                $output = "<select name='" . esc_attr($r['name']) . "' id='" . esc_attr($r['id']) . "'>\n";
                foreach ($choices as $key => $choice) {
                    $output .= '<option value="' . esc_attr($key) . '" ';
                    $output .= selected($r['selected'], $key, false);
                    $output .= '>' . esc_html($choice) . '</option>\n';
                }
                $output .= "</select>\n";
            }

            echo $output;
        }

    }

}

add_action( 'widgets_init', 'sewa_welcome_msg_widget' );

function sewa_welcome_msg_widget()
{
    register_widget( 'Sewa_Welcome_Msg_Widget' );

}