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

        // If the demo import is completed, display the "View Site" button
        if ($classic_toy_store_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.', 'classic-toy-store' ) . '</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 woocommerce is installed and activated
            if (!is_plugin_active('woocommerce/woocommerce.php')) {
                // Install the plugin if it doesn't exist
                $classic_toy_store_plugin_slug = 'woocommerce';
                $classic_toy_store_plugin_file = 'woocommerce/woocommerce.php';

                // Check if plugin is installed
                $classic_toy_store_installed_plugins = get_plugins();
                if (!isset($classic_toy_store_installed_plugins[$classic_toy_store_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
                    $classic_toy_store_upgrader = new Plugin_Upgrader();
                    $classic_toy_store_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($classic_toy_store_plugin_file);
            }

            // 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_toy_store_plugin_slug = 'classic-blog-grid';
                $classic_toy_store_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $classic_toy_store_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $classic_toy_store_installed_plugins = get_plugins();
                    if ( ! isset( $classic_toy_store_installed_plugins[ $classic_toy_store_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_toy_store_upgrader = new Plugin_Upgrader();
                        $classic_toy_store_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_toy_store_plugin_file );
                }
            }
        echo '</div>';
        
        // ------- Create Main Menu --------
        $classic_toy_store_menuname       = 'Primary Menu';
        $classic_toy_store_bpmenulocation = 'primary';
        $classic_toy_store_menu_exists   = wp_get_nav_menu_object( $classic_toy_store_menuname );

        if ( ! $classic_toy_store_menu_exists ) {

            $classic_toy_store_menu_id = wp_create_nav_menu( $classic_toy_store_menuname );

            /* ---------------- Home Page ---------------- */

            $classic_toy_store_home = array(
                'post_type'   => 'page',
                'post_title'  => 'Home',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_name'   => 'home',
            );

            $classic_toy_store_home_id = wp_insert_post( $classic_toy_store_home );

            if ( ! is_wp_error( $classic_toy_store_home_id ) ) {

                add_post_meta(
                    $classic_toy_store_home_id,
                    '_wp_page_template',
                    'templates/template-home-page.php'
                );

                update_option( 'page_on_front', $classic_toy_store_home_id );
                update_option( 'show_on_front', 'page' );

                wp_update_nav_menu_item( $classic_toy_store_menu_id, 0, array(
                    'menu-item-title'     => __( 'Home', 'classic-toy-store' ),
                    'menu-item-object'    => 'page',
                    'menu-item-object-id' => $classic_toy_store_home_id,
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',
                ) );
            }

            /* ---------------- Shop Page ---------------- */

            $classic_toy_store_shop = array(
                'post_type'   => 'page',
                'post_title'  => 'Shop',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_name'   => 'shop',
            );

            $classic_toy_store_shop_id = wp_insert_post( $classic_toy_store_shop );

            if ( ! is_wp_error( $classic_toy_store_shop_id ) ) {

                update_option( 'woocommerce_shop_page_id', $classic_toy_store_shop_id );

                wp_update_nav_menu_item( $classic_toy_store_menu_id, 0, array(
                    'menu-item-title'     => __( 'Shop', 'classic-toy-store' ),
                    'menu-item-object'    => 'page',
                    'menu-item-object-id' => $classic_toy_store_shop_id,
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',
                ) );
            }

            /* ---------------- Blog Page ---------------- */

            $classic_toy_store_blog = array(
                'post_type'   => 'page',
                'post_title'  => 'Blog',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_name'   => 'blog',
            );

            $classic_toy_store_blog_id = wp_insert_post( $classic_toy_store_blog );

            if ( ! is_wp_error( $classic_toy_store_blog_id ) ) {

                update_option( 'page_for_posts', $classic_toy_store_blog_id );

                wp_update_nav_menu_item( $classic_toy_store_menu_id, 0, array(
                    'menu-item-title'     => __( 'Blog', 'classic-toy-store' ),
                    'menu-item-object'    => 'page',
                    'menu-item-object-id' => $classic_toy_store_blog_id,
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',
                ) );
            }

            /* ---------------- About Us Page ---------------- */

            $classic_toy_store_about = array(
                'post_type'    => 'page',
                'post_title'   => 'About Us',
                'post_content' => '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'about-us',
            );

            $classic_toy_store_about_id = wp_insert_post( $classic_toy_store_about );

            if ( ! is_wp_error( $classic_toy_store_about_id ) ) {

                wp_update_nav_menu_item( $classic_toy_store_menu_id, 0, array(
                    'menu-item-title'     => __( 'About Us', 'classic-toy-store' ),
                    'menu-item-object'    => 'page',
                    'menu-item-object-id' => $classic_toy_store_about_id,
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',
                ) );
            }

            /* ---------------- Contact Us Page ---------------- */

            $classic_toy_store_contact = array(
                'post_type'    => 'page',
                'post_title'   => 'Contact Us',
                'post_content' => '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'contact-us',
            );

            $classic_toy_store_contact_id = wp_insert_post( $classic_toy_store_contact );

            if ( ! is_wp_error( $classic_toy_store_contact_id ) ) {

                wp_update_nav_menu_item( $classic_toy_store_menu_id, 0, array(
                    'menu-item-title'     => __( 'Contact Us', 'classic-toy-store' ),
                    'menu-item-object'    => 'page',
                    'menu-item-object-id' => $classic_toy_store_contact_id,
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',
                ) );
            }

            /* ---------------- Assign Menu Location ---------------- */

            if ( ! has_nav_menu( $classic_toy_store_bpmenulocation ) ) {

                $classic_toy_store_locations = get_theme_mod( 'nav_menu_locations', array() );
                $classic_toy_store_locations[ $classic_toy_store_bpmenulocation ] = $classic_toy_store_menu_id;
                set_theme_mod( 'nav_menu_locations', $classic_toy_store_locations );
            }
        }

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

        //Topbar Section
        set_theme_mod( 'classic_toy_store_topbar_text', 'Enjoy Free Shipping on Orders Over $50! Shop Now for the Latest Toys!');
        set_theme_mod( 'classic_toy_store_phone_icon', 'fas fa-phone');
        set_theme_mod( 'classic_toy_store_phone_title', '24/7 support');
        set_theme_mod( 'classic_toy_store_phone_number', '+123-4567-890');

        //Banner Section
        set_theme_mod( 'classic_toy_store_banner', true);
        set_theme_mod( 'classic_toy_store_banner_small_title', 'Free Delivery on Order above $50');
        set_theme_mod( 'classic_toy_store_button_text', 'Explore more');

        // Create a single page 
        $classic_toy_store_banner_title = 'Discover the Hottest Toys for Every Age and Adventure!';
        $classic_toy_store_banner_content = 'Explore our wide selection of toys, designed to inspire creativity, learning, and endless fun for kids of all ages!';

        $classic_toy_store_post = array(
            'post_title'    => wp_strip_all_tags($classic_toy_store_banner_title),
            'post_content'  => $classic_toy_store_banner_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
        );

        // Insert the post into the database
        $classic_toy_store_post_id = wp_insert_post($classic_toy_store_post);

        // If the post was successfully created
        if ($classic_toy_store_post_id && !is_wp_error($classic_toy_store_post_id)) {
            // Set the theme mod for the single banner page
            set_theme_mod('classic_toy_store_banner_pageboxes', $classic_toy_store_post_id);

            // Set the featured image for the post
            $classic_toy_store_image_url = get_template_directory_uri() . '/images/banner.png';
            
            // Use media_sideload_image to upload and set the image
            $classic_toy_store_image_id = media_sideload_image($classic_toy_store_image_url, $classic_toy_store_post_id, null, 'id');

            // Check if the image was successfully uploaded and set
            if (!is_wp_error($classic_toy_store_image_id)) {
                // Set the image as the post's featured image
                set_post_thumbnail($classic_toy_store_post_id, $classic_toy_store_image_id);
            } else {
                // Log or handle the error
                error_log("Error setting the featured image: " . $classic_toy_store_image_id->get_error_message());
            }

            // Set the single created page as the default page for the banner
            set_theme_mod('classic_toy_store_selected_banner_page', $classic_toy_store_post_id);

        } else {
            // Log or handle the error if the page creation failed
            error_log("Error creating the page: " . $classic_toy_store_post_id->get_error_message());
        }

        // Products Section
        set_theme_mod('classic_toy_store_disabled_featured_products_section', true);
        set_theme_mod( 'classic_toy_store_featured_products_text', 'Discover Our Latest Must-Have Toy Picks!');
        set_theme_mod( 'classic_toy_store_featured_products_title', 'New Featured Products');

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

        // Define the single product category name, product titles, and tags
        $classic_toy_store_category_name = 'productcategory1';
        $classic_toy_store_titles = array(
            "Strap Puzzle",
            "Slide",
            "Tortoise",
            "Plastic Toys"
        );

        // Create or retrieve the product category term ID
        $classic_toy_store_term = term_exists($classic_toy_store_category_name, 'product_cat');
        if (!$classic_toy_store_term) {
            $classic_toy_store_term = wp_insert_term($classic_toy_store_category_name, 'product_cat');
        }

        if (is_wp_error($classic_toy_store_term)) {
            error_log('Error creating category: ' . $classic_toy_store_term->get_error_message());
            return; // Exit if category creation fails
        }

        $classic_toy_store_term_id = is_array($classic_toy_store_term) ? $classic_toy_store_term['term_id'] : $classic_toy_store_term;

        // Loop to create 4 products for the category
        foreach ($classic_toy_store_titles as $index => $classic_toy_store_title) {
            // Create product content
            $classic_toy_store_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';

            // Create product post object
            $classic_toy_store_post_id = wp_insert_post(array(
                'post_title'    => wp_strip_all_tags($classic_toy_store_title),
                'post_content'  => $classic_toy_store_content,
                'post_status'   => 'publish',
                'post_type'     => 'product', // Post type set to 'product'
            ));

            if (is_wp_error($classic_toy_store_post_id)) {
                error_log('Error creating product: ' . $classic_toy_store_post_id->get_error_message());
                continue; // Skip to the next product if creation fails
            }

            // Assign the category to the product
            wp_set_object_terms($classic_toy_store_post_id, $classic_toy_store_term_id, 'product_cat');

            // Set product price and sale price
            update_post_meta($classic_toy_store_post_id, '_regular_price', '31.00'); // Regular price
            update_post_meta($classic_toy_store_post_id, '_sale_price', '24.80'); // Sale price with 20% discount
            update_post_meta($classic_toy_store_post_id, '_price', '24.80'); // Active price

            // Set 5-star rating
            update_post_meta($classic_toy_store_post_id, '_wc_average_rating', '5.0'); // Rating value
            update_post_meta($classic_toy_store_post_id, '_wc_review_count', '250'); // Number of reviews

            // Handle the featured image using media_sideload_image
            $classic_toy_store_image_url = get_template_directory_uri() . '/images/Product' . ($index + 1) . '.png';
            $classic_toy_store_image_id = media_sideload_image($classic_toy_store_image_url, $classic_toy_store_post_id, null, 'id');

            if (!is_wp_error($classic_toy_store_image_id)) {
                // Assign featured image to product
                set_post_thumbnail($classic_toy_store_post_id, $classic_toy_store_image_id);
            } else {
                error_log('Error downloading image for product: ' . $classic_toy_store_image_id->get_error_message());
            }
        }

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