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

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

            // ------- Create Nav Menu --------
            $swimming_pool_cleaning_menuname = 'Main Menus';
            $swimming_pool_cleaning_bpmenulocation = 'primary';
            $swimming_pool_cleaning_menu_exists = wp_get_nav_menu_object($swimming_pool_cleaning_menuname);

            if (!$swimming_pool_cleaning_menu_exists) {
                $swimming_pool_cleaning_menu_id = wp_create_nav_menu($swimming_pool_cleaning_menuname);

                // Create Home Page
                $swimming_pool_cleaning_home_title = 'Home';
                $swimming_pool_cleaning_home = array(
                    'post_type' => 'page',
                    'post_title' => $swimming_pool_cleaning_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $swimming_pool_cleaning_home_id = wp_insert_post($swimming_pool_cleaning_home);
                // Assign Home Page Template
                add_post_meta($swimming_pool_cleaning_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', $swimming_pool_cleaning_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($swimming_pool_cleaning_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'swimming-pool-cleaning'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $swimming_pool_cleaning_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $swimming_pool_cleaning_about_title = 'About Us';
                $swimming_pool_cleaning_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.';
                $swimming_pool_cleaning_about = array(
                    'post_type' => 'page',
                    'post_title' => $swimming_pool_cleaning_about_title,
                    'post_content' => $swimming_pool_cleaning_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $swimming_pool_cleaning_about_id = wp_insert_post($swimming_pool_cleaning_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($swimming_pool_cleaning_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'swimming-pool-cleaning'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $swimming_pool_cleaning_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $swimming_pool_cleaning_blog_page_title = 'Blog';
                $swimming_pool_cleaning_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($swimming_pool_cleaning_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$swimming_pool_cleaning_blog_page_query->have_posts()) {
                    $swimming_pool_cleaning_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $swimming_pool_cleaning_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $swimming_pool_cleaning_blog_page_id = wp_insert_post($swimming_pool_cleaning_blog_page);
                    update_option('page_for_posts', $swimming_pool_cleaning_blog_page_id);
                    wp_update_nav_menu_item($swimming_pool_cleaning_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'swimming-pool-cleaning'),
                    'menu-item-url'        => get_permalink($swimming_pool_cleaning_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $swimming_pool_cleaning_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                   ));
                }

                // Create Pages Page with Dummy Content
                $swimming_pool_cleaning_contact_title = 'Contact Us';
                $swimming_pool_cleaning_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.';
                $swimming_pool_cleaning_contact = array(
                    'post_type' => 'page',
                    'post_title' => $swimming_pool_cleaning_contact_title,
                    'post_content' => $swimming_pool_cleaning_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $swimming_pool_cleaning_contact_id = wp_insert_post($swimming_pool_cleaning_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($swimming_pool_cleaning_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'swimming-pool-cleaning'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $swimming_pool_cleaning_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($swimming_pool_cleaning_bpmenulocation)) {
                    $swimming_pool_cleaning_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($swimming_pool_cleaning_locations)) {
                        $swimming_pool_cleaning_locations = array();
                    }
                    $swimming_pool_cleaning_locations[$swimming_pool_cleaning_bpmenulocation] = $swimming_pool_cleaning_menu_id;
                    set_theme_mod('nav_menu_locations', $swimming_pool_cleaning_locations);
                }
            }

            //Header
            set_theme_mod('swimming_pool_cleaning_timing_text', 'Service Timing:8:00 am - 6:00 pm');
            set_theme_mod('swimming_pool_cleaning_location', 'Contact Us');
            set_theme_mod('swimming_pool_cleaning_location1', '42 Street, New York, UK');
            set_theme_mod('swimming_pool_cleaning_email_address', 'Email');
            set_theme_mod('swimming_pool_cleaning_email_address1', 'xyz123@example.com');
            set_theme_mod('swimming_pool_cleaning_call_number', 'Get Free Estimate');
            set_theme_mod('swimming_pool_cleaning_number1', '+123 456 789');
            set_theme_mod('swimming_pool_cleaning_header_btn_text', 'Get a Quote');
            set_theme_mod('swimming_pool_cleaning_header_btn_url', '#');

            // Slider
            set_theme_mod( 'swimming_pool_cleaning_slider_hide', true );
            set_theme_mod('swimming_pool_cleaning_slider_small_title', 'We are');
            set_theme_mod('swimming_pool_cleaning_slider_button_text', 'View All Services');
            set_theme_mod('swimming_pool_cleaning_slider_button_link', '#');
            set_theme_mod('swimming_pool_cleaning_slider_bg_image', esc_url(get_template_directory_uri() . '/images/Slider.png'));

            for($swimming_pool_cleaning_i=1;$swimming_pool_cleaning_i<=4;$swimming_pool_cleaning_i++){
                $swimming_pool_cleaning_slider_title = array(
                    'Certified & Experienced Pool Cleaners',
                    'Keep Your Pool Sparkling All Year Round',
                    'Affordable Pool Maintenance Solutions',
                    'Your Trusted Partner in Pool Care'
                );                
                $swimming_pool_cleaning_current_title = $swimming_pool_cleaning_slider_title[$swimming_pool_cleaning_i - 1];
                $swimming_pool_cleaning_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ';
                // Create post object
                $swimming_pool_cleaning_my_post = array(
                'post_title'    => wp_strip_all_tags( $swimming_pool_cleaning_current_title ),
                'post_content'  => $swimming_pool_cleaning_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
                );
 
                // Insert the post into the database
                $swimming_pool_cleaning_post_id = wp_insert_post( $swimming_pool_cleaning_my_post );
 
                if ($swimming_pool_cleaning_post_id) {
                  // Set the theme mod for the slider page
                  set_theme_mod('swimming_pool_cleaning_slider' . $swimming_pool_cleaning_i, $swimming_pool_cleaning_post_id);
 
                   $swimming_pool_cleaning_image_url = get_template_directory_uri().'/images/slider-right'.$swimming_pool_cleaning_i.'.png';
 
                 $swimming_pool_cleaning_image_id = media_sideload_image($swimming_pool_cleaning_image_url, $swimming_pool_cleaning_post_id, null, 'id');
 
                     if (!is_wp_error($swimming_pool_cleaning_image_id)) {
                         // Set the downloaded image as the post's featured image
                         set_post_thumbnail($swimming_pool_cleaning_post_id, $swimming_pool_cleaning_image_id);
                     }
                 }
            } 

            // Service Section
            set_theme_mod( 'swimming_pool_cleaning_choose_us_enable', true );
            set_theme_mod('swimming_pool_cleaning_section_small_title', 'With 25 years of pool experience ');
            set_theme_mod('swimming_pool_cleaning_section_title', 'Expert in Cleaning Services Reasons to Choose Us');
            set_theme_mod('swimming_pool_cleaning_service_image', esc_url(get_template_directory_uri() . '/images/service.png'));
            set_theme_mod('swimming_pool_cleaning_choose_us_posts_number', '4');

            // Set the service category for the theme mod
            set_theme_mod('swimming_pool_cleaning_serv_category', 'postcategory1');

            // Define category names and post titles/content
            $swimming_pool_cleaning_category_names = array('postcategory1', 'postcategory2', 'postcategory3', 'postcategory4');
            $swimming_pool_cleaning_title_array = array(
                array("25 Years of Pool Service", "Expert Pool Repairs", "Quality Guarantee", "Experienced Technicians"),
                array("25 Years of Pool Service", "Expert Pool Repairs", "Quality Guarantee", "Experienced Technicians"),
                array("25 Years of Pool Service", "Expert Pool Repairs", "Quality Guarantee", "Experienced Technicians"),
                array("25 Years of Pool Service", "Expert Pool Repairs", "Quality Guarantee", "Experienced Technicians")
            );

            $swimming_pool_cleaning_content_array = array(
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                ),
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                ),
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                ),
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                )
            );

            // Icons for each service post
            $swimming_pool_cleaning_icons = [
                'fas fa-wrench', // Icon for "25 Years of Pool Service"
                'fas fa-toolbox', // Icon for "Expert Pool Repairs"
                'fas fa-hammer',  // Icon for "Expert Pool Repairs" (second one)
                'fas fa-users'    // Icon for "Experienced Technicians"
            ];

            // Loop over category names to create posts and categories
            foreach ($swimming_pool_cleaning_category_names as $swimming_pool_cleaning_index => $swimming_pool_cleaning_category_name) {
                // Create or retrieve the post category term ID
                $swimming_pool_cleaning_term = term_exists($swimming_pool_cleaning_category_name, 'category');
                if (!$swimming_pool_cleaning_term || is_wp_error($swimming_pool_cleaning_term)) {
                    $swimming_pool_cleaning_term = wp_insert_term($swimming_pool_cleaning_category_name, 'category');
                    if (is_wp_error($swimming_pool_cleaning_term)) {
                        error_log('Error creating category: ' . $swimming_pool_cleaning_term->get_error_message());
                        continue; // Skip to the next iteration if category creation fails
                    }
                }

                $swimming_pool_cleaning_term_id = is_array($swimming_pool_cleaning_term) ? $swimming_pool_cleaning_term['term_id'] : $swimming_pool_cleaning_term;

                // Loop to create posts under each category
                for ($swimming_pool_cleaning_i = 0; $swimming_pool_cleaning_i < 4; $swimming_pool_cleaning_i++) {
                    // Create post content
                    $swimming_pool_cleaning_title = $swimming_pool_cleaning_title_array[$swimming_pool_cleaning_index][$swimming_pool_cleaning_i];
                    $swimming_pool_cleaning_content = $swimming_pool_cleaning_content_array[$swimming_pool_cleaning_index][$swimming_pool_cleaning_i];

                    // Create post object
                    $swimming_pool_cleaning_my_post = array(
                        'post_title'    => wp_strip_all_tags($swimming_pool_cleaning_title),
                        'post_content'  => $swimming_pool_cleaning_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $swimming_pool_cleaning_post_id = wp_insert_post($swimming_pool_cleaning_my_post);

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

                    // Assign the category to the post
                    wp_set_post_categories($swimming_pool_cleaning_post_id, array((int)$swimming_pool_cleaning_term_id));

                    // Log the post creation and assigned category
                    error_log('Post Created: ' . $swimming_pool_cleaning_title . ' with ID ' . $swimming_pool_cleaning_post_id . ' assigned to category ID ' . $swimming_pool_cleaning_term_id);

                    // Set the post's featured image if needed
                    // For each post, you may assign a unique image if needed or follow the same image loading mechanism
                }
            }

            // Set the service icons for each service post in the theme mods
            for ($swimming_pool_cleaning_i = 0; $swimming_pool_cleaning_i < 4; $swimming_pool_cleaning_i++) {
                set_theme_mod('swimming_pool_cleaning_service_posts_icon' . ($swimming_pool_cleaning_i + 1), $swimming_pool_cleaning_icons[$swimming_pool_cleaning_i]);
                error_log('Set icon for service ' . ($swimming_pool_cleaning_i + 1) . ': ' . $swimming_pool_cleaning_icons[$swimming_pool_cleaning_i]);
            }

            // Set the demo import completion flag
    		update_option('swimming_pool_cleaning_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.', 'swimming-pool-cleaning') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'swimming-pool-cleaning') . '</a></span>';
            //end
   
        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=swimming-pool-cleaning-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('swimming_pool_cleaning_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','swimming-pool-cleaning'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','swimming-pool-cleaning'); ?>" 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>
