<?php 
class newscope_company_Info_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'newscope_company_Info_widget',
            esc_html__('newscope : Company Info','newscope'),
            array(
                'description' => esc_html__( 'newscope Company Info WIdget ','newscope'),
            )
        );
    }

    public function widget($args, $instance) {
        echo wp_kses_post( $args['before_widget'] );
        
        if (!empty($instance['title'])) {
            echo wp_kses_post( $args['before_title'] ) . apply_filters('widget_title', $instance['title']) . wp_kses_post( $args['after_title'] );
        }

        // Add social media links
        $social_links = array(
            'facebook-f' => 'Facebook',
            'twitter' => 'Twitter',
            'instagram' => 'Instagram',
            'linkedin-in' => 'LinkedIn',
            'pinterest' => 'Pinterest',
            'youtube' => 'YouTube'
        );

        ?>
        <div class="newscope-company-info">
            <div class="company-info-top">

                <?php if (!empty($instance['image_url'])) : ?>
                    <div class="company-info-image">
                        <img src="<?php echo esc_url($instance['image_url']); ?>">
                    </div>
                <?php endif; ?>

            </div>

            <div class="company-info-content">
                <?php 
                    if (!empty($instance['description'])) {
                        echo '<p>' . esc_html($instance['description']) . '</p>';
                    }
                ?>

                <?php if( !empty( $social_links ) ) : ?>
                <div class="social-links">
                    <ul>
                       <?php 
                        foreach ($social_links as $key => $label) :
                        if (!empty($instance[$key])) : ?>
                            <li><a href="<?php echo esc_url( esc_url($instance[$key]) ); ?>" target="_blank" title="<?php echo esc_attr($label); ?>" class="<?php echo esc_attr($label); ?>"><i class="fab fa-<?php echo esc_attr($key); ?>"></i></a></li>
                        <?php endif; endforeach; ?>
                    </ul>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php 
        echo wp_kses_post($args['after_widget']);
    }

    public function form($instance) {
        $title = !empty($instance['title']) ? $instance['title'] : '';
        $image_url = !empty($instance['image_url']) ? $instance['image_url'] : '';
        $description = !empty($instance['description']) ? $instance['description'] : '';

        $social_links = array(
            'facebook-f' => 'Facebook',
            'twitter' => 'Twitter',
            'instagram' => 'Instagram',
            'linkedin-in' => 'LinkedIn',
            'pinterest' => 'Pinterest',
            'youtube' => 'YouTube'
        );

        ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id('title') ); ?>"> <?php echo esc_html__( 'Title:', 'newscope' ); ?> </label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_attr($title); ?>">
        </p>

        <p>
            <label for="<?php echo esc_attr( $this->get_field_id('image_url') ); ?>"><?php echo esc_html__( 'Author Image:', 'newscope' ); ?></label>
            <input class="widefat widget-image-url" id="<?php echo esc_attr( $this->get_field_id('image_url') ); ?>" name="<?php echo esc_attr( $this->get_field_name('image_url') ); ?>" type="text" value="<?php echo esc_url($image_url); ?>">
            <button class="upload-image-button button button-primary"><?php echo esc_html__( 'Upload Image:', 'newscope' ); ?></button>
        </p>

        <p>
            <label for="<?php echo esc_attr( $this->get_field_id('description') ); ?>"><?php echo esc_html__( 'Description:', 'newscope' ); ?></label>
            <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id('description') ); ?>" name="<?php echo esc_attr( $this->get_field_name('description') ); ?>" rows="8"><?php echo esc_textarea($description); ?></textarea>
        </p>

        <?php
        foreach ($social_links as $key => $label) {
            $link_value = !empty($instance[$key]) ? $instance[$key] : '';
            ?>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id($key) ); ?>"><?php echo $label; ?> <?php echo esc_html__( 'URL:', 'newscope' ); ?>:</label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id($key) ); ?>" name="<?php echo esc_attr( $this->get_field_name($key) ); ?>" type="text" value="<?php echo esc_url($link_value); ?>">
            </p>
            <?php
        }
    }

    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        $instance['image_url'] = (!empty($new_instance['image_url'])) ? esc_url_raw($new_instance['image_url']) : '';
        $instance['description'] = (!empty($new_instance['description'])) ? sanitize_textarea_field($new_instance['description']) : '';
    
        $social_links = array(
            'facebook-f',
            'twitter',
            'instagram',
            'linkedin-in',
            'pinterest',
            'youtube'
        );
    
        foreach ($social_links as $key) {
            $instance[$key] = (!empty($new_instance[$key])) ? esc_url_raw($new_instance[$key]) : '';
        }
    
        return $instance;
    }
}

function newscope_widget_uploader_script() {
    // Register the script
    wp_register_script('widget-uploader', get_template_directory_uri() . '/assets/js/widget-uploader.js', array('jquery'), '1.0.0', true);

    // Enqueue the script only on the widgets admin page
    if (is_admin() && get_current_screen()->id === 'widgets') {
        wp_enqueue_media(); // Enqueue the WordPress media uploader
        wp_enqueue_script('widget-uploader');
    }
}
add_action('admin_enqueue_scripts', 'newscope_widget_uploader_script');
?>