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

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

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

            // Check if Contact Form 7 is installed and activated
            if (!is_plugin_active('woocommerce/woocommerce.php')) {
              // Install the plugin if it doesn't exist
              $shopping_solution_plugin_slug = 'woocommerce';
              $shopping_solution_plugin_file = 'woocommerce/woocommerce.php';

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

            // ------- Create Nav Menu --------
            $shopping_solution_menuname = 'Primary Menu';
            $shopping_solution_bpmenulocation = 'primary';
            $shopping_solution_menu_exists = wp_get_nav_menu_object($shopping_solution_menuname);

            if (!$shopping_solution_menu_exists) {
                $shopping_solution_menu_id = wp_create_nav_menu($shopping_solution_menuname);

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

                // Create About Us Page with Dummy Content
                $shopping_solution_about_title = 'About Us';
                $shopping_solution_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.';
                $shopping_solution_about = array(
                    'post_type' => 'page',
                    'post_title' => $shopping_solution_about_title,
                    'post_content' => $shopping_solution_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $shopping_solution_about_id = wp_insert_post($shopping_solution_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'shopping-solution'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $shopping_solution_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                // Create Mobile Page with Dummy Content
                $shopping_solution_mobile_title = 'Mobile';
                $shopping_solution_mobile_content = '
                Explore our latest mobile collection.
                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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
                $shopping_solution_mobile = array(
                    'post_type' => 'page',
                    'post_title' => $shopping_solution_mobile_title,
                    'post_content' => $shopping_solution_mobile_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'mobile'
                );
                $shopping_solution_mobile_id = wp_insert_post($shopping_solution_mobile);
                // Add Mobile Page to Menu
                wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                    'menu-item-title' => __('Mobile', 'shopping-solution'),
                    'menu-item-classes' => 'mobile',
                    'menu-item-url' => home_url('/mobile/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $shopping_solution_mobile_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Headphone Page with Dummy Content
                $shopping_solution_headphone_title = 'Headphone';
                $shopping_solution_headphone_content = '
                Find the best headphones here.
                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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
                $shopping_solution_headphone = array(
                    'post_type' => 'page',
                    'post_title' => $shopping_solution_headphone_title,
                    'post_content' => $shopping_solution_headphone_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'headphone'
                );
                $shopping_solution_headphone_id = wp_insert_post($shopping_solution_headphone);
                // Add Headphone Page to Menu
                wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                    'menu-item-title' => __('Headphone', 'shopping-solution'),
                    'menu-item-classes' => 'headphone',
                    'menu-item-url' => home_url('/headphone/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $shopping_solution_headphone_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Clothes Page with Dummy Content
                $shopping_solution_clothes_title = 'Clothes';
                $shopping_solution_clothes_content = '
                Browse our clothing collection.
                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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
                $shopping_solution_clothes = array(
                    'post_type' => 'page',
                    'post_title' => $shopping_solution_clothes_title,
                    'post_content' => $shopping_solution_clothes_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'clothes'
                );
                $shopping_solution_clothes_id = wp_insert_post($shopping_solution_clothes);
                // Add Clothes Page to Menu
                wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                    'menu-item-title' => __('Clothes', 'shopping-solution'),
                    'menu-item-classes' => 'clothes',
                    'menu-item-url' => home_url('/clothes/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $shopping_solution_clothes_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
            // Create Shoes Page with Dummy Content
                $shopping_solution_shoes_title = 'Shoes';
                $shopping_solution_shoes_content = 'Discover top-quality shoes.
                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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
                $shopping_solution_shoes = array(
                    'post_type' => 'page',
                    'post_title' => $shopping_solution_shoes_title,
                    'post_content' => $shopping_solution_shoes_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'shoes'
                );
                $shopping_solution_shoes_id = wp_insert_post($shopping_solution_shoes);
                // Add Shoes Page to Menu
                wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                    'menu-item-title' => __('Shoes', 'shopping-solution'),
                    'menu-item-classes' => 'shoes',
                    'menu-item-url' => home_url('/shoes/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $shopping_solution_shoes_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                // Create Watches Page with Dummy Content
                $shopping_solution_watches_title = 'Watches';
                $shopping_solution_watches_content = 'Explore our watch collection.
                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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
                $shopping_solution_watches = array(
                    'post_type' => 'page',
                    'post_title' => $shopping_solution_watches_title,
                    'post_content' => $shopping_solution_watches_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'watches'
                );
                $shopping_solution_watches_id = wp_insert_post($shopping_solution_watches);
                // Add Watches Page to Menu
                wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                    'menu-item-title' => __('Watches', 'shopping-solution'),
                    'menu-item-classes' => 'watches',
                    'menu-item-url' => home_url('/watches/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $shopping_solution_watches_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $shopping_solution_blog_page_title = 'Blog';

                $shopping_solution_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($shopping_solution_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$shopping_solution_blog_page_query->have_posts()) {
                    $shopping_solution_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $shopping_solution_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $shopping_solution_blog_page_id = wp_insert_post($shopping_solution_blog_page);
                    update_option('page_for_posts', $shopping_solution_blog_page_id);

                    wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'shopping-solution'),
                        'menu-item-url'        => get_permalink($shopping_solution_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $shopping_solution_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Contact Us Page with Dummy Content
                $shopping_solution_contact_title = 'Contact Us';
                $shopping_solution_contact_content = 'Get in touch with us.
                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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
                $shopping_solution_contact = array(
                    'post_type' => 'page',
                    'post_title' => $shopping_solution_contact_title,
                    'post_content' => $shopping_solution_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact-us'
                );
                $shopping_solution_contact_id = wp_insert_post($shopping_solution_contact);
                // Add Contact Us Page to Menu
                wp_update_nav_menu_item($shopping_solution_menu_id, 0, array(
                    'menu-item-title' => __('Contact Us', 'shopping-solution'),
                    'menu-item-classes' => 'contact-us',
                    'menu-item-url' => home_url('/contact-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $shopping_solution_contact_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                // Set the menu location if it's not already set
                if (!has_nav_menu($shopping_solution_bpmenulocation)) {
                    $shopping_solution_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($shopping_solution_locations)) {
                        $shopping_solution_locations = array();
                    }
                    $shopping_solution_locations[$shopping_solution_bpmenulocation] = $shopping_solution_menu_id;
                    set_theme_mod('nav_menu_locations', $shopping_solution_locations);
                }
            }

            // Set the demo import completion flag
    		update_option('shopping_solution_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.', 'shopping-solution') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'shopping-solution') . '</a></span>';
            //end

            // Top Bar //
            set_theme_mod( 'ecommerce_solution_phone_number', '+123-456-7890' );
            set_theme_mod( 'shopping_solution_dicount_text', 'Due to the LOREM IPSUM, orders may be processed with a slight delay' );
            set_theme_mod( 'shopping_solution_sale_text', '100% Secure delivery without contacting the courier' );

            //Social Links
            set_theme_mod( 'shopping_solution_facebook_link', 'https://www.facebook.com' );
            set_theme_mod( 'shopping_solution_twitter_link', 'https://www.twitter.com' );
            set_theme_mod( 'shopping_solution_linkedin_link', 'https://www.linkedin.com' );
            set_theme_mod( 'shopping_solution_instagram_link', 'https://instagram.com' );

            // slider section start //
            set_theme_mod( 'ecommerce_solution_slider_hide', true );
            set_theme_mod( 'ecommerce_solution_slider_button_text', 'SHOP NOW' );
            set_theme_mod( 'ecommerce_solution_topbar_btn_link', '#' );

            $ecommerce_solution_slider_titles = array(
                'Get your family the best',
                'Build Your Online Store with Ease', 
                'Boost Sales with a Powerful E-Commerce Platform', 
                'Sell Anything, Anywhere with Flexible Solutions'
            );                    
            for ($ecommerce_solution_i = 0; $ecommerce_solution_i < 4; $ecommerce_solution_i++) {
                // Get the slider title and content
                $ecommerce_solution_slider_title = $ecommerce_solution_slider_titles[$ecommerce_solution_i];
                $ecommerce_solution_slider_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.';
            
                // Create post object
                $ecommerce_solution_my_post = array(
                    'post_title'    => wp_strip_all_tags($ecommerce_solution_slider_title),
                    'post_content'  => $ecommerce_solution_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );
            
                // Insert the post into the database
                $ecommerce_solution_post_id = wp_insert_post($ecommerce_solution_my_post);
            
                if ($ecommerce_solution_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('ecommerce_solution_slider_setting' . ($ecommerce_solution_i + 1), $ecommerce_solution_post_id);
            
                    // Set the slider image URL for each index
                    $ecommerce_solution_image_url = get_theme_file_uri() . '/images/slider' . ($ecommerce_solution_i + 1) . '.png';
            
                    // Download the image and set it as the featured image
                    $ecommerce_solution_image_id = media_sideload_image($ecommerce_solution_image_url, $ecommerce_solution_post_id, null, 'id');
            
                    if (!is_wp_error($ecommerce_solution_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($ecommerce_solution_post_id, $ecommerce_solution_image_id);
                    }
                }
            }

            // new collection
            set_theme_mod('ecommerce_solution_show_new_collection_section', true );
            set_theme_mod('ecommerce_solution_bestseller_section_title', 'Shop New Collection' );
            $ecommerce_solution_title_array = array(
                "Product Title 1",
                "Product Title 2",
                "Product Title 3",
                "Product Title 4",
            );
            
            for ($ecommerce_solution_i = 0; $ecommerce_solution_i < 4; $ecommerce_solution_i++) {
                // Create product content
                $ecommerce_solution_title = $ecommerce_solution_title_array[$ecommerce_solution_i];
                $ecommerce_solution_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...';
            
                // Create product post object
                $ecommerce_solution_my_post = array(
                    'post_title'    => wp_strip_all_tags($ecommerce_solution_title),
                    'post_content'  => $ecommerce_solution_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'product', // Post type set to 'product'
                );
                // Insert the product into the database
                $ecommerce_solution_post_id = wp_insert_post($ecommerce_solution_my_post);
                if (is_wp_error($ecommerce_solution_post_id)) {
                    error_log('Error creating product: ' . $ecommerce_solution_post_id->get_error_message());
                    continue; // Skip to the next product if creation fails
                }
            
                // Assign product to the 'Uncategorized' category
                wp_set_object_terms($ecommerce_solution_post_id, 'uncategorized', 'product_cat');
            
                // Store the product ID in the theme mod settings
                set_theme_mod('ecommerce_solution_product_setting_' . $ecommerce_solution_i, $ecommerce_solution_post_id);
                update_post_meta($ecommerce_solution_post_id, '_regular_price', '50'); // Regular price
                update_post_meta($ecommerce_solution_post_id, '_sale_price', '49.99'); // Sale price
                update_post_meta($ecommerce_solution_post_id, '_price', '49.99'); // Active price
            
                // Handle the featured image using media_sideload_image
                $ecommerce_solution_image_url = get_template_directory_uri() . '/images/product' . ($ecommerce_solution_i + 1) . '.png';
                $ecommerce_solution_image_id = media_sideload_image($ecommerce_solution_image_url, $ecommerce_solution_post_id, null, 'id');
            
                if (is_wp_error($ecommerce_solution_image_id)) {
                    error_log('Error downloading image: ' . $ecommerce_solution_image_id->get_error_message());
                    continue; // Skip to the next product if image download fails
                }
                // Assign featured image to product
                set_post_thumbnail($ecommerce_solution_post_id, $ecommerce_solution_image_id);
            }
            
            $ecommerce_solution_page_query = new WP_Query(array(
                'post_type' => 'page',
                'title' => 'Products',
                'post_status' => 'publish',
                'posts_per_page' => 1
            ));
            
            if (!$ecommerce_solution_page_query->have_posts()) {
                $ecommerce_solution_page_title = 'SHOP NEW COLLECTION';
                $ecommerce_solution_productpage = '[products limit="4" columns="4"]';
            
                // Append the WooCommerce products shortcode to the content
                $ecommerce_solution_content = '';
                $ecommerce_solution_content .= do_shortcode($ecommerce_solution_productpage);
            
                // Create the new page
                $ecommerce_solution_page = array(
                    'post_type'    => 'page',
                    'post_title'   => $ecommerce_solution_page_title,
                    'post_content' => $ecommerce_solution_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'products'
                );
                // Insert the page and get its ID
                $ecommerce_solution_page_id = wp_insert_post($ecommerce_solution_page);
            
                // Store the page URL and ID in theme mod
                if (!is_wp_error($ecommerce_solution_page_id)) {
                    $ecommerce_solution_page_url = get_permalink($ecommerce_solution_page_id);
                    set_theme_mod('ecommerce_solution_product_settings', $ecommerce_solution_page_id); // Store ID instead of URL
                }
            }        

            //Copyright Text
            set_theme_mod( 'ecommerce_solution_footer_text', 'By Buywptemplate' );
        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=shopping-solution-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('shopping_solution_demo_import_completed')) : ?>
        <div class="demo-btn">
        <h3><?php esc_html_e( 'Click the below run importer button to import demo content', 'shopping-solution' ); ?></h3>
        <form method="post">
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','shopping-solution'); ?>" class="button button-primary button-large">
        </form>
        </div>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
            if ( confirm("Do you really want to import the theme demo content?")) {
                // Show loader
                document.getElementById('demo-import-loader').style.display = 'block';
                
                // Submit form
                document.forms[0].submit();
            } else {
                return false;
            }
		}
	</script>
</div>
