<?php
if (!class_exists( 'Sewa_Our_mission_Widget' )) {
    class Sewa_Our_mission_Widget extends WP_Widget
    {
        private function defaults()
        {
            $defaults = array(
                'page_id'           => '',
                'button-text'       => esc_html__('Learn More','sewa'),
                'button-text-link'  => '#',
                'background-image'  => '',
                 'image-alignment' => 'right',
            );
            return $defaults;
        }

        public function __construct()
        {
            parent::__construct(
                'sewa-our-mission-widget',
                 esc_html__('Sewa Our Misson Widget', 'sewa'),
                 array('description' => esc_html__(' Sewa Our Mission Page', 'sewa'))
            );
        }

        public function widget($args, $instance)
        {

            if ( !empty($instance ) ) {
                
                $instance        = wp_parse_args((array)$instance, $this->defaults());
                
                $page_id         = absint($instance['page_id']);
                
                $button_text     = esc_html($instance['button-text']);
                
                $button_url      = esc_url($instance['button-text-link']);
                
                $bgimage         = esc_url($instance['background-image']);
                
               
                $image_alignment = esc_attr($instance['image-alignment']);
                
                echo $args['before_widget'];
                
                $sewa_page_args = array(
                    'page_id'        => $page_id,
                    'posts_per_page' => 1,
                    'post_type'      => 'page',
                    'no_found_rows'  => true,
                    'post_status'    => 'publish'
                );
                $mission_query = new WP_Query($sewa_page_args);
                if ($mission_query->have_posts()):
                    while ($mission_query->have_posts()):$mission_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];
                        }

                        if (empty($image_path)) {
                            $value = 12;
                        }
                        else
                        {
                            $value = 12;
                        }
                        ?>

                        <section id="mission" class="section-margine section-5-background" style="background: url(<?php echo $bgimage; ?>) 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-5-box-text-cont text-left">
                                            <h2><?php the_title(); ?></h2>
                                            <p><?php echo esc_html( wp_trim_words(get_the_content(), 20) ); ?></p>
                                            <?php
                                                if (!empty( $button_text ) && !empty($button_url) ) {
                                                    ?>
                                                    <a href="<?php echo $button_url; ?>
                                                    " class="btn btn-primary">
                                                      <?php echo $button_text; ?> </a>
                                                <?php } 
                                            ?>
                                        </div>
                                    </div>
                                    <div class="col-sm-5 col-md-5">
                                            <div class="section-2-box-right text-center">
                                              <?php if(isset($image_path)){ ?>
                                                <img src="<?php echo  $image_path ?>" alt="<?php the_title(); ?>">
                                              <?php } ?>
                                            </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['button-text']      = sanitize_text_field($new_instance['button-text']);
             $instance['button-text-link'] = esc_url_raw($new_instance['button-text-link']);
             $instance['background-image'] = esc_url_raw($new_instance['background-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']);
             $button_text     = esc_html($instance['button-text']);
             $button_url      = esc_url($instance['button-text-link']);
             $bgimage         = esc_url($instance['background-image']);
             $image_alignment = esc_attr($instance['image-alignment']);
            
            ?>

            <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'             => $this->get_field_name( 'page_id' ),
                    'id'               => $this->get_field_id( 'page_id' ),
                    'class'            => 'widefat',
                    'show_option_none' => esc_attr__( 'Select Page', 'sewa' )
                );

                wp_dropdown_pages($args);
                
                ?>
            </p>
            <hr>
            
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('button-text')); ?>">
                    <?php esc_html_e('Button Text', 'sewa'); ?>
                </label><br/>
                
                <input id="<?php echo esc_attr( $this->get_field_id( 'button-text' )); ?>" type="text" name="<?php echo esc_attr( $this->get_field_name( 'button-text' ) ); ?>" class="widefat" value="<?php echo $button_text; ?>">
            </p>
            <hr>
         
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('button-text-link')); ?>"><?php esc_html_e('Button Link', 'sewa'); ?></label><br/>
                <input type="text" name="<?php echo esc_attr( $this->get_field_name('button-text-link') ); ?>" class="widefat"  id="<?php echo esc_attr( $this->get_field_id('button-text-link')); ?>"  value="<?php echo $button_url; ?>">
            </p>

            <hr>
			<?php
					if(isset($instance['image'])){ ?>
             <p>
                <label for="<?php echo esc_attr( $this->get_field_id('image') ); ?>">
                   <?php echo esc_html__('Image', 'sewa'); ?>
                </label>
                <br/>
                <?php
						$image = $instance['image'];
                    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>
			 <?php }
					
                 ?>
             <hr>
            <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>
            <hr>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('background-image') ); ?>">
                    <?php esc_html_e('Background Image', 'sewa'); ?>
                </label>
                <br/>
         
                <?php
         
                if ( !empty( $bgimage ) ) :
         
                    echo '<img class="custom_media_image widefat" src="' . esc_url( $instance['background-image'], 'sewa') . '" style="margin:0;padding:0;max-width:100px;float:left;display:inline-block" /><br />';
                endif;
         
                ?>
         
                <input type="text" class="widefat custom_media_url" name="<?php echo esc_attr($this->get_field_name('background-image')); ?>"  id="<?php echo esc_attr($this->get_field_id('background-image')); ?>" value="<?php echo $bgimage; ?>">
         
                <input type="button" class="button button-primary custom_media_button  media-image-upload" id="custom_media_button"
                       name="<?php echo esc_attr( $this->get_field_name('background-image')); ?>"
                       value="<?php esc_attr_e('Upload Image', 'sewa') ?>"/>
            </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_our_mission_widget' );

function sewa_our_mission_widget()
{
    register_widget( 'Sewa_Our_mission_Widget' );

}















