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

        // If the demo import is completed, display the "View Site" button
        if ($handyman_cleaner_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'handyman-cleaner') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'handyman-cleaner') . '</a></span>';
        }
		
        if (isset($_POST['submit'])) {

            // ------- Create Nav Menu --------
            $handyman_cleaner_menuname = 'Main Menus';
            $handyman_cleaner_bpmenulocation = 'primary';
            $handyman_cleaner_menu_exists = wp_get_nav_menu_object($handyman_cleaner_menuname);

            if (!$handyman_cleaner_menu_exists) {
                $handyman_cleaner_menu_id = wp_create_nav_menu($handyman_cleaner_menuname);

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

                // Create About Us Page with Dummy Content
                $handyman_cleaner_about_title = 'About Us';
                $handyman_cleaner_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.';
                $handyman_cleaner_about = array(
                    'post_type' => 'page',
                    'post_title' => $handyman_cleaner_about_title,
                    'post_content' => $handyman_cleaner_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $handyman_cleaner_about_id = wp_insert_post($handyman_cleaner_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($handyman_cleaner_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'handyman-cleaner'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $handyman_cleaner_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $handyman_cleaner_blog_page_title = 'Blog';
                $handyman_cleaner_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($handyman_cleaner_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$handyman_cleaner_blog_page_query->have_posts()) {
                    $handyman_cleaner_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $handyman_cleaner_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $handyman_cleaner_blog_page_id = wp_insert_post($handyman_cleaner_blog_page);
                    update_option('page_for_posts', $handyman_cleaner_blog_page_id);
                    wp_update_nav_menu_item($handyman_cleaner_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'handyman-cleaner'),
                    'menu-item-url'        => get_permalink($handyman_cleaner_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $handyman_cleaner_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                   ));
                }

                // Create Pages Page with Dummy Content
                $handyman_cleaner_contact_title = 'Contact Us';
                $handyman_cleaner_contact_content = 'Explore all the contact 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 contactly 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.';
                $handyman_cleaner_contact = array(
                    'post_type' => 'page',
                    'post_title' => $handyman_cleaner_contact_title,
                    'post_content' => $handyman_cleaner_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $handyman_cleaner_contact_id = wp_insert_post($handyman_cleaner_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($handyman_cleaner_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'handyman-cleaner'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $handyman_cleaner_contact_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

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

            //Topbar
            set_theme_mod('handyman_cleaner_email_address_text', 'Mail To Us');
            set_theme_mod('handyman_cleaner_email_address1', 'xyz123@example.com');
            set_theme_mod('handyman_cleaner_phone_text', 'Call For Help');
            set_theme_mod('handyman_cleaner_phone_number', '0123456789');
            set_theme_mod('handyman_cleaner_time', '9am To 8pm');
            set_theme_mod('handyman_cleaner_day', 'Monday To Saturday');
            set_theme_mod('handyman_cleaner_location_text', 'Address');
            set_theme_mod('handyman_cleaner_add_location', 'Albert Berlin Germany');

            //Social Icon
            set_theme_mod('handyman_cleaner_facebook_url', 'https://www.facebook.com');
            set_theme_mod('handyman_cleaner_twitter_url', 'https://www.twitter.com');
            set_theme_mod('handyman_cleaner_youtube_url', 'https://www.youtube.com');
            set_theme_mod('handyman_cleaner_instagram_url', 'https://www.instagram.com');

            //Slider Section
            set_theme_mod('handyman_cleaner_slider_hide', true);
            set_theme_mod('handyman_cleaner_slider_icon', true);
            set_theme_mod('handyman_cleaner_slider_button_text', 'Learn More');
            set_theme_mod('handyman_cleaner_slider_button_link', '#');

            $handyman_cleaner_slider_titles = array(
                'A Tradition Of Quality Cleaning',
                'We Bring Sparkle To Your Space',
                'Affordable, Reliable, And Professional Cleaning',
                'Making Your Home Shine Like New',
            );
            
            for ($handyman_cleaner_i = 0; $handyman_cleaner_i < count($handyman_cleaner_slider_titles); $handyman_cleaner_i++) {
                $handyman_cleaner_slider_title = $handyman_cleaner_slider_titles[$handyman_cleaner_i];
                $handyman_cleaner_slider_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
                
                // Create post object
                $handyman_cleaner_my_post = array(
                    'post_title'    => wp_strip_all_tags($handyman_cleaner_slider_title),
                    'post_content'  => $handyman_cleaner_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );
            
                // Insert the post into the database
                $handyman_cleaner_post_id = wp_insert_post($handyman_cleaner_my_post);
            
                if ($handyman_cleaner_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('handyman_cleaner_slider' . ($handyman_cleaner_i + 1), $handyman_cleaner_post_id);
            
                    $handyman_cleaner_image_url = get_template_directory_uri() . '/images/slider' . ($handyman_cleaner_i + 1) . '.png';
            
                    // Download the image and associate it with the post
                    $handyman_cleaner_image_id = media_sideload_image($handyman_cleaner_image_url, $handyman_cleaner_post_id, null, 'id');
            
                    if (!is_wp_error($handyman_cleaner_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($handyman_cleaner_post_id, $handyman_cleaner_image_id);
                    }
                }
            }            

            //planning section
            set_theme_mod('handyman_cleaner_price_section', true);
            set_theme_mod('handyman_cleaner_show_pricing_section', true);
            // Plan1
            set_theme_mod('handyman_cleaner_regular_small_title', 'Regular Cleaning');
            set_theme_mod('handyman_cleaner_regular_price', '$120');

            // Define features for Regular Cleaning
            $handyman_cleaner_regular_cleaning_features = [
                'Cleaning with one cleaner',
                'Scheduled Cleaning',
                'Additional Payment',
                'Only Detergents',
            ];
            foreach ($handyman_cleaner_regular_cleaning_features as $handyman_cleaner_key => $handyman_cleaner_feature) {
                set_theme_mod('handyman_cleaner_regular_about_page_list' . ($handyman_cleaner_key + 1), $handyman_cleaner_feature);
            }

            set_theme_mod('handyman_cleaner_regular_button_text', 'Order Cleaning');
            set_theme_mod('handyman_cleaner_regular_button_link', '#');

            // Plan2
            set_theme_mod('handyman_cleaner_small_title', 'Premium Cleaning');
            set_theme_mod('handyman_cleaner_price', '$350');

            // Define features for Premium Cleaning
            $handyman_cleaner_premium_cleaning_features = [
                'Pro Team Of 4 Cleaners',
                'Cleaning is Managed',
                'All-Inclusive Cleaning',
                'The Presence of All Devices',
            ];
            foreach ($handyman_cleaner_premium_cleaning_features as $handyman_cleaner_key => $handyman_cleaner_feature) {
                set_theme_mod('handyman_cleaner_about_page_list' . ($handyman_cleaner_key + 1), $handyman_cleaner_feature);
            }

            set_theme_mod('handyman_cleaner_card_button_text', 'Order Cleaning');
            set_theme_mod('handyman_cleaner_button_link', '#');

            // Set values for single settings
            set_theme_mod('handyman_cleaner_price_page_small_title', 'Pricing');
            set_theme_mod('handyman_cleaner_price_page_heading', 'The Best Prices You Can Only Find');
            set_theme_mod('handyman_cleaner_price_page_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, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea.');
        
            $handyman_cleaner_default_texts = array(
                'Performer Arrives at the Appointed Time',
                'Cliners are Monitored, Including Reviews',
                'Performers are Selected and Trained',
                'Standard Cleaning Plan can be Adapted'
            );
        
            for ($handyman_cleaner_j = 1; $handyman_cleaner_j <= 4; $handyman_cleaner_j++) {
                // Set default texts
                $handyman_cleaner_text_setting = 'handyman_cleaner_regular_pricing_page_text' . $handyman_cleaner_j;
                $handyman_cleaner_text_value = isset($handyman_cleaner_default_texts[$handyman_cleaner_j - 1]) ? $handyman_cleaner_default_texts[$handyman_cleaner_j - 1] : '';
                set_theme_mod($handyman_cleaner_text_setting, $handyman_cleaner_text_value);
            }
            
            // Set the demo import completion flag
    		update_option('handyman_cleaner_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.', 'handyman-cleaner') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'handyman-cleaner') . '</a></span>';
            //end

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=handyman-cleaner-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('handyman_cleaner_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','handyman-cleaner'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','handyman-cleaner'); ?>" class="button button-primary button-large">
        </form>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
            if (confirm("Do you really want to import the theme demo content?")) {
			// Show loader
			document.getElementById('demo-import-loader').style.display = 'block';
			
			// Submit form
			document.forms[0].submit();
            } else {
                return false;
            }
        }
	</script>
</div>
