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

        // If the demo import is completed, display the "View Site" button
        if ($robotics_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.', 'robotics-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 Robotics 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
                $robotics_services_plugin_slug = 'classic-blog-grid';
                $robotics_services_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $robotics_services_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $robotics_services_installed_plugins = get_plugins();
                    if ( ! isset( $robotics_services_installed_plugins[ $robotics_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
                        $robotics_services_upgrader = new Plugin_Upgrader();
                        $robotics_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( $robotics_services_plugin_file );
                }
            }
        echo '</div>';
        // ------- Create Main Menu --------
        $robotics_services_menuname = 'Primary Menu'; 
        $robotics_services_bpmenulocation = 'primary';
        $robotics_services_menu_exists = wp_get_nav_menu_object($robotics_services_menuname);

        if (!$robotics_services_menu_exists) {
            // Create a new menu
            $robotics_services_menu_id = wp_create_nav_menu($robotics_services_menuname);

            // Define pages to be created
            $robotics_services_pages = array(
                'home' => array(
                    'title' => 'Home',
                    'template' => '/templates/template-home-page.php'
                ),
                'about-us' => array(
                    'title' => 'About Us',
                    '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>'
                ),
                'services' => array(
                    'title' => 'Services',
                    '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>'
                ),
                'pages' => array(
                    'title' => '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>'
                ),
                'blogs' => array(
                    'title' => 'Blogs',
                    '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>'
                )
            );

            $robotics_services_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($robotics_services_pages as $robotics_services_slug => $robotics_services_data) {
                $robotics_services_existing_page = get_page_by_path($robotics_services_slug);

                if ($robotics_services_existing_page) {
                    // If the page already exists, use its ID
                    $robotics_services_page_id = $robotics_services_existing_page->ID;
                } else {
                    // Create a new page
                    $robotics_services_page_data = array(
                        'post_type'    => 'page',
                        'post_title'   => $robotics_services_data['title'],
                        'post_content' => isset($robotics_services_data['content']) ? $robotics_services_data['content'] : '',
                        'post_status'  => 'publish',
                        'post_author'  => get_current_user_id(), // Set author dynamically
                        'post_name'    => $robotics_services_slug,
                    );

                    $robotics_services_page_id = wp_insert_post($robotics_services_page_data);

                    // Assign custom page template if specified
                    if (!empty($robotics_services_data['template'])) {
                        update_post_meta($robotics_services_page_id, '_wp_page_template', $robotics_services_data['template']);
                    }
                }

                // Store the page IDs
                $robotics_services_page_ids[$robotics_services_slug] = $robotics_services_page_id;
            }

            // Set homepage and blog page
            update_option('page_for_posts', $robotics_services_page_ids['blogs']);
            update_option('page_on_front', $robotics_services_page_ids['home']);
            update_option('show_on_front', 'page');

            // Define menu items
            $robotics_services_menu_items = array(
                'home',
                'about-us',
                'services',
                'pages',
                'blogs'
            );

            // Add menu items dynamically
            foreach ($robotics_services_menu_items as $robotics_services_slug) {
                wp_update_nav_menu_item($robotics_services_menu_id, 0, array(
                    'menu-item-title' => esc_html($robotics_services_pages[$robotics_services_slug]['title']),
                    'menu-item-url' => get_permalink($robotics_services_page_ids[$robotics_services_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $robotics_services_page_ids[$robotics_services_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $robotics_services_locations = get_theme_mod('nav_menu_locations', array());
            $robotics_services_locations[$robotics_services_bpmenulocation] = $robotics_services_menu_id;
            set_theme_mod('nav_menu_locations', $robotics_services_locations);
        }

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

        //Header Section       
        set_theme_mod('robotics_services_header_btn_text', 'Get a Quote');   
        set_theme_mod('robotics_services_header_btn_url', '#');   

        //Slider Section
        set_theme_mod( 'robotics_services_banner', true);
        set_theme_mod('robotics_services_banner_small_title', 'Welcome To CT Robotic services Company'); 
        set_theme_mod('robotics_services_button_text', 'Explore More');   

        // Function to fetch or create a page using WP_Query
        function get_or_create_page_by_title( $robotics_services_page_title, $robotics_services_page_content = '' ) {
            $robotics_services_args = array(
                'post_type'      => 'page',
                'title'          => $robotics_services_page_title,
                'post_status'    => 'publish',
                'posts_per_page' => 1,
                'fields'         => 'ids'
            );
            $robotics_services_query = new WP_Query( $robotics_services_args );

            if ( ! empty( $robotics_services_query->posts ) ) {
                return $robotics_services_query->posts[0];
            } else {
                // Create the page if it doesn't exist
                $robotics_services_page_id = wp_insert_post( array(
                    'post_type'    => 'page',
                    'post_title'   => $robotics_services_page_title,
                    'post_content' => $robotics_services_page_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1
                ));
                return $robotics_services_page_id;
            }
        }

        // Create Page
        $robotics_services_page_title = 'Innovating Tomorrow with Smart Robotics';
        $robotics_services_page_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type.';
        $robotics_services_page_id = get_or_create_page_by_title( $robotics_services_page_title, $robotics_services_page_content );

        if ( $robotics_services_page_id ) {
            set_theme_mod( 'robotics_services_banner_pageboxes', $robotics_services_page_id );
        } else {
            error_log('Failed to create or fetch the "Welcome to Corporate Business Theme" page.');
        }

        $robotics_services_image_url = get_template_directory_uri().'/images/slider.png';
        $robotics_services_image_id = media_sideload_image($robotics_services_image_url, $robotics_services_page_id, null, 'id');
        if (!is_wp_error($robotics_services_image_id)) {
            // Set the downloaded image as the post's featured image
            set_post_thumbnail($robotics_services_page_id, $robotics_services_image_id);
        }  

        //Case Study Section
        set_theme_mod('robotics_services_disabled_case_study_section', true);  
        set_theme_mod('robotics_services_case_study_text', 'Case Studies');   
        set_theme_mod('robotics_services_case_study_title', 'Explore our case studies');  
        set_theme_mod('robotics_services_case_btn_text', 'Explore More');   
        set_theme_mod('robotics_services_case_btn_url', '#');  

        $robotics_services_service_titles = array('Surgical Robotic For Medical Field', 'Industrial Robotics & AI Install');
        $robotics_services_image_urls = array(
            get_template_directory_uri() . '/images/case-study1.png',
            get_template_directory_uri() . '/images/case-study2.png',
        );

        // Loop to create posts for each service
        for ($robotics_services_i = 0; $robotics_services_i < 2; $robotics_services_i++) {
            $robotics_services_title = $robotics_services_service_titles[$robotics_services_i];
            $robotics_services_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ';

            // Create the post object
            $robotics_services_post_id = wp_insert_post(array(
                'post_title'    => wp_strip_all_tags($robotics_services_title),
                'post_content'  => $robotics_services_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
            ));

            // If the post was successfully created
            if ($robotics_services_post_id && !is_wp_error($robotics_services_post_id)) {
                // Set the theme mod for selecting this post
                set_theme_mod('robotics_services_select_post' . ($robotics_services_i + 1), $robotics_services_post_id);

                // Handle the featured image using media_sideload_image
                $robotics_services_image_url = $robotics_services_image_urls[$robotics_services_i];
                $robotics_services_image_id = media_sideload_image($robotics_services_image_url, $robotics_services_post_id, null, 'id');

                if (!is_wp_error($robotics_services_image_id)) {
                    set_post_thumbnail($robotics_services_post_id, $robotics_services_image_id);
                } else {
                    error_log("Failed to download image for $robotics_services_title: " . $robotics_services_image_id->get_error_message());
                }
            } else {
                error_log("Failed to create post: " . print_r($robotics_services_post_id, true));
            }
        }

        // Show success message and the "View Site" button
        update_option('robotics_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.', 'robotics-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('robotics_services_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'robotics-services' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'robotics-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.', 'robotics-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','robotics-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>



 