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

        // If the demo import is completed, display the "View Site" button
        if ($kindergarten_school_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.', 'kindergarten-school' ) . '</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'] ) ) {

        // 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
            $classic_kindergarten_plugin_slug = 'classic-blog-grid';
            $classic_kindergarten_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
        
            // Check if Classic Blog Grid is installed and activated
            if ( ! is_plugin_active( $classic_kindergarten_plugin_file ) ) {
        
                // Check if Classic Blog Grid is installed
                $classic_kindergarten_installed_plugins = get_plugins();
                if ( ! isset( $classic_kindergarten_installed_plugins[ $classic_kindergarten_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
                    $classic_kindergarten_upgrader = new Plugin_Upgrader();
                    $classic_kindergarten_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( $classic_kindergarten_plugin_file );
            }
        }

        // ------- Create Main Menu --------
        $classic_kindergarten_menuname = 'Primary Menu';
        $classic_kindergarten_bpmenulocation = 'primary';
        $classic_kindergarten_menu_exists = wp_get_nav_menu_object( $classic_kindergarten_menuname );
    
        if ( !$classic_kindergarten_menu_exists ) {
            $classic_kindergarten_menu_id = wp_create_nav_menu( $classic_kindergarten_menuname );

            // Create Home Page
            $classic_kindergarten_home_title = 'Home';
            $classic_kindergarten_home = array(
                'post_type'    => 'page',
                'post_title'   => $classic_kindergarten_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $classic_kindergarten_home_id = wp_insert_post($classic_kindergarten_home);
            // Assign Home Page Template
            add_post_meta($classic_kindergarten_home_id, '_wp_page_template', '/template-home-page.php');

            // Update options to set Home Page as the front page
            update_option('page_on_front', $classic_kindergarten_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($classic_kindergarten_menu_id, 0, array(
                'menu-item-title' => __('Home', 'classic-kindergarten'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_kindergarten_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $classic_kindergarten_pages_title = 'Pages';
            $classic_kindergarten_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>';
            $classic_kindergarten_pages = array(
                'post_type'    => 'page',
                'post_title'   => $classic_kindergarten_pages_title,
                'post_content' => $classic_kindergarten_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $classic_kindergarten_pages_id = wp_insert_post($classic_kindergarten_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($classic_kindergarten_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'classic-kindergarten'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_kindergarten_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $classic_kindergarten_about_title = 'About Us';
            $classic_kindergarten_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>';
            $classic_kindergarten_about = array(
                'post_type'    => 'page',
                'post_title'   => $classic_kindergarten_about_title,
                'post_content' => $classic_kindergarten_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $classic_kindergarten_about_id = wp_insert_post($classic_kindergarten_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($classic_kindergarten_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'classic-kindergarten'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $classic_kindergarten_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( $classic_kindergarten_bpmenulocation ) ) {
                $classic_kindergarten_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $classic_kindergarten_locations ) ) {
                    $classic_kindergarten_locations = array();
                }
                $classic_kindergarten_locations[ $classic_kindergarten_bpmenulocation ] = $classic_kindergarten_menu_id;
                set_theme_mod( 'nav_menu_locations', $classic_kindergarten_locations );
            }
        }

        set_theme_mod( 'kindergarten_school_the_custom_logo', esc_url( get_template_directory_uri().'/images/logo.png'));

        //Header Section
        set_theme_mod( 'kindergarten_school_topbar', true);
        set_theme_mod( 'kindergarten_school_phone_number', '01234567890');
        set_theme_mod( 'kindergarten_school_email_address', 'info@example.com');

        //Social Media Section
        set_theme_mod( 'kindergarten_school_social_facebook', '#');
        set_theme_mod( 'kindergarten_school_social_twitter', '#');
        set_theme_mod( 'kindergarten_school_social_youtube', '#');
        set_theme_mod( 'kindergarten_school_social_instagram', '#');
        set_theme_mod( 'kindergarten_school_social_linkdin', '#');

        //Slider Section
        set_theme_mod( 'kindergarten_school_slider', true);

        $kindergarten_school_featured_category_id = wp_create_category('slider');
        set_theme_mod('kindergarten_school_slider_cat', 'slider');

        $kindergarten_school_titles = array(
            'Look at School',
            'Our Classrooms',
            'Meet the Teachers'
        );                   
        $kindergarten_school_content = '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 ($kindergarten_school_i = 0; $kindergarten_school_i < 3; $kindergarten_school_i++) {
            set_theme_mod('kindergarten_school_title' . ($kindergarten_school_i + 1), $kindergarten_school_titles[$kindergarten_school_i]);

            $kindergarten_school_my_post = array(
                'post_title'    => wp_strip_all_tags($kindergarten_school_titles[$kindergarten_school_i]),
                'post_content'  => $kindergarten_school_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($kindergarten_school_featured_category_id),
            );

            $kindergarten_school_post_id = wp_insert_post($kindergarten_school_my_post);

            if (!is_wp_error($kindergarten_school_post_id)) {
                $kindergarten_school_image_url = get_theme_file_uri() . '/images/slider-img' . ($kindergarten_school_i + 1) . '.png';
                $kindergarten_school_image_id = media_sideload_image($kindergarten_school_image_url, $kindergarten_school_post_id, null, 'id');
                if (!is_wp_error($kindergarten_school_image_id)) {
                    set_post_thumbnail($kindergarten_school_post_id, $kindergarten_school_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $kindergarten_school_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($kindergarten_school_post_id, true));
            }
        }

        //Features Section
        set_theme_mod( 'kindergarten_school_hidcatfeature', true);
        set_theme_mod( 'kindergarten_school_pgboxes_title', 'Our Features');

        function get_or_create_page_by_title( $kindergarten_school_title, $kindergarten_school_content = '' ) {
            $kindergarten_school_args = array(
                'post_type'      => 'page',
                'title'          => $kindergarten_school_title,
                'post_status'    => 'publish',
                'posts_per_page' => 1,
                'fields'         => 'ids'
            );
            $kindergarten_school_query = new WP_Query( $kindergarten_school_args );
        
            if ( ! empty( $kindergarten_school_query->posts ) ) {
                return $kindergarten_school_query->posts[0];
            }
        
            // Create the page
            return wp_insert_post( array(
                'post_type'    => 'page',
                'post_title'   => $kindergarten_school_title,
                'post_content' => $kindergarten_school_content,
                'post_status'  => 'publish',
                'post_author'  => 1
            ));
        }
        
        // Common content for all pages
        $kindergarten_school_common_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';
        
        // Page definitions
        $kindergarten_school_pages = array(
            array( 'title' => 'Playground',               'image' => 'features1.png', 'mod_key' => 'kindergarten_school_pageboxes1' ),
            array( 'title' => 'Active Teachers',               'image' => 'features2.png', 'mod_key' => 'kindergarten_school_pageboxes2' ),
            array( 'title' => 'Time Management',           'image' => 'features3.png', 'mod_key' => 'kindergarten_school_pageboxes3' ),
            array( 'title' => 'Happy Infrastructure', 'image' => 'features4.png', 'mod_key' => 'kindergarten_school_pageboxes4' ),
        );
        
        // Loop to create/fetch pages and assign images
        foreach ( $kindergarten_school_pages as $kindergarten_school_page ) {
            $kindergarten_school_page_id = get_or_create_page_by_title( $kindergarten_school_page['title'], $kindergarten_school_common_content );
        
            if ( $kindergarten_school_page_id ) {
                set_theme_mod( $kindergarten_school_page['mod_key'], $kindergarten_school_page_id );
        
                $kindergarten_school_image_url = get_theme_file_uri() . '/images/' . $kindergarten_school_page['image'];
                $kindergarten_school_image_id = media_sideload_image( $kindergarten_school_image_url, $kindergarten_school_page_id, null, 'id' );
        
                if ( ! is_wp_error( $kindergarten_school_image_id ) ) {
                    set_post_thumbnail( $kindergarten_school_page_id, $kindergarten_school_image_id );
                }
            } else {
                error_log( 'Failed to create or fetch the page: ' . $kindergarten_school_page['title'] );
            }
        }

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