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

        // If the demo import is completed, display the "View Site" button
        if ($solar_panels_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-panels' ) . '</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
    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_panels_plugin_slug = 'classic-blog-grid';
                $solar_panels_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $solar_panels_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $solar_panels_installed_plugins = get_plugins();
                    if ( ! isset( $solar_panels_installed_plugins[ $solar_panels_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_panels_upgrader = new Plugin_Upgrader();
                        $solar_panels_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_panels_plugin_file );
                }
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $solar_panels_menuname = 'Primary Menu';
        $solar_panels_bpmenulocation = 'primary';
        $solar_panels_menu_exists = wp_get_nav_menu_object( $solar_panels_menuname );
    
        if ( !$solar_panels_menu_exists ) {
            $solar_panels_menu_id = wp_create_nav_menu( $solar_panels_menuname );

            // Create Home Page
            $solar_panels_home_title = 'Home';
            $solar_panels_home = array(
                'post_type'    => 'page',
                'post_title'   => $solar_panels_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $solar_panels_home_id = wp_insert_post($solar_panels_home);
            // Assign Home Page Template
            add_post_meta($solar_panels_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_panels_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($solar_panels_menu_id, 0, array(
                'menu-item-title' => __('Home', 'solar-panels'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $solar_panels_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $solar_panels_pages_title = 'Pages';
            $solar_panels_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_panels_pages = array(
                'post_type'    => 'page',
                'post_title'   => $solar_panels_pages_title,
                'post_content' => $solar_panels_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $solar_panels_pages_id = wp_insert_post($solar_panels_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($solar_panels_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'solar-panels'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $solar_panels_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $solar_panels_about_title = 'About Us';
            $solar_panels_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_panels_about = array(
                'post_type'    => 'page',
                'post_title'   => $solar_panels_about_title,
                'post_content' => $solar_panels_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $solar_panels_about_id = wp_insert_post($solar_panels_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($solar_panels_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'solar-panels'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $solar_panels_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_panels_bpmenulocation ) ) {
                $solar_panels_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $solar_panels_locations ) ) {
                    $solar_panels_locations = array();
                }
                $solar_panels_locations[ $solar_panels_bpmenulocation ] = $solar_panels_menu_id;
                set_theme_mod( 'nav_menu_locations', $solar_panels_locations );
            }
        }

        set_theme_mod( 'solar_services_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));

        //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_theme_file_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);
                    }
                }
            }
        }

        //Services Section
        set_theme_mod( 'solar_services_headingtext1', 'Our Projects'); 
        set_theme_mod( 'solar_services_headingtext_para', 'Project Details'); 
        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('Solartech AC Solar Pumping System', '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_theme_file_uri() . '/images/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_panels_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-panels' ) . '</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_panels_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'solar-panels' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'solar-panels' ); ?></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-panels' ); ?></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-panels'); ?>" 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>