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

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

    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
          $vw_clothing_store_plugin_slug = 'woocommerce';
          $vw_clothing_store_plugin_file = 'woocommerce/woocommerce.php';

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

        // Check if  GTranslate is installed and activated
        if (!is_plugin_active('gtranslate/gtranslate.php')) {
          // Install the plugin if it doesn't exist
          $vw_clothing_store_plugin_slug = 'gtranslate';
          $vw_clothing_store_plugin_file = 'gtranslate/gtranslate.php';

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

        // Check if ibtana visual editor is installed and activated
        if (!is_plugin_active('ibtana-visual-editor/plugin.php')) {
          // Install the plugin if it doesn't exist
          $vw_clothing_store_plugin_slug = 'ibtana-visual-editor';
          $vw_clothing_store_plugin_file = 'ibtana-visual-editor/plugin.php';

          // Check if plugin is installed
          $vw_clothing_store_installed_plugins = get_plugins();
          if (!isset($vw_clothing_store_installed_plugins[$vw_clothing_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
              $vw_clothing_store_upgrader = new Plugin_Upgrader();
              $vw_clothing_store_upgrader->install('https://downloads.wordpress.org/plugin/ibtana-visual-editor.latest-stable.zip');
          }
          // Activate the plugin
          activate_plugin($vw_clothing_store_plugin_file);
        }

        // Create a front page and assign the template
        $vw_clothing_store_home_page = null;
        // Using WP_Query instead of get_page_by_title()
        $vw_clothing_store_home_query = new WP_Query(array(
           'post_type' => 'page',
           'title' => 'Home',
           'post_status' => 'publish',
           'posts_per_page' => 1
        ));
        if (!$vw_clothing_store_home_query->have_posts()) {
           $vw_clothing_store_home_title = 'Home';           
           // Create the page
           $vw_clothing_store_home = array(
               'post_type' => 'page',
               'post_title' => $vw_clothing_store_home_title,
               'post_status' => 'publish',
               'post_author' => 1,
               'post_slug' => 'home'
           );
           $vw_clothing_store_home_id = wp_insert_post($vw_clothing_store_home);
        } else {
           $vw_clothing_store_home_page = $vw_clothing_store_home_query->posts[0];
           $vw_clothing_store_home_id = $vw_clothing_store_home_page->ID;
        }

        // Set the home page template
        add_post_meta($vw_clothing_store_home_id, '_wp_page_template', 'page-template/custom-home-page.php');

        // Set the static front page
        update_option('page_on_front', $vw_clothing_store_home_id);
        update_option('show_on_front', 'page');

        // Create another page if needed
        $vw_clothing_store_page_query = new WP_Query(array(
           'post_type' => 'page',
           'title' => 'Page',
           'post_status' => 'publish',
           'posts_per_page' => 1
        ));

        if (!$vw_clothing_store_page_query->have_posts()) {
           $vw_clothing_store_page_title = 'Page';
           $vw_clothing_store_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.';
           
            $vw_clothing_store_page = array(
               'post_type' => 'page',
               'post_title' => $vw_clothing_store_page_title,
               'post_content' => $vw_clothing_store_content,
               'post_status' => 'publish',
               'post_author' => 1,
               'post_slug' => 'page'
            );
            $vw_clothing_store_page_id = wp_insert_post($vw_clothing_store_page);
        }       

        // Set the demo import completion flag
    		update_option('vw_clothing_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.', 'vw-clothing-store') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'vw-clothing-store') . '</a></span>';

        //end 

        // topbar section start //         
        set_theme_mod( 'vw_clothing_store_offer_text', 'Populate this marketing banner to advertise a special promotion such as: Save 20% this weekend!' );
        set_theme_mod( 'vw_clothing_store_myaccount_icon', 'fas fa-user' );
        set_theme_mod( 'vw_clothing_store_heart_icon', 'fas fa-heart' );
        set_theme_mod( 'vw_clothing_store_cart_icon', 'fas fa-cart-plus' );

        // slider section start //  
        set_theme_mod( 'vw_clothing_store_banner_image', get_template_directory_uri().'/assets/images/banner.png' );   
        set_theme_mod( 'vw_clothing_store_extra_text', '25% Off' );
        set_theme_mod( 'vw_clothing_store_tagline_title', 'Men Fashion Collection' );
        set_theme_mod( 'vw_clothing_store_designation_text', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' );
        
        set_theme_mod('vw_clothing_store_product_category', 'productcategory1');
        // Define product category names and product titles
            $vw_clothing_store_category_names = array('productcategory1', 'productcategory2');
            $vw_clothing_store_title_array = array(
                array("Women Collection", "Men Collection", "Kids Collection", "Women Collection"),
                array("Women Collection", "Men Collection", "Kids Collection", "Women Collection")
            );

            foreach ($vw_clothing_store_category_names as $vw_clothing_store_index => $vw_clothing_store_category_name) {
                // Create or retrieve the product category term ID
                $vw_clothing_store_term = term_exists($vw_clothing_store_category_name, 'product_cat');
                if ($vw_clothing_store_term === 0 || $vw_clothing_store_term === null) {
                    // If the term does not exist, create it
                    $vw_clothing_store_term = wp_insert_term($vw_clothing_store_category_name, 'product_cat');
                }

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

                // Loop to create 4 products for each category
                for ($vw_clothing_store_i = 0; $vw_clothing_store_i < 4; $vw_clothing_store_i++) {
                    // Create product content
                    $vw_clothing_store_title = $vw_clothing_store_title_array[$vw_clothing_store_index][$vw_clothing_store_i];
                    $vw_clothing_store_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. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.';

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

                    // Insert the product into the database
                    $vw_clothing_store_post_id = wp_insert_post($vw_clothing_store_my_post);

                    if (is_wp_error($vw_clothing_store_post_id)) {
                        error_log('Error creating product: ' . $vw_clothing_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($vw_clothing_store_post_id, (int)$vw_clothing_store_term['term_id'], 'product_cat');

                    update_post_meta($vw_clothing_store_post_id, 'vw_clothing_store_rating_text', "25% Off");

                    // Handle the featured image using media_sideload_image
                    $vw_clothing_store_image_url = get_template_directory_uri() . '/assets/images/banner-product' . ($vw_clothing_store_i + 1) . '.png';
                    $vw_clothing_store_image_id = media_sideload_image($vw_clothing_store_image_url, $vw_clothing_store_post_id, null, 'id');

                    if (is_wp_error($vw_clothing_store_image_id)) {
                        error_log('Error downloading image: ' . $vw_clothing_store_image_id->get_error_message());
                        continue; // Skip to the next product if image download fails
                    }
                    // Assign featured image to product
                    set_post_thumbnail($vw_clothing_store_post_id, $vw_clothing_store_image_id);

                }
            }


            // product section start //  
            set_theme_mod( 'vw_clothing_store_product_clock_timer_end', 'June 3 2025 11:00:00' );   
            set_theme_mod( 'vw_clothing_store_new_collection_heading', 'New Collection' );
            set_theme_mod( 'vw_clothing_store_new_collection_small_title', 'Lorem Ipsum is simply dummy text of the printing.' );
            set_theme_mod( 'vw_clothing_store_trending_collection_heading', 'Trending Collection' );
            set_theme_mod( 'vw_clothing_store_trending_collection_small_title', 'Lorem Ipsum is simply dummy text of the printing.' );
            set_theme_mod( 'vw_clothing_store_featured_collection_heading', 'Featured Collection' );
            set_theme_mod( 'vw_clothing_store_featured_collection_small_title', 'Lorem Ipsum is simply dummy text of the printing.' );
            
            set_theme_mod('vw_clothing_store_best_product_category', 'productcat1');
            // Define product category names and product titles
                $vw_clothing_store_category_names = array('productcat1', 'productcat2');
                $vw_clothing_store_title_array = array(
                    array("Green Puffer Jacket", "Print Maxi Dress", "Checked Cotton Pullover","Gry Printed T-shirt"),
                    array("Gry Printed T-shirt", "Checked Cotton Pullover", "Print Maxi Dress","Green Puffer Jacket")
                );

            foreach ($vw_clothing_store_category_names as $vw_clothing_store_index => $vw_clothing_store_category_name) {
                // Create or retrieve the product category term ID
                $vw_clothing_store_term = term_exists($vw_clothing_store_category_name, 'product_cat');
                if ($vw_clothing_store_term === 0 || $vw_clothing_store_term === null) {
                    // If the term does not exist, create it
                    $vw_clothing_store_term = wp_insert_term($vw_clothing_store_category_name, 'product_cat');
                }

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

                // Loop to create 4 products for each category
                for ($vw_clothing_store_i = 0; $vw_clothing_store_i < 4; $vw_clothing_store_i++) {
                    // Create product content
                    $vw_clothing_store_title = $vw_clothing_store_title_array[$vw_clothing_store_index][$vw_clothing_store_i];
                    $vw_clothing_store_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. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.';

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

                    // Insert the product into the database
                    $vw_clothing_store_post_id = wp_insert_post($vw_clothing_store_my_post);

                    if (is_wp_error($vw_clothing_store_post_id)) {
                        error_log('Error creating product: ' . $vw_clothing_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($vw_clothing_store_post_id, (int)$vw_clothing_store_term['term_id'], 'product_cat');

                    update_post_meta($vw_clothing_store_post_id, 'vw_clothing_store_rating_text', "25% Off");
                    update_post_meta($vw_clothing_store_post_id, 'vw_clothing_store_total_sale', "555");
                    // // Add product meta (price, etc.)
                    update_post_meta($vw_clothing_store_post_id, '_regular_price', '69.00'); // Regular price
                    update_post_meta($vw_clothing_store_post_id, '_sale_price', '85.00'); // Sale price
                    update_post_meta($vw_clothing_store_post_id, '_price', '85.00'); // Active price

                    // Handle the featured image using media_sideload_image
                    $vw_clothing_store_image_url = get_template_directory_uri() . '/assets/images/product' . ($vw_clothing_store_i + 1) . '.png';
                    $vw_clothing_store_image_id = media_sideload_image($vw_clothing_store_image_url, $vw_clothing_store_post_id, null, 'id');

                    if (is_wp_error($vw_clothing_store_image_id)) {
                        error_log('Error downloading image: ' . $vw_clothing_store_image_id->get_error_message());
                        continue; // Skip to the next product if image download fails
                    }
                    // Assign featured image to product
                    set_post_thumbnail($vw_clothing_store_post_id, $vw_clothing_store_image_id);


                    // Now, we add gallery images for the product
                    $gallery_image_ids = array();

                    // Loop to add gallery images (for example, we are using 3 gallery images for each product)
                    for ($vw_clothing_store_gallery_i = 1; $vw_clothing_store_gallery_i <= 4; $vw_clothing_store_gallery_i++) {
                        // Example gallery images (change these URLs as needed)
                        $vw_clothing_store_gallery_image_url = get_template_directory_uri() . '/assets/images/product-gallery' . ($vw_clothing_store_i + $vw_clothing_store_gallery_i) . '.png';
                        $vw_clothing_store_gallery_image_id = media_sideload_image($vw_clothing_store_gallery_image_url, $vw_clothing_store_post_id, null, 'id');
                        
                        if (is_wp_error($vw_clothing_store_gallery_image_id)) {
                            error_log('Error downloading gallery image: ' . $vw_clothing_store_gallery_image_id->get_error_message());
                            continue; // Skip to the next gallery image if download fails
                        }

                        // Add the gallery image ID to the gallery images array
                        $gallery_image_ids[] = $vw_clothing_store_gallery_image_id;
                    }

                    // Assign the gallery images to the product
                    if (!empty($gallery_image_ids)) {
                        update_post_meta($vw_clothing_store_post_id, '_product_image_gallery', implode(',', $gallery_image_ids));
                    }

                }
            }
        
          //Copyright Text
          set_theme_mod( 'vw_clothing_store_footer_text', 'By VWThemes' );             
    }
  ?>
  
	<p><?php esc_html_e('Please back up your website if it’s already live with data. This importer will overwrite your existing settings with the new customizer values for VW Clothing Store', 'vw-clothing-store'); ?></p>
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=vw_clothing_store_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('vw_clothing_store_demo_import_completed')) : ?>
            <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'vw-clothing-store'); ?>" class="button button-primary button-large">
        <?php endif; ?>
        <div id="spinner" style="display:none;">         
            <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/images/spinner.png" alt="" />
        </div>
    </form>
    <script type="text/javascript">
        function validate(form) {
            if (confirm("Do you really want to import the theme demo content?")) {
                // Show the spinner
                document.getElementById('spinner').style.display = 'block';
                // Allow the form to be submitted
                return true;
            } 
            else {
                return false;
            }
        }
    </script>
</div>

