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

        // If the demo import is completed, display the "View Site" button
        if ($electronics_store_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'electronics-store') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '"  class= "run-import view-site" target="_blank">' . esc_html__('VIEW SITE', 'electronics-store') . '</a></span>';
        }

		// POST and update the customizer and other related data of THE COURIER SERVICESPRO
        if (isset($_POST['submit'])) {

        // Check if woocommerce is installed and activated
        if (!is_plugin_active('woocommerce/woocommerce.php')) {
            // Install the plugin if it doesn't exist
            $electronics_store_plugin_slug = 'woocommerce';
            $electronics_store_plugin_file = 'woocommerce/woocommerce.php';
    
            // Check if plugin is installed
            $electronics_store_installed_plugins = get_plugins();
            if (!isset($electronics_store_installed_plugins[$electronics_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
                $electronics_store_upgrader = new Plugin_Upgrader();
                $electronics_store_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
            }
            // Activate the plugin
            activate_plugin($electronics_store_plugin_file);
        }
    
            // ------- Create Nav Menu --------
            $electronics_store_menuname = 'Main Menus';
            $electronics_store_bpmenulocation = 'primary';
            $electronics_store_menu_exists = wp_get_nav_menu_object($electronics_store_menuname);

            if (!$electronics_store_menu_exists) {
                $electronics_store_menu_id = wp_create_nav_menu($electronics_store_menuname);

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

                // Create Shop Page with Dummy Content
                $electronics_store_pages_title = 'Shop';
                $electronics_store_pages_content = '
                <p>Explore all the pages we have on our website. Find information about our services, company, and more.</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.<br>

                    All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $electronics_store_pages = array(
                    'post_type' => 'page',
                    'post_title' => $electronics_store_pages_title,
                    'post_content' => $electronics_store_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $electronics_store_pages_id = wp_insert_post($electronics_store_pages);
                // Add Shop Page to Menu
                wp_update_nav_menu_item($electronics_store_menu_id, 0, array(
                    'menu-item-title' => __('Shop', 'electronics-store'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $electronics_store_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Products Page with Dummy Content
                $electronics_store_about_title = 'Products';
                $electronics_store_about_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...<br>

                            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.<br>

                            There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br>

                            All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $electronics_store_about = array(
                    'post_type' => 'page',
                    'post_title' => $electronics_store_about_title,
                    'post_content' => $electronics_store_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $electronics_store_about_id = wp_insert_post($electronics_store_about);
                // Add Products Page to Menu
                wp_update_nav_menu_item($electronics_store_menu_id, 0, array(
                    'menu-item-title' => __('Products', 'electronics-store'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $electronics_store_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Sales Page with Dummy Content
                $electronics_store_about_title = 'Sales';
                $electronics_store_about_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...<br>

                            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.<br>

                            There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br>

                            All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $electronics_store_about = array(
                    'post_type' => 'page',
                    'post_title' => $electronics_store_about_title,
                    'post_content' => $electronics_store_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $electronics_store_about_id = wp_insert_post($electronics_store_about);
                // Add Sales Page to Menu
                wp_update_nav_menu_item($electronics_store_menu_id, 0, array(
                    'menu-item-title' => __('Sales', 'electronics-store'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $electronics_store_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

               // ===== CREATE BLOG PAGE =====
                $electronics_store_blog_page = get_page_by_path('blog');
                if (!$electronics_store_blog_page) {
                    $electronics_store_blog_page_id = wp_insert_post(array(
                        'post_type'   => 'page',
                        'post_title'  => 'Blog',
                        'post_status' => 'publish',
                        'post_name'   => 'blog',
                    ));

                } else {
                    $electronics_store_blog_page_id = $electronics_store_blog_page->ID;
                }
                update_option('page_for_posts', $electronics_store_blog_page_id);

                wp_update_nav_menu_item($electronics_store_menu_id, 0, array(
                    'menu-item-title'     => __('Blog', 'electronics-store'),
                    'menu-item-object-id' => $electronics_store_blog_page_id,
                    'menu-item-object'    => 'page',
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',
                ));

                // Create Hot Deals Page with Dummy Content
                $electronics_store_about_title = 'Hot Deals';
                $electronics_store_about_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...<br>

                            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.<br>

                            There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br>

                            All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $electronics_store_about = array(
                    'post_type' => 'page',
                    'post_title' => $electronics_store_about_title,
                    'post_content' => $electronics_store_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $electronics_store_about_id = wp_insert_post($electronics_store_about);
                // Add Hot Deals Page to Menu
                wp_update_nav_menu_item($electronics_store_menu_id, 0, array(
                    'menu-item-title' => __('Hot Deals', 'electronics-store'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $electronics_store_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Set the menu location if it's not already set
                if (!has_nav_menu($electronics_store_bpmenulocation)) {
                    $electronics_store_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($electronics_store_locations)) {
                        $electronics_store_locations = array();
                    }
                    $electronics_store_locations[$electronics_store_bpmenulocation] = $electronics_store_menu_id;
                    set_theme_mod('nav_menu_locations', $electronics_store_locations);
                }

        }        
        
        // Top Bar Settings  
        set_theme_mod('electronics_store_show_topbar', true); 
        set_theme_mod('electronics_store_topbar_shipping_text', 'Shipping'); 
        set_theme_mod('electronics_store_topbar_shipping_url', '#'); 
        set_theme_mod('electronics_store_topbar_faqs_text', 'FAQs');
        set_theme_mod('electronics_store_topbar_faqs_url', '#');
        set_theme_mod('electronics_store_topbar_contact_text', 'Contact'); 
        set_theme_mod('electronics_store_topbar_contact_url', '#'); 
        set_theme_mod('electronics_store_topbar_sale_text', 'TODAY DEAL SALE OFF 70%. END IN 00 days 00:00:00 HURRY UP!!!');
        set_theme_mod('electronics_store_topbar_guarantee_text', 'Free 30 day money back guarantee');


        // Header Settings
        set_theme_mod('electronics_store_shipping_text', 'Free Shipping Above $100 Total Order');
        set_theme_mod('electronics_store_wishlist_url', '#');

        // Slider Settings
        set_theme_mod('electronics_store_slider_hide_show', true);
        set_theme_mod('electronics_store_slider_small_title', 'NEW COLLECTION');
        set_theme_mod('electronics_store_slider_button_link', '#');

        for ($electronics_store_i = 1; $electronics_store_i <= 4; $electronics_store_i++) {
            // Define different titles and contents for each page
            $electronics_store_slider_titles = array(
                'New Gaming Consoles',
                'Latest Tech Reviews',
                'Top Gadgets Of Year',
                'Future Innovations'
            );

            $electronics_store_slider_content = 'Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ratione quo odit saepe libero voluptatum mollitia!';

            // Create post object with dynamic title
            $electronics_store_my_post = array(
                'post_title'   => wp_strip_all_tags($electronics_store_slider_titles[$electronics_store_i - 1]), // Get unique title for each page
                'post_content' => $electronics_store_slider_content,
                'post_status'  => 'publish',
                'post_type'    => 'page',
            );

            // Insert the post into the database
            $electronics_store_post_id = wp_insert_post($electronics_store_my_post);

            if ($electronics_store_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('electronics_store_slider_page' . $electronics_store_i, $electronics_store_post_id);

                // Define the image URL for each page
                $electronics_store_image_url = get_template_directory_uri() . '/images/slider' . $electronics_store_i . '.png';

                // Use media_sideload_image() to fetch and attach the image to the page
                $electronics_store_image_id = media_sideload_image($electronics_store_image_url, $electronics_store_post_id, null, 'id');

                if (!is_wp_error($electronics_store_image_id)) {
                    // Set the downloaded image as the post's featured image
                    set_post_thumbnail($electronics_store_post_id, $electronics_store_image_id);
                } else {
                    error_log('Error downloading image for slider ' . $electronics_store_i . ': ' . $electronics_store_image_id->get_error_message());
                }
            }
        }

        // Product Section  
        set_theme_mod('electronics_store_product_section_hide_show', true);
        set_theme_mod('electronics_store_section_title', 'RECOMMENDED FOR YOU');
        set_theme_mod('electronics_store_product_deals_image', get_template_directory_uri() . '/images/sale-banner.png');
        set_theme_mod('electronics_store_product_deal_text', 'Hot Deals on Smartphones!');

        // Titles for the products
        $electronics_store_title_array = array(
            "Real Tab 4GB RAM, 10.4 inch Tablet",
            "Xbox Series One - 1TB",
            "3000D DSLR Camera, 18-55mm Lens",
        );

        for ($electronics_store_i = 0; $electronics_store_i < 3; $electronics_store_i++) {
            // Create product content
            $electronics_store_title = $electronics_store_title_array[$electronics_store_i];
            $electronics_store_content = 'Lorem Ipsum description for the product.';

            // Create product post object
            $electronics_store_my_post = array(
                'post_title'   => wp_strip_all_tags($electronics_store_title),
                'post_content' => $electronics_store_content,
                'post_status'  => 'publish',
                'post_type'    => 'product', // Post type set to 'product'
            );

            // Insert the product into the database
            $electronics_store_post_id = wp_insert_post($electronics_store_my_post);
            if (is_wp_error($electronics_store_post_id)) {
                error_log('Error creating product: ' . $electronics_store_post_id->get_error_message());
                continue; // Skip to the next product if creation fails
            }

            // Store the product ID in the theme mod settings
            set_theme_mod('electronics_store_product_setting_' . $electronics_store_i, $electronics_store_post_id);

            // Add product meta data
            update_post_meta($electronics_store_post_id, '_regular_price', '270'); // Regular price
            update_post_meta($electronics_store_post_id, '_sale_price', '216.23'); // Sale price
            update_post_meta($electronics_store_post_id, '_price', '216.23'); // Active price

            // Handle the featured image using media_sideload_image
            $electronics_store_image_url = get_template_directory_uri() . '/images/electronics-product' . ($electronics_store_i + 1) . '.png';
            $electronics_store_image_id = media_sideload_image($electronics_store_image_url, $electronics_store_post_id, null, 'id');

            if (is_wp_error($electronics_store_image_id)) {
                error_log('Error downloading image: ' . $electronics_store_image_id->get_error_message());
                continue; // Skip to the next product if image download fails
            }

            // Assign featured image to product
            set_post_thumbnail($electronics_store_post_id, $electronics_store_image_id);

            // Assign the product to the "Uncategorized" category
            wp_set_object_terms($electronics_store_post_id, 'uncategorized', 'product_cat');
        }

        // Check if the Products page already exists
        $electronics_store_page_query = new WP_Query(array(
            'post_type'      => 'page',
            'name'           => 'products',
            'post_status'    => 'publish',
            'posts_per_page' => 1,
        ));

        if (!$electronics_store_page_query->have_posts()) {
            // Create a new Products page
            $electronics_store_page_title = 'NEW COLLECTION';
            $electronics_store_productpage = '[products limit="3" columns="3"]';

            // Prepare the page content
            $electronics_store_content = do_shortcode($electronics_store_productpage);

            // Create the new page
            $electronics_store_page = array(
                'post_type'    => 'page',
                'post_title'   => $electronics_store_page_title,
                'post_content' => $electronics_store_content,
                'post_status'  => 'publish',
                'post_author'  => get_current_user_id(), // Ensure proper author ID
                'post_name'    => 'products',
            );

            // Insert the page and get its ID
            $electronics_store_page_id = wp_insert_post($electronics_store_page);

            if (!is_wp_error($electronics_store_page_id)) {
                set_theme_mod('electronics_store_product_page', $electronics_store_page_id); // Store the page ID
            } else {
                error_log('Error creating Products page: ' . $electronics_store_page_id->get_error_message());
            }
        }

        // Set the demo import completion flag
        update_option('electronics_store_demo_import_completed', true);
        // Display success message and "View Site" button
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'electronics-store') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="run-import site-btn" target="_blank">' . esc_html__('VIEW SITE', 'electronics-store') . '</a></span>';

        }
    ?>
  
  <P class="note"><?php esc_html_e( 'Please Note: If your website is live and already contains data, we recommend creating a backup first. Running this importer will replace your current settings with the custom values from the demo.', 'electronics-store' ); ?></P>
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=electronics_store_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('electronics_store_demo_import_completed')) : ?>
        <button type="submit" name="submit" class="run-import">
                    <?php esc_html_e('Run Importer','electronics-store'); ?>
                    <span id="spinner" style="display: none;">
                        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/spinner.gif" alt="Loading..." style="width:34px; height:34px; vertical-align: middle;" />
                    </span>
        </button>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
			 if(confirm("Do you really want to import the theme demo content?")){
			    document.getElementById('spinner').style.display = 'inline-block';
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>