<div class="theme-offer">
   <?php
        // Check if the demo import has been completed
        $solar_services_demo_import_completed = get_option('solar_services_demo_import_completed', false);

        // If the demo import is completed, display the "View Site" button
        if ($solar_services_demo_import_completed) {
            echo '<br>';
            echo '<div class="success">Demo Import Successful</div>';
            echo '<br>';
            echo '<hr>';
            echo '<br>';
            echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'solar-services' ) . '</span>';
            echo '<br>';
            echo '<br>';
            echo '<br>';
            echo '<div class="view-site-btn">';
            echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
            echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
            echo '</div>';
        }
     // POST and update the customizer and other related data of Solar Services
    if ( isset( $_POST['submit'] ) ) {
        echo '<div class="plugin-notice">';
            // Check if Classic Blog Grid plugin is installed
            if (!is_plugin_active('classic-blog-grid/classic-blog-grid.php')) {
                // Plugin slug and file path for Classic Blog Grid
                $solar_services_plugin_slug = 'classic-blog-grid';
                $solar_services_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $solar_services_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $solar_services_installed_plugins = get_plugins();
                    if ( ! isset( $solar_services_installed_plugins[ $solar_services_plugin_file ] ) ) {
            
                        // Include necessary files to install plugins
                        include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
                        include_once( ABSPATH . 'wp-admin/includes/file.php' );
                        include_once( ABSPATH . 'wp-admin/includes/misc.php' );
                        include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
            
                        // Download and install Classic Blog Grid
                        $solar_services_upgrader = new Plugin_Upgrader();
                        $solar_services_upgrader->install( 'https://downloads.wordpress.org/plugin/classic-blog-grid.latest-stable.zip' );
                    }
            
                    // Activate the Classic Blog Grid plugin after installation (if needed)
                    activate_plugin( $solar_services_plugin_file );
                }
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $solar_services_menuname = 'Primary Menu';
        $solar_services_bpmenulocation = 'primary';
        $solar_services_menu_exists = wp_get_nav_menu_object( $solar_services_menuname );
    
        if ( !$solar_services_menu_exists ) {
            $solar_services_menu_id = wp_create_nav_menu( $solar_services_menuname );

            // Create Home Page
            $solar_services_home_title = 'Home';
            $solar_services_home = array(
                'post_type'    => 'page',
                'post_title'   => $solar_services_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $solar_services_home_id = wp_insert_post($solar_services_home);
            // Assign Home Page Template
            add_post_meta($solar_services_home_id, '_wp_page_template', 'templates/template-home-page.php');
            // Update options to set Home Page as the front page
            update_option('page_on_front', $solar_services_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($solar_services_menu_id, 0, array(
                'menu-item-title' => __('Home', 'solar-services'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $solar_services_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $solar_services_pages_title = 'Pages';
            $solar_services_pages_content = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';
            $solar_services_pages = array(
                'post_type'    => 'page',
                'post_title'   => $solar_services_pages_title,
                'post_content' => $solar_services_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $solar_services_pages_id = wp_insert_post($solar_services_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($solar_services_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'solar-services'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $solar_services_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $solar_services_about_title = 'About Us';
            $solar_services_about_content = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>'; 
            $solar_services_about = array(
                'post_type'    => 'page',
                'post_title'   => $solar_services_about_title,
                'post_content' => $solar_services_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $solar_services_about_id = wp_insert_post($solar_services_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($solar_services_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'solar-services'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $solar_services_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign the menu to the primary location if not already set
            if ( ! has_nav_menu( $solar_services_bpmenulocation ) ) {
                $solar_services_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $solar_services_locations ) ) {
                    $solar_services_locations = array();
                }
                $solar_services_locations[ $solar_services_bpmenulocation ] = $solar_services_menu_id;
                set_theme_mod( 'nav_menu_locations', $solar_services_locations );
            }
        }

        //Header Section
        set_theme_mod( 'solar_services_topbar', true); 
        set_theme_mod( 'solar_services_the_custom_logo', esc_url( get_template_directory_uri().'/images/logo.png'));
        set_theme_mod( 'solar_services_phone_text', '123 456 7890'); 
        set_theme_mod( 'solar_services_email_text', 'xyz123@example.com'); 
        set_theme_mod( 'solar_services_twitt_link', '#'); 
        set_theme_mod( 'solar_services_insta_link', '#'); 
        set_theme_mod( 'solar_services_pinterest_link', '#'); 
        set_theme_mod( 'solar_services_fb_link', '#'); 
        set_theme_mod( 'solar_services_headerbutton_txt', 'Contact Us'); 
        set_theme_mod( 'solar_services_headerbutton_link', '#'); 

        //Slider Section
        set_theme_mod( 'solar_services_button_text', 'Our Service'); 
        set_theme_mod( 'solar_services_button_link_slider', '#'); 
        set_theme_mod( 'solar_services_button_text2', 'More About Us'); 

        // Create the 'Solar Panel' category and retrieve its ID
        $solar_services_slider_category_id = wp_create_category('Solar Panel');
        set_theme_mod('solar_services_slidersection', $solar_services_slider_category_id);

        $solar_services_titles = array(
            'Largest Producer Of Wind And Solar Energy', 
            'Renewable Energy for a Sustainable Future', 
            'Tomorrow with Clean & Green Energy'
        );        

        // Create three demo posts and assign them to the 'Solar Panel' category
        for ($solar_services_i = 1; $solar_services_i <= 3; $solar_services_i++) {
            $title_index = $solar_services_i - 1; // Adjust for zero-based index
            set_theme_mod('solar_services_title' . $solar_services_i, $solar_services_titles[$title_index]);

            // Prepare the post object
            $solar_services_my_post = array(
                'post_title'  => wp_strip_all_tags($solar_services_titles[$title_index]),
                'post_status' => 'publish',
                'post_type'   => 'post',
                'tax_input'   => array('category' => array($solar_services_slider_category_id)), // Correct category assignment
            );

            // Insert the post into the database
            $solar_services_post_id = wp_insert_post($solar_services_my_post);

            // If the post was successfully created, set the featured image
            if (!is_wp_error($solar_services_post_id)) {
                // Set the image URL based on the current loop index
                $solar_services_image_url = esc_url(get_template_directory_uri() . '/images/slider' . $solar_services_i . '.png');
                $solar_services_upload_dir = wp_upload_dir();

                // Download the image data
                $solar_services_response = wp_remote_get($solar_services_image_url);
                if (!is_wp_error($solar_services_response)) {
                    $solar_services_image_data = wp_remote_retrieve_body($solar_services_response);

                    if (!empty($solar_services_image_data)) {
                        // Handle the file upload process
                        $solar_services_image_name = 'slider' . $solar_services_i . '.png';
                        $solar_services_unique_file_name = wp_unique_filename($solar_services_upload_dir['path'], $solar_services_image_name);
                        $solar_services_file = $solar_services_upload_dir['path'] . '/' . $solar_services_unique_file_name;

                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($solar_services_file, $solar_services_image_data);

                        // Check file type and prepare for attachment
                        $solar_services_wp_filetype = wp_check_filetype($solar_services_unique_file_name, null);
                        $solar_services_attachment = array(
                            'post_mime_type' => $solar_services_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($solar_services_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );

                        // Insert the image into the media library and set it as the post's featured image
                        $solar_services_attach_id = wp_insert_attachment($solar_services_attachment, $solar_services_file, $solar_services_post_id);
                        require_once ABSPATH . 'wp-admin/includes/image.php';
                        $solar_services_attach_data = wp_generate_attachment_metadata($solar_services_attach_id, $solar_services_file);
                        wp_update_attachment_metadata($solar_services_attach_id, $solar_services_attach_data);
                        set_post_thumbnail($solar_services_post_id, $solar_services_attach_id);
                    }
                }
            }
        }

        //Features Section
        set_theme_mod( 'solar_services_featureleftheading', 'Our Facility');
        set_theme_mod( 'solar_services_featureleftdescription', 'The sun provides us with free energy, place solar panels on roof. solar cell, solar module manufacturing facilities are considerable. we must go above our customer expectations during interaction.');
        set_theme_mod( 'solar_services_featureleftlist1', 'Best Energy Solution');
        set_theme_mod( 'solar_services_featureleftlist2', 'End-To-End Services');
        set_theme_mod( 'solar_services_featureleftlist3', 'Global Expertise');
        set_theme_mod( 'solar_services_featureleftlist4', 'Certified Engineers');
        set_theme_mod( 'solar_services_featureleftbtntext', 'Our Service');
        set_theme_mod( 'solar_services_featureleftbtnlink', '#');

        $solar_services_featured_category_id = wp_create_category('Solar Features');
        set_theme_mod('solar_services_feature_cat', $solar_services_featured_category_id);

        $solar_services_titles = array('Fast Services', 'Moneyback Guarantee', 'High Capacity', 'Easy Installation', 'Energy Sever', 'Expert Technician', 'Best Monitoring', 'Active/Quick Support');

        for ($solar_services_i = 0; $solar_services_i < 8; $solar_services_i++) {
            set_theme_mod('solar_services_title' . ($solar_services_i + 1), $solar_services_titles[$solar_services_i]);

            $solar_services_my_post = array(
                'post_title'    => wp_strip_all_tags($solar_services_titles[$solar_services_i]),
                'post_content'  => '',
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($solar_services_featured_category_id),
            );

            $solar_services_post_id = wp_insert_post($solar_services_my_post);

            if (!is_wp_error($solar_services_post_id)) {
                $solar_services_image_url = get_template_directory_uri() . '/images/solar-features/solar-feature' . ($solar_services_i + 1) . '.png';
                $solar_services_image_id = media_sideload_image($solar_services_image_url, $solar_services_post_id, null, 'id');
                if (!is_wp_error($solar_services_image_id)) {
                    set_post_thumbnail($solar_services_post_id, $solar_services_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $solar_services_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($solar_services_post_id, true));
            }
        }

        //Services Section
        set_theme_mod( 'solar_services_headingtext1', 'Our Service'); 
        set_theme_mod( 'solar_services_headingtext_para', 'What We Provide'); 
        set_theme_mod( 'solar_services_blog_count', '3'); 

        $solar_services_featured_category_id = wp_create_category('Solar Services');
        set_theme_mod('solar_services_blog_cat', $solar_services_featured_category_id);

        $solar_services_titles = array('Solar Panels', 'Solar Energy Solutions', 'Renewable Power Systems');
        $solar_services_contents = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';

        for ($solar_services_i = 0; $solar_services_i < 3; $solar_services_i++) {
            set_theme_mod('solar_services_title' . ($solar_services_i + 1), $solar_services_titles[$solar_services_i]);

            $solar_services_my_post = array(
                'post_title'    => wp_strip_all_tags($solar_services_titles[$solar_services_i]),
                'post_content'  => $solar_services_contents,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($solar_services_featured_category_id),
            );

            $solar_services_post_id = wp_insert_post($solar_services_my_post);

            if (!is_wp_error($solar_services_post_id)) {
                $solar_services_image_url = get_template_directory_uri() . '/images/solar-services/solar-service' . ($solar_services_i + 1) . '.png';
                $solar_services_image_id = media_sideload_image($solar_services_image_url, $solar_services_post_id, null, 'id');
                if (!is_wp_error($solar_services_image_id)) {
                    set_post_thumbnail($solar_services_post_id, $solar_services_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $solar_services_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($solar_services_post_id, true));
            }
        }

        // Show success message and the "View Site" button
        update_option('solar_services_demo_import_completed', true);
        echo '<br>';
        echo '<div class="success">Demo Import Successful</div>';
        echo '<br>';
        echo '<hr>';
        echo '<br>';
        echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'solar-services' ) . '</span>';
        echo '<br>';
    }
     ?>
    <ul>
        <li>
        <?php 
        // Check if the form is submitted
        if ( !isset( $_POST['submit'] ) ) : ?>
            <!-- Show demo importer form only if it's not submitted -->
            <?php if (!get_option('solar_services_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'solar-services' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'solar-services' ); ?></b><br><br>
                <small><b><?php echo esc_html('Please take a backup if your website is already live with data. This importer will overwrite existing data.', 'solar-services' ); ?></b></small><br><br>
                <form id="demo-importer-form" action="" method="POST" onsubmit="return runDemoImport();">
                    <input type="submit" name="submit" value="<?php echo esc_attr('Run Importer','solar-services'); ?>" class="button button-primary button-large">
                </form>
                <script type="text/javascript">
                    function runDemoImport() {
                        if (confirm('Do you really want to do this?')) {
                            document.getElementById('demo-import-loader').style.display = 'block';
                            return true;
                        }
                        return false;
                    }
                </script>
             <?php endif; ?>
         <?php 
        endif; 

        // Show "View Site" button after form submission
        if ( isset( $_POST['submit'] ) ) {
        echo '<div class="view-site-btn">';
        echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
        echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
        echo '</div>';
        }
        ?>
        </li>
    </ul>
 </div>