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

        // If the demo import is completed, display the "View Site" button
        if ($vw_gift_shop_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'vw-gift-shop') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'vw-gift-shop') . '</a></span>';
        echo '<span><a href="'. esc_url(admin_url('customize.php') ) .'" class="button button-primary demo-btn" target=_blank>'. esc_html__( 'Customize Your Site', 'vw-gift-shop' ) .'</a></span>';
        echo '<span><a href="'. esc_url( 'https://preview.vwthemesdemo.com/docs/free-vw-gift-shop/' ) .'" class="button button-primary doc-btn" target=_blank>'. esc_html__( 'Free Theme Documentation', 'vw-gift-shop' ) .'</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
          $vw_gift_shop_plugin_slug = 'woocommerce';
          $vw_gift_shop_plugin_file = 'woocommerce/woocommerce.php';

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

        // Check if wishlist is installed and activated
        if (!is_plugin_active('yith-woocommerce-wishlist/init.php')) {
          // Install the plugin if it doesn't exist
          $vw_gift_shop_plugin_slug = 'yith-woocommerce-wishlist';
          $vw_gift_shop_plugin_file = 'yith-woocommerce-wishlist/init.php';

          // Check if plugin is installed
          $vw_gift_shop_installed_plugins = get_plugins();
          if (!isset($vw_gift_shop_installed_plugins[$vw_gift_shop_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_gift_shop_upgrader = new Plugin_Upgrader();
              $vw_gift_shop_upgrader->install('https://downloads.wordpress.org/plugin/yith-woocommerce-wishlist.latest-stable.zip');
          }
          // Activate the plugin
          activate_plugin($vw_gift_shop_plugin_file);
        }

        // Check if Quick View is installed and activated
        if (!is_plugin_active('yith-woocommerce-quick-view/init.php')) {
          // Install the plugin if it doesn't exist
          $vw_gift_shop_plugin_slug = 'yith-woocommerce-quick-view';
          $vw_gift_shop_plugin_file = 'yith-woocommerce-quick-view/init.php';

          // Check if plugin is installed
          $vw_gift_shop_installed_plugins = get_plugins();
          if (!isset($vw_gift_shop_installed_plugins[$vw_gift_shop_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_gift_shop_upgrader = new Plugin_Upgrader();
              $vw_gift_shop_upgrader->install('https://downloads.wordpress.org/plugin/yith-woocommerce-quick-view.latest-stable.zip');
          }
          // Activate the plugin
          activate_plugin($vw_gift_shop_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_gift_shop_plugin_slug = 'ibtana-visual-editor';
          $vw_gift_shop_plugin_file = 'ibtana-visual-editor/plugin.php';

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

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

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

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

            // Set the demo import completion flag
    		update_option('vw_gift_shop_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-gift-shop') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'vw-gift-shop') . '</a></span>';
            echo '<span><a href="'. esc_url(admin_url('customize.php') ) .'" class="button button-primary demo-btn" target=_blank>'. esc_html__( 'Customize Your Site', 'vw-gift-shop' ) .'</a></span>';
            echo '<span><a href="'. esc_url( 'https://preview.vwthemesdemo.com/docs/free-vw-gift-shop/' ) .'" class="button button-primary doc-btn" target=_blank>'. esc_html__( 'Free Theme Documentation', 'vw-gift-shop' ) .'</a></span>';
            //end 

            // slider section start //        
            set_theme_mod( 'vw_gift_shop_slide_number', '3' );
            for($vw_gift_shop_i=1; $vw_gift_shop_i<=3; $vw_gift_shop_i++) {
                set_theme_mod( 'vw_gift_shop_slider_small_text'.$vw_gift_shop_i, 'Gift Shop' );
                set_theme_mod( 'vw_gift_shop_slider_title'.$vw_gift_shop_i, 'Immerse Your Self In The Gift Flavor' );
                set_theme_mod( 'vw_gift_shop_slider_text'.$vw_gift_shop_i, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' );
                set_theme_mod( 'vw_gift_shop_slider_button_text'.$vw_gift_shop_i, 'Shop Now' );
                set_theme_mod( 'vw_gift_shop_slider_button_link'.$vw_gift_shop_i, '#' );
                set_theme_mod( 'vw_gift_shop_side_img'.$vw_gift_shop_i, get_template_directory_uri().'/assets/images/slider'.$vw_gift_shop_i.'.png' );
            }

            // Set a single product for the slider
            set_theme_mod('vw_gift_shop_slider_product', 'Product Name Here');

            // Define product titles
            $vw_gift_shop_title_array = array(
                "Product Name Here",
                "Product Name Here",
                "Product Name Here",
                "Product Name Here"
            );

            // Define the tag to be added to each product
            $vw_gift_shop_tag_name = 'Brand Name Here'; // Tag name

            // Create or retrieve the product tag term ID
            $vw_gift_shop_tag = term_exists($vw_gift_shop_tag_name, 'product_tag');
            if ($vw_gift_shop_tag === 0 || $vw_gift_shop_tag === null) {
                // If the tag does not exist, create it
                $vw_gift_shop_tag = wp_insert_term($vw_gift_shop_tag_name, 'product_tag');
            }

            // Loop to create 4 products
            foreach ($vw_gift_shop_title_array as $vw_gift_shop_title) {
                // Create product content
                $vw_gift_shop_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
                $vw_gift_shop_my_post = array(
                    'post_title'    => wp_strip_all_tags($vw_gift_shop_title),
                    'post_content'  => $vw_gift_shop_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'product', // Post type set to 'product'
                );

                // Insert the product into the database
                $vw_gift_shop_post_id = wp_insert_post($vw_gift_shop_my_post);

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

                // Set product as simple product and assign a price
                update_post_meta($vw_gift_shop_post_id, '_price', '39.00'); // Set a price for the product (remove $ sign)
                update_post_meta($vw_gift_shop_post_id, '_regular_price', '79.00'); // Set regular price (remove $ sign)
                update_post_meta($vw_gift_shop_post_id, '_stock_status', 'instock'); // Set stock status to 'in stock'
                update_post_meta($vw_gift_shop_post_id, '_manage_stock', 'no'); // Not managing stock for this example
                update_post_meta($vw_gift_shop_post_id, '_product_type', 'simple'); // Set product type to 'simple'

                // Assign the tag to the product
                if (!is_wp_error($vw_gift_shop_tag)) {
                    wp_set_object_terms($vw_gift_shop_post_id, (int)$vw_gift_shop_tag['term_id'], 'product_tag');
                }

                // Handle the featured image using media_sideload_image
                $vw_gift_shop_image_url = get_template_directory_uri() . '/assets/images/Product' . rand(1, 4) . '.png'; // Random image for demo purposes
                $vw_gift_shop_image_id = media_sideload_image($vw_gift_shop_image_url, $vw_gift_shop_post_id, null, 'id');

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

                // Assign featured image to product
                set_post_thumbnail($vw_gift_shop_post_id, $vw_gift_shop_image_id);
            }

            set_theme_mod( 'vw_gift_shop_explore_button_text', 'Explore More' );
            set_theme_mod( 'vw_gift_shop_explore_button_link', '#' );

            // Slider Contact Section
            set_theme_mod( 'vw_gift_shop_location_text', 'Park Ave, Portland, OR, USA' );
            set_theme_mod( 'vw_gift_shop_track_locatin_url', '#' );
            set_theme_mod( 'vw_gift_shop_phone_number', '+1 123 456 7890' );
            set_theme_mod( 'vw_gift_shop_slider_contact_text', 'Christmas Offers! ' );
            set_theme_mod( 'vw_gift_shop_slider_contact_url_text', 'Grab Deals Now' );
            set_theme_mod( 'vw_gift_shop_slider_contact_link', '#' );

            // Product Section //
            set_theme_mod( 'vw_gift_shop_new_product_section_title', 'New Products' ); 
            set_theme_mod( 'vw_gift_shop_new_product_section_text', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.' ); 

            set_theme_mod('vw_gift_shop_best_product_category', 'productcategory1');

            // Define product category names, product titles, and tags
            $vw_gift_shop_category_names = array('productcategory1', 'productcategory2', 'productcategory3', 'productcategory4');
            $vw_gift_shop_title_array = array(
                array("Short Product Name Here", "Short Product Name Here", "Short Product Name Here", "Short Product Name Here"),
                array("Short Product Name Here", "Short Product Name Here", "Short Product Name Here", "Short Product Name Here"),
                array("Short Product Name Here", "Short Product Name Here", "Short Product Name Here", "Short Product Name Here"),
                array("Short Product Name Here", "Short Product Name Here", "Short Product Name Here", "Short Product Name Here")
            );

            // Define tags for the products
            $vw_gift_shop_tag_names = array('Brand Name Here'); 

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

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

                // Loop to create 4 products for each category
                for ($vw_gift_shop_i = 0; $vw_gift_shop_i < 4; $vw_gift_shop_i++) {
                    // Create product content
                    $vw_gift_shop_title = $vw_gift_shop_title_array[$vw_gift_shop_index][$vw_gift_shop_i];
                    $vw_gift_shop_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_gift_shop_my_post = array(
                        'post_title'    => wp_strip_all_tags($vw_gift_shop_title),
                        'post_content'  => $vw_gift_shop_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'product', // Post type set to 'product'
                    );

                    // Insert the product into the database
                    $vw_gift_shop_post_id = wp_insert_post($vw_gift_shop_my_post);

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

                    // Assign tags to the product
                    wp_set_object_terms($vw_gift_shop_post_id, $vw_gift_shop_tag_names, 'product_tag');

                    // Set product as simple product and assign a price
                    update_post_meta($vw_gift_shop_post_id, '_price', '39.00'); // Set a price for the product
                    update_post_meta($vw_gift_shop_post_id, '_regular_price', '79.00'); // Set regular price
                    update_post_meta($vw_gift_shop_post_id, '_stock_status', 'instock'); // Set stock status to 'in stock'
                    update_post_meta($vw_gift_shop_post_id, '_manage_stock', 'no'); // Not managing stock for this example
                    update_post_meta($vw_gift_shop_post_id, '_product_type', 'simple'); // Set product type to 'simple'

                    // Handle the featured image using media_sideload_image
                    $vw_gift_shop_image_url = get_template_directory_uri() . '/assets/images/Product' . ($vw_gift_shop_i + 1) . '.png';
                    $vw_gift_shop_image_id = media_sideload_image($vw_gift_shop_image_url, $vw_gift_shop_post_id, null, 'id');

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

                    // Assign featured image to product
                    set_post_thumbnail($vw_gift_shop_post_id, $vw_gift_shop_image_id);
                }
            }

            //Copyright Text
            set_theme_mod( 'vw_gift_shop_footer_text', 'By VWThemes' );  
     
        }
    ?>
  
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=vw_gift_shop_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('vw_gift_shop_demo_import_completed')) : ?>
            <form method="post">   
            <p class="run-import-text"><?php esc_html_e('Click On The Below Run Importer Button To Import Demo Content Of vw gift shop', 'vw-gift-shop'); ?></p>
                <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 gift shop', 'vw-gift-shop'); ?></p>
                <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'vw-gift-shop'); ?>" class="button button-primary button-large">
                 </form> 
        <?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>
