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

        // If the demo import is completed, display the "View Site" button
        if ($juice_center_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.', 'juice-center' ) . '</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>';
        }
    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
                $juice_center_plugin_slug = 'classic-blog-grid';
                $juice_center_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $juice_center_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $juice_center_installed_plugins = get_plugins();
                    if ( ! isset( $juice_center_installed_plugins[ $juice_center_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
                        $juice_center_upgrader = new Plugin_Upgrader();
                        $juice_center_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( $juice_center_plugin_file );
                }
            }
            
            // Check if woocommerce is installed and activated
            if (!is_plugin_active('woocommerce/woocommerce.php')) {
                // Install the plugin if it doesn't exist
                $juice_center_plugin_slug = 'woocommerce';
                $juice_center_plugin_file = 'woocommerce/woocommerce.php';

                // Check if plugin is installed
                $juice_center_installed_plugins = get_plugins();
                if (!isset($juice_center_installed_plugins[$juice_center_plugin_file])) {
                    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');

                    // Install the plugin
                    $juice_center_upgrader = new Plugin_Upgrader();
                    $juice_center_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($juice_center_plugin_file);
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $juice_center_menuname = 'Primary Menu';
        $juice_center_bpmenulocation = 'primary';
        $juice_center_menu_exists = wp_get_nav_menu_object( $juice_center_menuname );
    
        if ( !$juice_center_menu_exists ) {
            $juice_center_menu_id = wp_create_nav_menu( $juice_center_menuname );

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

            // Create a new Page 
            $juice_center_pages_title = 'Pages';
            $juice_center_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>';
            $juice_center_pages = array(
                'post_type'    => 'page',
                'post_title'   => $juice_center_pages_title,
                'post_content' => $juice_center_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $juice_center_pages_id = wp_insert_post($juice_center_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($juice_center_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'juice-center'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $juice_center_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $juice_center_about_title = 'About Us';
            $juice_center_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>';
            $juice_center_about = array(
                'post_type'    => 'page',
                'post_title'   => $juice_center_about_title,
                'post_content' => $juice_center_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $juice_center_about_id = wp_insert_post($juice_center_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($juice_center_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'juice-center'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $juice_center_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( $juice_center_bpmenulocation ) ) {
                $juice_center_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $juice_center_locations ) ) {
                    $juice_center_locations = array();
                }
                $juice_center_locations[ $juice_center_bpmenulocation ] = $juice_center_menu_id;
                set_theme_mod( 'nav_menu_locations', $juice_center_locations );
            }
        }

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

        //Header Section
        set_theme_mod( 'classic_bakery_shop_text', 'Shop'); 
        set_theme_mod( 'classic_bakery_shop_link', '#'); 

        //Social Media Section
        set_theme_mod( 'classic_bakery_fb_link', '#'); 
        set_theme_mod( 'classic_bakery_twitt_link', '#');
        set_theme_mod( 'classic_bakery_insta_link', '#');

        //Slider Section
        set_theme_mod( 'classic_bakery_hide_categorysec', true); 
        set_theme_mod( 'classic_bakery_button_text', 'Shop'); 
        set_theme_mod( 'classic_bakery_button_link_slider', '#');
        
        // Create the 'Bakery' category and retrieve its ID
        $classic_bakery_slider_category_id = wp_create_category('Shake');
        
        // Set the category in theme mods for the slider section
        if (!is_wp_error($classic_bakery_slider_category_id)) {
            set_theme_mod('classic_bakery_slidersection', $classic_bakery_slider_category_id); // Update with correct category ID
        }

        $classic_bakery_titles = array(
                'Best Shake In Town',
                'Fresh Ingredients',
                'Made to Order'          
        );        
        $classic_bakery_content = 'Los Angeles, California.';
        // Create three demo posts and assign them to the 'Bakery' category
        for ($classic_bakery_i = 0; $classic_bakery_i <= 2; $classic_bakery_i++) {
            set_theme_mod('classic_bakery_title' . ($classic_bakery_i + 1), $classic_bakery_titles[$classic_bakery_i]);
    
            // Prepare the post object
            $classic_bakery_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_bakery_titles[$classic_bakery_i]),
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_bakery_slider_category_id),
            );
    
            // Insert the post into the database
            $classic_bakery_post_id = wp_insert_post($classic_bakery_my_post);
    
            // If the post was successfully created, set the featured image
            if (!is_wp_error($classic_bakery_post_id)) {
                $classic_bakery_image_url = get_theme_file_uri() . '/images/slider' . ($classic_bakery_i + 1) . '.png';
                $classic_bakery_image_id = media_sideload_image($classic_bakery_image_url, $classic_bakery_post_id, null, 'id');
                if (!is_wp_error($classic_bakery_image_id)) {
                    set_post_thumbnail($classic_bakery_post_id, $classic_bakery_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $classic_bakery_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($classic_bakery_post_id, true));
            }
        }

        // Category Section
        set_theme_mod( 'juice_center_product_cat', true); 
        set_theme_mod( 'juice_center_product_title', 'Feature Item'); 
        set_theme_mod( 'juice_center_product_text', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor'); 

         // Set the theme mod for the product category
         set_theme_mod('juice_center_hot_products_cat', 'productcategory1');

         // Define the single product category name, product titles, and tags
         $juice_center_category_name = 'productcategory1';
         $juice_center_titles = array(
             "Lemon Shake",
             "Orange Shake",
             "Strawberry Shake"
         );
 
         // Create or retrieve the product category term ID
         $juice_center_term = term_exists($juice_center_category_name, 'product_cat');
         if (!$juice_center_term) {
             $juice_center_term = wp_insert_term($juice_center_category_name, 'product_cat');
         }
 
         if (is_wp_error($juice_center_term)) {
             error_log('Error creating category: ' . $juice_center_term->get_error_message());
             return; // Exit if category creation fails
         }
 
         $juice_center_term_id = is_array($juice_center_term) ? $juice_center_term['term_id'] : $juice_center_term;
 
         // Loop to create 4 products for the category
         foreach ($juice_center_titles as $index => $juice_center_title) {
             // Create product content
             $juice_center_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
 
             // Create product post object
             $juice_center_post_id = wp_insert_post(array(
                 'post_title'    => wp_strip_all_tags($juice_center_title),
                 'post_content'  => $juice_center_content,
                 'post_status'   => 'publish',
                 'post_type'     => 'product', // Post type set to 'product'
             ));
 
             if (is_wp_error($juice_center_post_id)) {
                 error_log('Error creating product: ' . $juice_center_post_id->get_error_message());
                 continue; // Skip to the next product if creation fails
             }
 
             // Assign the category to the product
             wp_set_object_terms($juice_center_post_id, $juice_center_term_id, 'product_cat');
 
             // Set product price
             update_post_meta($juice_center_post_id, '_price', '50.00'); // Set a price for the product
 
             // Handle the featured image using media_sideload_image
             $juice_center_image_url = get_theme_file_uri() . '/images/products/product' . ($index + 1) . '.png';
             $juice_center_image_id = media_sideload_image($juice_center_image_url, $juice_center_post_id, null, 'id');
 
             if (!is_wp_error($juice_center_image_id)) {
                 // Assign featured image to product
                 set_post_thumbnail($juice_center_post_id, $juice_center_image_id);
             } else {
                 error_log('Error downloading image for product: ' . $juice_center_image_id->get_error_message());
             }
         }
                    
        // Show success message and the "View Site" button
        update_option('juice_center_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.', 'juice-center' ) . '</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('juice_center_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'juice-center' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'juice-center' ); ?></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.', 'juice-center' ); ?></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','juice-center'); ?>" 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>