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

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

		// POST and update the customizer and other related data
        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_wholesaler_plugin_slug = 'woocommerce';
          $vw_wholesaler_plugin_file = 'woocommerce/woocommerce.php';

          // Check if plugin is installed
          $vw_wholesaler_installed_plugins = get_plugins();
          if (!isset($vw_wholesaler_installed_plugins[$vw_wholesaler_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_wholesaler_upgrader = new Plugin_Upgrader();
              $vw_wholesaler_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
          }
          // Activate the plugin
          activate_plugin($vw_wholesaler_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_wholesaler_plugin_slug = 'ibtana-visual-editor';
          $vw_wholesaler_plugin_file = 'ibtana-visual-editor/plugin.php';

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

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

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

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

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

        // Top Bar
        set_theme_mod( 'vw_wholesaler_topbar_text', 'Limited Time Offer: Get Extra Discounts on Bulk Orders – Shop Now!' );
        set_theme_mod( 'vw_wholesaler_topbar_icon', 'fa-solid fa-bullhorn' );


        // Banner Settings
        set_theme_mod( 'vw_wholesaler_show_hide_banner',true);
        set_theme_mod( 'vw_wholesaler_banner_small_text', 'BulkHub' );
        set_theme_mod( 'vw_wholesaler_banner_title', 'Your One-Stop Destination for Premium Wholesale Products at Unbeatable Prices' );
        set_theme_mod( 'vw_wholesaler_banner_para_text', 'Discover unbeatable deals on top-quality products at BulkZone. From electronics to apparel, shop our extensive selection of wholesale goods and enjoy exclusive discounts on bulk orders. Start saving today!' );
        set_theme_mod( 'vw_wholesaler_explore_button_label', 'Explore more' );
        set_theme_mod( 'vw_wholesaler_explore_button_url', '#' );
        set_theme_mod( 'vw_wholesaler_explore_btn_icon', 'fa-solid fa-angle-right' );
        set_theme_mod( 'vw_wholesaler_banner_bg_text', 'BulkHub' );
        for($vw_wholesaler_j=1; $vw_wholesaler_j<=9; $vw_wholesaler_j++) {
            set_theme_mod( 'vw_wholesaler_featured_image_sec'.$vw_wholesaler_j, get_template_directory_uri().'/assets/images/banner'.$vw_wholesaler_j.'.png' );
        }

        // Category Section
        set_theme_mod('vw_wholesaler_hide_show_category_section',true);
        set_theme_mod( 'vw_wholesaler_section_text', 'All Category' );
        set_theme_mod( 'vw_wholesaler_section_heading', 'Explore our diverse range of product categories.' );

        // Define product category names, product titles, and tags
        $vw_wholesaler_category_names = array('Electronics', 'Apparel', 'Home Goods', 'Beauty Products', 'Food');
        $vw_wholesaler_title_array = array(
            array("Shirt pack of 3", "Jeans pack of 3", "Cups pack of 11"),
            array("Shirt pack of 3", "Jeans pack of 3", "Cups pack of 11"),
            array("Shirt pack of 3", "Jeans pack of 3", "Cups pack of 11"),
            array("Shirt pack of 3", "Jeans pack of 3", "Cups pack of 11"),
            array("Shirt pack of 3", "Jeans pack of 3", "Cups pack of 11"),
        );

        $vw_wholesaler_tag_names = array('Brand Name Here');

        // Category icons
        $vw_wholesaler_category_icons = array(
            '<i class="fa-solid fa-lightbulb"></i>',
            '<i class="fa-solid fa-user-tie"></i>',
            '<i class="fa-solid fa-house"></i>',
            '<i class="fa-solid fa-spray-can"></i>',
            '<i class="fa-solid fa-bowl-food"></i>'
        );

        foreach ($vw_wholesaler_category_names as $vw_wholesaler_index => $vw_wholesaler_category_name) {
            // Create or retrieve the product category term ID
            $vw_wholesaler_term = term_exists($vw_wholesaler_category_name, 'product_cat');
            if ($vw_wholesaler_term === 0 || $vw_wholesaler_term === null) {
                // Create term if it doesn't exist
                $vw_wholesaler_term = wp_insert_term($vw_wholesaler_category_name, 'product_cat');
                if (!is_wp_error($vw_wholesaler_term)) {
                    // Add category icon meta for the new category
                    update_term_meta($vw_wholesaler_term['term_id'], 'category_icon_html', $vw_wholesaler_category_icons[$vw_wholesaler_index]);
                }
            }

            if (is_wp_error($vw_wholesaler_term)) {
                error_log('Error creating category: ' . $vw_wholesaler_term->get_error_message());
                continue; // Skip if category creation fails
            }

            for ($vw_wholesaler_i = 0; $vw_wholesaler_i < 3; $vw_wholesaler_i++) {
                // Create product content
                $vw_wholesaler_title = $vw_wholesaler_title_array[$vw_wholesaler_index][$vw_wholesaler_i];
                $vw_wholesaler_content = 'This is a description for ' . $vw_wholesaler_title;

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

                // Insert the product into the database
                $vw_wholesaler_post_id = wp_insert_post($vw_wholesaler_my_post);

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

                // Assign tags to the product
                wp_set_object_terms($vw_wholesaler_post_id, $vw_wholesaler_tag_names, 'product_tag');

                // Set product metadata
                update_post_meta($vw_wholesaler_post_id, '_regular_price', '29'); // Set regular price
                update_post_meta($vw_wholesaler_post_id, '_sale_price', '19'); // Set sale price
                update_post_meta($vw_wholesaler_post_id, '_price', '19'); // Current price (sale price or regular price)
                update_post_meta($vw_wholesaler_post_id, '_stock_status', 'instock'); // Set stock status to 'in stock'
                update_post_meta($vw_wholesaler_post_id, '_manage_stock', 'no'); // Not managing stock
                update_post_meta($vw_wholesaler_post_id, '_product_type', 'simple'); // Set product type to 'simple'

                // Handle featured image
                $vw_wholesaler_image_url = get_template_directory_uri() . '/assets/images/Product' . ($vw_wholesaler_i + 1) . '.png';
                $vw_wholesaler_image_id = media_sideload_image($vw_wholesaler_image_url, $vw_wholesaler_post_id, null, 'id');

                if (!is_wp_error($vw_wholesaler_image_id)) {
                    set_post_thumbnail($vw_wholesaler_post_id, $vw_wholesaler_image_id);
                } else {
                    error_log('Error downloading image: ' . $vw_wholesaler_image_id->get_error_message());
                }
            }
        }

        //Copyright Text
        set_theme_mod( 'vw_wholesaler_footer_text', 'By VWThemes' );  
     
        }
    ?>
  
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=vw_wholesaler_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('vw_wholesaler_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 wholesaler', 'vw-wholesaler'); ?></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 wholesaler', 'vw-wholesaler'); ?></p>
                <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'vw-wholesaler'); ?>" 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>
