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

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

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

         // Check if woocommerce is installed and activated
        if (!is_plugin_active('woocommerce/woocommerce.php')) {
          // Install the plugin if it doesn't exist
          $vw_locksmith_plugin_slug = 'woocommerce';
          $vw_locksmith_plugin_file = 'woocommerce/woocommerce.php';

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

        // ------- Create Nav Menu --------
        $vw_locksmith_menuname = 'Main Menus';
        $vw_locksmith_bpmenulocation = 'primary';
        $vw_locksmith_menu_exists = wp_get_nav_menu_object($vw_locksmith_menuname);

        if (!$vw_locksmith_menu_exists) {
            $vw_locksmith_menu_id = wp_create_nav_menu($vw_locksmith_menuname);

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

            // Create About Us Page with Dummy Content
            $vw_locksmith_pages_title = 'About Us';
            $vw_locksmith_pages_content = '
            About Us all the pages we have on our website. Find information about our services, company, and more.
            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>
            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_locksmith_pages = array(
                'post_type' => 'page',
                'post_title' => $vw_locksmith_pages_title,
                'post_content' => $vw_locksmith_pages_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'explore'
            );
            $vw_locksmith_pages_id = wp_insert_post($vw_locksmith_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($vw_locksmith_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'vw-locksmith'),
                'menu-item-classes' => 'explore',
                'menu-item-url' => home_url('/about/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $vw_locksmith_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Shop Page with Dummy Content
            $vw_locksmith_about_title = 'Shop';
            $vw_locksmith_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.';
            $vw_locksmith_about = array(
                'post_type' => 'page',
                'post_title' => $vw_locksmith_about_title,
                'post_content' => $vw_locksmith_about_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'shop'
            );
            $vw_locksmith_about_id = wp_insert_post($vw_locksmith_about);
            // Add  Shop Page to Menu
            wp_update_nav_menu_item($vw_locksmith_menu_id, 0, array(
                'menu-item-title' => __('Shop', 'vw-locksmith'),
                'menu-item-classes' => 'memberShip',
                'menu-item-url' => home_url('/shop/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $vw_locksmith_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

             // Create Pages Page with Dummy Content
                $vw_locksmith_pages_title = 'Pages';
                $vw_locksmith_pages_content = '
                Explore all the pages we have on our website. Find information about our services, company, and more.
                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>
                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_locksmith_pages = array(
                    'post_type' => 'page',
                    'post_title' => $vw_locksmith_pages_title,
                    'post_content' => $vw_locksmith_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $vw_locksmith_pages_id = wp_insert_post($vw_locksmith_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($vw_locksmith_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'vw-locksmith'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $vw_locksmith_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

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

                $vw_locksmith_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($vw_locksmith_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$vw_locksmith_blog_page_query->have_posts()) {

                    $vw_locksmith_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $vw_locksmith_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $vw_locksmith_blog_page_id = wp_insert_post($vw_locksmith_blog_page);

                    update_option('page_for_posts', $vw_locksmith_blog_page_id);

                    wp_update_nav_menu_item($vw_locksmith_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'vw-locksmith'),
                        'menu-item-url'        => get_permalink($vw_locksmith_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $vw_locksmith_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }

             // Create Contact Us Page with Dummy Content
            $vw_locksmith_pages_title = 'Contact Us';
            $vw_locksmith_pages_content = '
            Contact all the pages we have on our website. Find information about our services, company, and more.
            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>
            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_locksmith_pages = array(
                'post_type' => 'page',
                'post_title' => $vw_locksmith_pages_title,
                'post_content' => $vw_locksmith_pages_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'Contact'
            );
            $vw_locksmith_pages_id = wp_insert_post($vw_locksmith_pages);
            // Add Contact Us Page to Menu
            wp_update_nav_menu_item($vw_locksmith_menu_id, 0, array(
                'menu-item-title' => __('Contact Us', 'vw-locksmith'),
                'menu-item-classes' => 'contact-us',
                'menu-item-url' => home_url('/contact-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $vw_locksmith_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Set the menu location if it's not already set
            if (!has_nav_menu($vw_locksmith_bpmenulocation)) {
                $vw_locksmith_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                if (empty($vw_locksmith_locations)) {
                    $vw_locksmith_locations = array();
                }
                $vw_locksmith_locations[$vw_locksmith_bpmenulocation] = $vw_locksmith_menu_id;
                set_theme_mod('nav_menu_locations', $vw_locksmith_locations);
            }
        }

        // Set the demo import completion flag
		update_option('vw_locksmith_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-locksmith') . '</p>';
		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'vw-locksmith') . '</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-locksmith' ) .'</a></span>';
        echo '<span><a href="'. esc_url( 'https://preview.vwthemesdemo.com/docs/vw-locksmith-free-doc/' ) .'" class="button button-primary doc-btn" target=_blank>'. esc_html__( 'Free Theme Documentation', 'vw-locksmith' ) .'</a></span>';
        //end 
         
        // Top Bar 
        set_theme_mod('vw_locksmith_topbar_hide_show',true);   
        set_theme_mod('vw_locksmith_topbar_location_icon','fas fa-map-marker');
        set_theme_mod('vw_locksmith_topbar_location_text','82 12th Street, Office 14, LA'); 
        set_theme_mod('vw_locksmith_track_locatin_url','#');  
        set_theme_mod('vw_locksmith_topbar_time_icon','fas fa-clock');  
        set_theme_mod('vw_locksmith_topbar_time_text','Monday To Friday 09:00 am To 05:00 Pm');  
        set_theme_mod('vw_locksmith_top_email_address_icon','fa-solid fa-envelope-open');  
        set_theme_mod('vw_locksmith_top_email_address','locksmith12@example.com');  
        set_theme_mod('vw_locksmith_topbar_phone_icon','fa-solid fa-phone-flip');  
        set_theme_mod('vw_locksmith_topbar_phone_text','Get Consultation');  
        set_theme_mod('vw_locksmith_topbar_phone_number','8005122050');  
        set_theme_mod('vw_locksmith_search_hide_show',true);  
        set_theme_mod('vw_locksmith_search_open_icon','fas fa-search');  
        set_theme_mod('vw_locksmith_search_close_icon','fas fa-search');

        // Slider Section 
        set_theme_mod('vw_locksmith_hide_show_slider_section', true);   
        set_theme_mod('vw_locksmith_slide_number', 3);

        // Slider Images
        $vw_locksmith_slider_images = array(
            'banner-img.png','banner-img1.png','banner-img2.png'
        );
        // Titles & Content
        $vw_locksmith_small_titles = array(
            'Welcome To Locksmith', 'Trusted Locksmith Experts', 'Secure Your Home Today'
        );
        $vw_locksmith_titles = array(
            'Fast, Reliable Locksmith Services – 24/7', 'Professional Lock Repair & Installation','Your Safety Is Our Priority Lock Repair'
        );
        $vw_locksmith_texts = array(
            'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
            'We provide high-quality locksmith services for homes and businesses.',
            'Get quick and secure solutions for all your lock-related needs.'
        );

        for ($vw_locksmith_i = 1; $vw_locksmith_i <= 3; $vw_locksmith_i++) {
            set_theme_mod(
                'vw_locksmith_slider_bg_img' . $vw_locksmith_i,
                get_template_directory_uri() . '/assets/images/' . $vw_locksmith_slider_images[$vw_locksmith_i - 1]
            );
            // Text content
            set_theme_mod('vw_locksmith_slider_small_title' . $vw_locksmith_i, $vw_locksmith_small_titles[$vw_locksmith_i - 1]);
            set_theme_mod('vw_locksmith_slider_title' . $vw_locksmith_i, $vw_locksmith_titles[$vw_locksmith_i - 1]);
            set_theme_mod('vw_locksmith_slider_text' . $vw_locksmith_i, $vw_locksmith_texts[$vw_locksmith_i - 1]);
            // Buttons (same for all)
            set_theme_mod('vw_locksmith_slider_button_label' . $vw_locksmith_i, 'READ MORE');  
            set_theme_mod('vw_locksmith_slider_button_url' . $vw_locksmith_i, '#');  
            set_theme_mod('vw_locksmith_slider_next_button_label' . $vw_locksmith_i, 'GET IN APPOINTMENT');  
            set_theme_mod('vw_locksmith_slider_next_button_url' . $vw_locksmith_i, '#');
        }
       
        // Product Section
        set_theme_mod('vw_locksmith_our_product_section', true);
        set_theme_mod('vw_locksmith_products_section_hide_show', true);
        set_theme_mod('vw_locksmith_products_section_title', 'Our Product');
        set_theme_mod('vw_locksmith_products_section_title_icon', '');
        set_theme_mod('vw_locksmith_product_sec_button_label', 'View More Product');
        set_theme_mod('vw_locksmith_product_sec_button_url', '#');
        set_theme_mod('vw_locksmith_products_section_title_icon','fa-solid fa-door-closed');
        set_theme_mod('vw_locksmith_best_product_category', 'category1');

        $vw_locksmith_categories = array('Category1', 'Category2', 'Category3', 'Category4', 'Category5');
        // Product titles
        $vw_locksmith_titles = array(
            array("PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01"),
            array("PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01"),
            array("PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01"),
            array("PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01"),
            array("PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01", "PRODUCT NAME HWER 01")
        );
            foreach ($vw_locksmith_categories as $vw_locksmith_index => $vw_locksmith_cat_name) {
                // Create or get category
                $vw_locksmith_term = term_exists($vw_locksmith_cat_name, 'product_cat');
                if (!$vw_locksmith_term) {
                    $vw_locksmith_term = wp_insert_term($vw_locksmith_cat_name, 'product_cat');
                }if (is_wp_error($vw_locksmith_term)) {
                    continue;
                }
                $vw_locksmith_term_id = is_array($vw_locksmith_term) ? $vw_locksmith_term['term_id'] : $vw_locksmith_term;
                // Create 4 products
                for ($vw_locksmith_i = 0; $vw_locksmith_i < 4; $vw_locksmith_i++) {

                    $vw_locksmith_post_id = wp_insert_post(array(
                        'post_title'  => $vw_locksmith_titles[$vw_locksmith_index][$vw_locksmith_i],
                        'post_status' => 'publish',
                        'post_type'   => 'product',
                    ));

                    if (is_wp_error($vw_locksmith_post_id)) {
                        continue;
                    }
                    // Assign category
                    wp_set_object_terms($vw_locksmith_post_id, $vw_locksmith_term_id, 'product_cat');
                    wp_set_object_terms($vw_locksmith_post_id, 'simple', 'product_type');
                    // Price only
                    update_post_meta($vw_locksmith_post_id, '_regular_price', '120');
                    $vw_locksmith_image_path = get_template_directory() . '/assets/images/Product' . ($vw_locksmith_i + 1) . '.png';

                    if (file_exists($vw_locksmith_image_path)) {
                       $vw_locksmith_upload = wp_upload_bits(basename($vw_locksmith_image_path), null, file_get_contents($vw_locksmith_image_path));
                        if (!$vw_locksmith_upload['error']) {
                            $vw_locksmith_attachment_id = wp_insert_attachment(array(
                                'post_mime_type' => mime_content_type($vw_locksmith_image_path),
                                'post_title'     => basename($vw_locksmith_image_path),
                                'post_status'    => 'inherit'
                            ), $vw_locksmith_upload['file'], $vw_locksmith_post_id);

                            set_post_thumbnail($vw_locksmith_post_id, $vw_locksmith_attachment_id);
                        }
                    }
               }
            }
                echo "<script>window.location.href='" . admin_url('themes.php?page=vw_locksmith_guide') . "';</script>";
                set_theme_mod( 'vw_locksmith_footer_text', 'By VWThemes' );  
        }

    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=vw_locksmith_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('vw_locksmith_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 Locksmith', 'vw-locksmith'); ?></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 Locksmith', 'vw-locksmith'); ?></p>
            <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'vw-locksmith'); ?>" 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>
