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

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

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

        // 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_bakers_plugin_slug = 'ibtana-visual-editor';
          $vw_bakers_plugin_file = 'ibtana-visual-editor/plugin.php';

          // Check if plugin is installed
          $vw_bakers_installed_plugins = get_plugins();
          if (!isset($vw_bakers_installed_plugins[$vw_bakers_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_bakers_upgrader = new Plugin_Upgrader();
              $vw_bakers_upgrader->install('https://downloads.wordpress.org/plugin/ibtana-visual-editor.latest-stable.zip');
          }
          // Activate the plugin
          activate_plugin($vw_bakers_plugin_file);
        }
// Create or Get Menu
$vw_bakers_menu_name = 'Primary Menu';
$vw_bakers_menu = wp_get_nav_menu_object($vw_bakers_menu_name);

if (!$vw_bakers_menu) {
    $vw_bakers_menu_id = wp_create_nav_menu($vw_bakers_menu_name);
} else {
    $vw_bakers_menu_id = $vw_bakers_menu->term_id;
}
        // Create a front page and assign the template
        $vw_bakers_home_page = null;
        // Using WP_Query instead of get_page_by_title()
        $vw_bakers_home_query = new WP_Query(array(
           'post_type' => 'page',
           'title' => 'Home',
           'post_status' => 'publish',
           'posts_per_page' => 1
        ));
        if (!$vw_bakers_home_query->have_posts()) {
           $vw_bakers_home_title = 'Home';           
           // Create the page
           $vw_bakers_home = array(
               'post_type' => 'page',
               'post_title' => $vw_bakers_home_title,
               'post_status' => 'publish',
               'post_author' => 1,
               'post_slug' => 'home'
           );
           $vw_bakers_home_id = wp_insert_post($vw_bakers_home);
        } else {
           $vw_bakers_home_page = $vw_bakers_home_query->posts[0];
           $vw_bakers_home_id = $vw_bakers_home_page->ID;
        }

        // Create About Us Page
        $vw_bakers_about_title = 'About Us';
        $vw_bakers_about_content = '<p>Welcome to our Bakery! This is About Us demo content.</p>';
        $vw_bakers_about = array(
            'post_type'     => 'page',
            'post_title'    => $vw_bakers_about_title,
            'post_name'     => 'about-us',
            'post_content'  => $vw_bakers_about_content,
            'post_status'   => 'publish',
            'post_author'   => 1
        );
        $vw_bakers_about_id = wp_insert_post($vw_bakers_about);

        wp_update_nav_menu_item($vw_bakers_menu_id, 0, array(
            'menu-item-title'     => __('About Us', 'vw-bakers'),
            'menu-item-object-id' => $vw_bakers_about_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
            'menu-item-status'    => 'publish'
        ));


        // Create Shop Page
        $vw_bakers_shop_title = 'Shop';
        $vw_bakers_shop_content = '<p>This is Shop page demo content.</p>';
        $vw_bakers_shop = array(
            'post_type'     => 'page',
            'post_title'    => $vw_bakers_shop_title,
            'post_name'     => 'shop',
            'post_content'  => $vw_bakers_shop_content,
            'post_status'   => 'publish',
            'post_author'   => 1
        );
        $vw_bakers_shop_id = wp_insert_post($vw_bakers_shop);

        wp_update_nav_menu_item($vw_bakers_menu_id, 0, array(
            'menu-item-title'     => __('Shop', 'vw-bakers'),
            'menu-item-object-id' => $vw_bakers_shop_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
            'menu-item-status'    => 'publish'
        ));

        // Create Contact Us Page
        $vw_bakers_contact_title = 'Contact Us';
        $vw_bakers_contact_content = '<p>This is Contact Us page demo content.</p>';
        $vw_bakers_contact = array(
            'post_type'     => 'page',
            'post_title'    => $vw_bakers_contact_title,
            'post_name'     => 'contact-us',
            'post_content'  => $vw_bakers_contact_content,
            'post_status'   => 'publish',
            'post_author'   => 1
        );
        $vw_bakers_contact_id = wp_insert_post($vw_bakers_contact);

        wp_update_nav_menu_item($vw_bakers_menu_id, 0, array(
            'menu-item-title'     => __('Contact Us', 'vw-bakers'),
            'menu-item-object-id' => $vw_bakers_contact_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',   
            'menu-item-status'    => 'publish'
        ));

        // Create Blog Page (Posts Page)
        $vw_bakers_blog_title = 'Blog';
        $vw_bakers_blog = array(
        'post_type'     => 'page',
        'post_title'    => $vw_bakers_blog_title,
        'post_name'     => 'blog',
        'post_status'   => 'publish',
        'post_author'   => 1
        );
        $vw_bakers_blog_id = wp_insert_post($vw_bakers_blog);
        // Set Blog page as posts page
        update_option('page_for_posts', $vw_bakers_blog_id);
        wp_update_nav_menu_item($vw_bakers_menu_id, 0, array(
        'menu-item-title'     => __('Blog', 'vw-bakers'),
        'menu-item-object-id' => $vw_bakers_blog_id,
        'menu-item-object'    => 'page',
        'menu-item-type'      => 'post_type',
        'menu-item-status'    => 'publish'
        ));

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

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

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

        // Top Bar
        set_theme_mod( 'vw_bakers_topbar_text', 'Freshly Baked Delights Daily! Enjoy 10% Off on Your First Order with Code WELCOME10!' );
        set_theme_mod( 'vw_bakers_topbar_icon', 'fa-solid fa-bullhorn' );

        // Banner Settings
        set_theme_mod( 'vw_bakers_banner_small_text', 'Haven Bakes' );
        set_theme_mod( 'vw_bakers_banner_title', 'Sweet Moments, Freshly Baked: Indulge in Every Bite' );
        set_theme_mod( 'vw_bakers_banner_para_text', 'Savor the taste of perfection with our handcrafted pastries, breads, and cakes. Baked fresh daily using the finest ingredients, every bite is a delightful journey of flavor. Treat yourself to the joy of homemade goodness.' );
        set_theme_mod( 'vw_bakers_shop_button_label', 'Shop Now' );
        set_theme_mod( 'vw_bakers_shop_button_url', '#' );
        for($vw_bakers_j=1; $vw_bakers_j<=3; $vw_bakers_j++) {
            set_theme_mod( 'vw_bakers_featured_image_sec'.$vw_bakers_j, get_template_directory_uri().'/assets/images/banner'.$vw_bakers_j.'.png' );
        }

        // Our Chefs Section
        set_theme_mod( 'vw_bakers_section_text', 'Our Chefs' );
        set_theme_mod( 'vw_bakers_section_heading', 'Meet Our Master Chefs: Crafting Every Bite with Passion and Expertise.' );

        $chef_names = array('David Stewart', 'Jonathan Alfred', 'Angelina Masqurade', 'Bella Andrew');
        for ($vw_bakers_i = 1; $vw_bakers_i <= 4; $vw_bakers_i++) {
            set_theme_mod('vw_bakers_chefs_image_sec' . $vw_bakers_i, get_template_directory_uri() . '/assets/images/chef' . $vw_bakers_i . '.png');
            set_theme_mod('vw_bakers_chefs_name' . $vw_bakers_i, $chef_names[$vw_bakers_i - 1]);
        }

        echo "<script>window.location.href='" . admin_url('themes.php?page=vw_bakers_guide') . "';</script>";
        //Copyright Text
        set_theme_mod( 'vw_bakers_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 Bakers', 'vw-bakers'); ?></p>
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=vw_bakers_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('vw_bakers_demo_import_completed')) : ?>
            <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'vw-bakers'); ?>" 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>
