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

        // If the demo import is completed, display the "View Site" button
        if ($ecommerce_hub_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'ecommerce-hub') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'ecommerce-hub') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'ecommerce-hub') . '</a></span>';
        }

		//POST and update the customizer and other related data of POLITICAL CAMPAIGN
        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
                $ecommerce_hub_plugin_slug = 'woocommerce';
                $ecommerce_hub_plugin_file = 'woocommerce/woocommerce.php';

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

            // ------- Create Nav Menu --------
            $ecommerce_hub_menuname = 'Main Menus';
            $ecommerce_hub_bpmenulocation = 'primary';
            $ecommerce_hub_menu_exists = wp_get_nav_menu_object($ecommerce_hub_menuname);

            if (!$ecommerce_hub_menu_exists) {
                $ecommerce_hub_menu_id = wp_create_nav_menu($ecommerce_hub_menuname);

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

                // Create Pages Page with Dummy Content
                $ecommerce_hub_pages_title = 'Pages';
                $ecommerce_hub_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.';
                $ecommerce_hub_pages = array(
                    'post_type' => 'page',
                    'post_title' => $ecommerce_hub_pages_title,
                    'post_content' => $ecommerce_hub_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $ecommerce_hub_pages_id = wp_insert_post($ecommerce_hub_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($ecommerce_hub_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'ecommerce-hub'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $ecommerce_hub_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $ecommerce_hub_about_title = 'About Us';
                $ecommerce_hub_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.';
                $ecommerce_hub_about = array(
                    'post_type' => 'page',
                    'post_title' => $ecommerce_hub_about_title,
                    'post_content' => $ecommerce_hub_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $ecommerce_hub_about_id = wp_insert_post($ecommerce_hub_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($ecommerce_hub_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'ecommerce-hub'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $ecommerce_hub_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                // Create Blog Page only if it doesn't exist already
                $ecommerce_hub_existing_blog_page = get_page_by_path('blog');
                if (!$ecommerce_hub_existing_blog_page) {
                    $ecommerce_hub_blog_id = wp_insert_post(array(
                    'post_type'    => 'page',
                    'post_title'   => __('Blog', 'ecommerce-hub'),
                    'post_content' => '',
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'blog'
                ));
                } else {
                    $ecommerce_hub_blog_id = $ecommerce_hub_existing_blog_page->ID;
                }
                // Assign as Posts Page
                update_option('page_for_posts', $ecommerce_hub_blog_id);
                update_option('show_on_front', 'page');
                //Add to Menu (only if not already added)
                $ecommerce_hub_menu_items = wp_get_nav_menu_items($ecommerce_hub_menu_id);
                $ecommerce_hub_blog_in_menu = false;
                if ($ecommerce_hub_menu_items) {
                    foreach ($ecommerce_hub_menu_items as $ecommerce_hub_item) {
                        if ((int) $ecommerce_hub_item->object_id === (int) $ecommerce_hub_blog_id) {
                            $ecommerce_hub_blog_in_menu = true;
                            break;
                        }
                    }
                }
                if (!$ecommerce_hub_blog_in_menu) {
                wp_update_nav_menu_item($ecommerce_hub_menu_id, 0, array(
                    'menu-item-title'       => __('Blog', 'ecommerce-hub'),
                    'menu-item-url'         => home_url('/blog/'),
                    'menu-item-status'      => 'publish',
                    'menu-item-object-id'   => $ecommerce_hub_blog_id,
                    'menu-item-object'      => 'page',
                    'menu-item-type'        => 'post_type',
                    'menu-item-classes'     => 'ecommerce-hub-blog-link'
                ));
                }
                // Create Shop Page only if it doesn't exist already
                $ecommerce_hub_existing_shop_page = get_page_by_path('shop');
                if (!$ecommerce_hub_existing_shop_page) {
                    $ecommerce_hub_shop_id = wp_insert_post(array(
                        'post_type'    => 'page',
                        'post_title'   => __('Shop', 'ecommerce-hub'),
                        'post_content' => '',
                        'post_status'  => 'publish',
                        'post_author'  => 1,
                        'post_name'    => 'shop'
                    ));
                } else {
                    $ecommerce_hub_shop_id = $ecommerce_hub_existing_shop_page->ID;
                }

                // Assign as WooCommerce Shop Page
                update_option('woocommerce_shop_page_id', $ecommerce_hub_shop_id);

                // Add to Menu (only if not already added)
                $ecommerce_hub_menu_items = wp_get_nav_menu_items($ecommerce_hub_menu_id);
                $ecommerce_hub_shop_in_menu = false;

                if ($ecommerce_hub_menu_items) {
                    foreach ($ecommerce_hub_menu_items as $ecommerce_hub_item) {
                        if ((int) $ecommerce_hub_item->object_id === (int) $ecommerce_hub_shop_id) {
                            $ecommerce_hub_shop_in_menu = true;
                            break;
                        }
                    }
                }
                if (!$ecommerce_hub_shop_in_menu) {
                    wp_update_nav_menu_item($ecommerce_hub_menu_id, 0, array(
                        'menu-item-title'       => __('Shop', 'ecommerce-hub'),
                        'menu-item-url'         => home_url('/shop/'),
                        'menu-item-status'      => 'publish',
                        'menu-item-object-id'   => $ecommerce_hub_shop_id,
                        'menu-item-object'      => 'page',
                        'menu-item-type'        => 'post_type',
                        'menu-item-classes'     => 'ecommerce-hub-shop-link'
                    ));
                }

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

        // Set the demo import completion flag
        update_option('ecommerce_hub_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.', 'ecommerce-hub') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'ecommerce-hub') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'ecommerce-hub') . '</a></span>';
        //end 

        // Topbar settings   
        set_theme_mod('ecommerce_hub_top_header', true);
        set_theme_mod('ecommerce_hub_welcome', 'Welcome To The Ecommerce Hub Store!');
        set_theme_mod('ecommerce_hub_email_text', 'Drop To Us');
        set_theme_mod('ecommerce_hub_call_text', 'xyz123@example.com');
        set_theme_mod('ecommerce_hub_email', 'Support On 24/7');
        set_theme_mod('ecommerce_hub_call_number', '(123) 456 789');

        //Social Icon
        set_theme_mod('ecommerce_hub_facebook', '#');
        set_theme_mod('ecommerce_hub_pintrest', '#');
        set_theme_mod('ecommerce_hub_rss', '#');
        set_theme_mod('ecommerce_hub_twitter', '#');
        set_theme_mod('ecommerce_hub_youtube', '#');

        //Slider
        set_theme_mod('ecommerce_hub_slider_hide', true);
        set_theme_mod( 'ecommerce_hub_slider_small_title', '2025' );
        set_theme_mod( 'ecommerce_hub_slider_button_label', 'SHOP NOW' );
        set_theme_mod( 'ecommerce_hub_slider_button_link', '#' );

        $ecommerce_hub_slider_titles = [
            'LOREM IPSUM',
            'Your Style, Our Passion for Quality',
            'Crafting Convenience',
            'Empowering Your Shopping Experience',
        ];     
        
        $ecommerce_hub_slider_contents = [
            'LOREM IPSUM DOLOR SIT AMET',
            'LOREM IPSUM DOLOR SIT AMET',
            'LOREM IPSUM DOLOR SIT AMET',
            'LOREM IPSUM DOLOR SIT AMET',
        ];  
        
        for($ecommerce_hub_i=1;$ecommerce_hub_i<=4;$ecommerce_hub_i++){
            $ecommerce_hub_slider_title = $ecommerce_hub_slider_titles[$ecommerce_hub_i - 1];
            $ecommerce_hub_slider_content = $ecommerce_hub_slider_contents[$ecommerce_hub_i - 1];
            // Create post object
            $ecommerce_hub_my_post = array(
            'post_title'    => wp_strip_all_tags( $ecommerce_hub_slider_title ),
            'post_content'  => $ecommerce_hub_slider_content,
            'post_excerpt'  => $ecommerce_hub_slider_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
            );

            // Insert the post into the database
            $ecommerce_hub_post_id = wp_insert_post( $ecommerce_hub_my_post );

            if ($ecommerce_hub_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('ecommerce_hub_slider_page' . $ecommerce_hub_i, $ecommerce_hub_post_id);

                $ecommerce_hub_image_url = get_template_directory_uri().'/images/slider'.$ecommerce_hub_i.'.png';

                $ecommerce_hub_image_id = media_sideload_image($ecommerce_hub_image_url, $ecommerce_hub_post_id, null, 'id');

                    if (!is_wp_error($ecommerce_hub_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($ecommerce_hub_post_id, $ecommerce_hub_image_id);
                    }
            }
        } 

        //Product Section
        set_theme_mod( 'ecommerce_hub_featured_products_hide', true );
        set_theme_mod( 'ecommerce_hub_page_title', 'FEATURED PRODUCTS' );

        $ecommerce_hub_title_array = array(
            array("Product Title 1", "Product Title 2", "Product Title 3", "Product Title 4")
            );

        foreach ($ecommerce_hub_title_array as $ecommerce_hub_titles) {
            // Loop to create only 3 products
            for ($ecommerce_hub_i = 0; $ecommerce_hub_i < 4; $ecommerce_hub_i++) {
                // Create product content
                $ecommerce_hub_title = $ecommerce_hub_titles[$ecommerce_hub_i];
                $ecommerce_hub_content = 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.';

                // Create product post object
                $ecommerce_hub_my_post = array(
                    'post_title'    => wp_strip_all_tags($ecommerce_hub_title),
                    'post_content'  => $ecommerce_hub_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'product',
                );
                set_theme_mod('ecommerce_hub_feature_page', esc_url($ecommerce_hub_post_id));
                // Insert the product into the database
                $ecommerce_hub_post_id = wp_insert_post($ecommerce_hub_my_post);

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

                // Add product meta (price, etc.)
                update_post_meta($ecommerce_hub_post_id, '_regular_price', '25.00'); // Regular price
                update_post_meta($ecommerce_hub_post_id, '_sale_price', '20.00'); // Sale price
                update_post_meta($ecommerce_hub_post_id, '_price', '20.00'); // Active price

                // Handle the featured image using media_sideload_image
                $ecommerce_hub_image_url = get_template_directory_uri() . '/images/product' . ($ecommerce_hub_i + 1) . '.png';
                $ecommerce_hub_image_id = media_sideload_image($ecommerce_hub_image_url, $ecommerce_hub_post_id, null, 'id');

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

                // Assign featured image to product
                set_post_thumbnail($ecommerce_hub_post_id, $ecommerce_hub_image_id);
            }
        }

        // Create the 'Products' page if it doesn't exist
        $ecommerce_hub_page_query = new WP_Query(array(
            'post_type'      => 'page',
            'title'          => 'Products',
            'post_status'    => 'publish',
            'posts_per_page' => 1
        ));

        if (!$ecommerce_hub_page_query->have_posts()) {
            $ecommerce_hub_page_title = 'Products';
            $ecommerce_hub_productpage = '[products limit="4" columns="4"]';

            // Append the WooCommerce products shortcode to the content
            $ecommerce_hub_content = '';
            $ecommerce_hub_content .= do_shortcode($ecommerce_hub_productpage);

            // Create the new page
            $ecommerce_hub_page = array(
                'post_type'    => 'page',
                'post_title'   => $ecommerce_hub_page_title,
                'post_content' => $ecommerce_hub_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'products'
            );

            // Insert the page and get its ID
            $ecommerce_hub_page_id = wp_insert_post($ecommerce_hub_page);

            // Store the page ID in theme mod
            if (!is_wp_error($ecommerce_hub_page_id)) {
                set_theme_mod('ecommerce_hub_feature_page', $ecommerce_hub_page_id);
            }
        }

        //Copyright Text
        set_theme_mod( 'ecommerce_hub_footer_text', 'By Themesglance' );

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=ecommerce_hub_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('ecommerce_hub_demo_import_completed')) : ?>
        <div class="demo-btn">
        <p><?php echo  esc_html_e( 'You are about to use the most easy to use and flexible WordPress theme.', 'ecommerce-hub' ); ?></p>
        <button id="import-button" type="submit" name="submit" class="import-btn run-import button-large">
            <?php esc_html_e('Run Importer', 'ecommerce-hub'); ?>
                <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; margin-left:10px;vertical-align: middle;" />
                </span>
        </button>
        </div>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
			 if(confirm("Do you really want to import the Demo Theme Content?")){
			    document.getElementById('spinner').style.display = 'inline-block';
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>

