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

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

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

            // ------- Create Nav Menu --------
            $summer_camp_menuname = 'Main Menus';
            $summer_camp_bpmenulocation = 'primary';
            $summer_camp_menu_exists = wp_get_nav_menu_object($summer_camp_menuname);

            if (!$summer_camp_menu_exists) {
                $summer_camp_menu_id = wp_create_nav_menu($summer_camp_menuname);

                // Create Home Page
                $summer_camp_home_title = 'Home';
                $summer_camp_home = array(
                    'post_type' => 'page',
                    'post_title' => $summer_camp_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $summer_camp_home_id = wp_insert_post($summer_camp_home);
                // Assign Home Page Template
                add_post_meta($summer_camp_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', $summer_camp_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($summer_camp_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'summer-camp'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $summer_camp_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $summer_camp_pages_title = 'Pages';
                $summer_camp_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.';
                $summer_camp_pages = array(
                    'post_type' => 'page',
                    'post_title' => $summer_camp_pages_title,
                    'post_content' => $summer_camp_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $summer_camp_pages_id = wp_insert_post($summer_camp_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($summer_camp_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'summer-camp'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $summer_camp_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $summer_camp_about_title = 'About Us';
                $summer_camp_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.';
                $summer_camp_about = array(
                    'post_type' => 'page',
                    'post_title' => $summer_camp_about_title,
                    'post_content' => $summer_camp_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $summer_camp_about_id = wp_insert_post($summer_camp_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($summer_camp_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'summer-camp'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $summer_camp_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Camp Page
                $summer_camp_camp_title = 'Camp';
                $summer_camp_camp_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
                Our summer camp offers fun-filled activities, outdoor adventures, team building games, and learning experiences for children of all age groups.';

                $summer_camp_camp = array(
                    'post_type'   => 'page',
                    'post_title'  => $summer_camp_camp_title,
                    'post_content'=> $summer_camp_camp_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug'   => 'camp'
                );

                $summer_camp_camp_id = wp_insert_post($summer_camp_camp);

                // Add Camp Page to Menu
                wp_update_nav_menu_item($summer_camp_menu_id, 0, array(
                    'menu-item-title'     => __('Camp', 'summer-camp'),
                    'menu-item-classes'  => 'camp',
                    'menu-item-url'      => home_url('/camp/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $summer_camp_camp_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

                // Create Gallery Page
                $summer_camp_gallery_title = 'Gallery';
                $summer_camp_gallery_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
                Explore memorable moments from our summer camps through photos and videos showcasing fun, learning, and adventure.';

                $summer_camp_gallery = array(
                    'post_type'   => 'page',
                    'post_title'  => $summer_camp_gallery_title,
                    'post_content'=> $summer_camp_gallery_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug'   => 'gallery'
                );

                $summer_camp_gallery_id = wp_insert_post($summer_camp_gallery);

                // Add Gallery Page to Menu
                wp_update_nav_menu_item($summer_camp_menu_id, 0, array(
                    'menu-item-title'     => __('Gallery', 'summer-camp'),
                    'menu-item-classes'  => 'gallery',
                    'menu-item-url'      => home_url('/gallery/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $summer_camp_gallery_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

                // Create Blog Page 
                $summer_camp_blog_page_title = 'Blog';
                $summer_camp_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($summer_camp_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$summer_camp_blog_page_query->have_posts()) {
                    $summer_camp_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $summer_camp_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $summer_camp_blog_page_id = wp_insert_post($summer_camp_blog_page);
                    update_option('page_for_posts', $summer_camp_blog_page_id);
                    wp_update_nav_menu_item($summer_camp_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'summer-camp'),
                    'menu-item-url'        => get_permalink($summer_camp_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $summer_camp_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }
                
                // Create Contact Us Page
                $summer_camp_contact_title = 'Contact Us';
                $summer_camp_contact_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
                Get in touch with us for camp registrations, schedules, or any questions. We would love to hear from you.';

                $summer_camp_contact = array(
                    'post_type'   => 'page',
                    'post_title'  => $summer_camp_contact_title,
                    'post_content'=> $summer_camp_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug'   => 'contact-us'
                );

                $summer_camp_contact_id = wp_insert_post($summer_camp_contact);

                // Add Contact Us Page to Menu
                wp_update_nav_menu_item($summer_camp_menu_id, 0, array(
                    'menu-item-title'     => __('Contact Us', 'summer-camp'),
                    'menu-item-classes'  => 'contact-us',
                    'menu-item-url'      => home_url('/contact-us/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $summer_camp_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($summer_camp_bpmenulocation)) {
                    $summer_camp_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($summer_camp_locations)) {
                        $summer_camp_locations = array();
                    }
                    $summer_camp_locations[$summer_camp_bpmenulocation] = $summer_camp_menu_id;
                    set_theme_mod('nav_menu_locations', $summer_camp_locations);
                }

        }
            // Set the demo import completion flag
    		update_option('summer_camp_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.', 'summer-camp') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="run-import view-site" target="_blank">' . esc_html__('View Site', 'summer-camp') . '</a></span>';
            //end


            // Top Bar //
            set_theme_mod('summer_camp_hide_show_topbar_section',true);
            set_theme_mod( 'summer_camp_phone_number', '+1234567890' );
            set_theme_mod( 'summer_camp_email_address', 'xyz123@example.com' );
            set_theme_mod( 'summer_camp_search_icon', 'fas fa-search' );
            set_theme_mod( 'summer_camp_search_close_icon', 'fa fa-window-close' );
            set_theme_mod( 'summer_camp_cart_icon', 'fas fa-plus' );
            set_theme_mod( 'summer_camp_top_myaccount_icon_url', '#' );

            // slider section start //
            set_theme_mod('summer_camp_slider_hide_show',true);
            set_theme_mod( 'summer_camp_slider_image_overlay_color', '#000000' );
            set_theme_mod( 'summer_camp_events_small_heading', 'Welcome to the Church' );
            set_theme_mod( 'summer_camp_top_button_url', '#' );


            for($summer_camp_i=1;$summer_camp_i<=3;$summer_camp_i++){
                $summer_camp_slider_title = 'MAKE YOUR SUMMER UNFORGETABLE';
                $summer_camp_slider_content = 'Lorem ipsum is simply dummy text of the printing and typesetting industry.';
                   // Create post object
                $summer_camp_my_post = array(
                'post_title'    => wp_strip_all_tags( $summer_camp_slider_title ),
                'post_content'  => $summer_camp_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
                );

                // Insert the post into the database
                $summer_camp_post_id = wp_insert_post( $summer_camp_my_post );

                if ($summer_camp_post_id) {
                  // Set the theme mod for the slider page
                  set_theme_mod('summer_camp_slider_page' . $summer_camp_i, $summer_camp_post_id);

                   $summer_camp_image_url = get_template_directory_uri().'/assets/images/slider'.$summer_camp_i.'.png';

                 $summer_camp_image_id = media_sideload_image($summer_camp_image_url, $summer_camp_post_id, null, 'id');

                    if (!is_wp_error($summer_camp_image_id)) {
                         // Set the downloaded image as the post's featured image
                        set_post_thumbnail($summer_camp_post_id, $summer_camp_image_id);
                    }
                }
            }

            // classes
            set_theme_mod('summer_camp_section_title', 'Our Camp' );
            set_theme_mod('summer_camp_feature_courses_tagline_title', 'Hello Summer Camp' );
           
            set_theme_mod('summer_camp_claases_number', '3');
           // Defining the array of post titles
            $summer_camp_title_array = array(
                "Mini Camp",
                "Traditional Camp",
                "International Trips"
            );

            for ($summer_camp_post_index = 1; $summer_camp_post_index <= 3; $summer_camp_post_index++) {

                // Fetching title and content
                $summer_camp_title = $summer_camp_title_array[$summer_camp_post_index - 1];
                $summer_camp_content = 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s...';

                // Create post
                $summer_camp_my_post = array(
                    'post_title'   => wp_strip_all_tags($summer_camp_title),
                    'post_content' => $summer_camp_content,
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                );
                $summer_camp_post_id = wp_insert_post($summer_camp_my_post);

                if (!is_wp_error($summer_camp_post_id)) {

                    // Setting theme mod for the current post
                    set_theme_mod('summer_camp_services_category' . $summer_camp_post_index, $summer_camp_post_id);

                    // Setting theme mod values dynamically for each post
                    $summer_camp_i = $summer_camp_post_index; // Use the current index for the theme mod
                    set_theme_mod('summer_camp_user' . $summer_camp_i, '6 Years');
                    set_theme_mod('summer_camp_size' . $summer_camp_i, '20 Seats');
                    set_theme_mod('summer_camp_price' . $summer_camp_i, '20');
                    set_theme_mod('summer_camp_single_icon' . $summer_camp_i, 'fas fa-angle-right');

                    // Fetch image and upload
                    $summer_camp_image_url = get_template_directory_uri() . '/assets/images/camp' . $summer_camp_post_index . '.png';
                    
                    $summer_camp_image_data = wp_remote_get($summer_camp_image_url);
                    if (!is_wp_error($summer_camp_image_data)) {
                        $summer_camp_image_body = wp_remote_retrieve_body($summer_camp_image_data);
                        $summer_camp_upload_dir = wp_upload_dir();
                        $summer_camp_image_name = 'camp' . $summer_camp_post_index . '.png';
                        $summer_camp_uploaded_file = wp_upload_bits($summer_camp_image_name, null, $summer_camp_image_body);

                        if (!$summer_camp_uploaded_file['error']) {
                            // Attach image to post
                            $summer_camp_attachment = array(
                                'post_mime_type' => $summer_camp_uploaded_file['type'],
                                'post_title'     => sanitize_file_name($summer_camp_image_name),
                                'post_content'   => '',
                                'post_status'    => 'inherit',
                            );
                            $summer_camp_attach_id = wp_insert_attachment($summer_camp_attachment, $summer_camp_uploaded_file['file'], $summer_camp_post_id);
                            require_once(ABSPATH . 'wp-admin/includes/image.php');
                            $summer_camp_attach_data = wp_generate_attachment_metadata($summer_camp_attach_id, $summer_camp_uploaded_file['file']);
                            wp_update_attachment_metadata($summer_camp_attach_id, $summer_camp_attach_data);
                            set_post_thumbnail($summer_camp_post_id, $summer_camp_attach_id);
                        }
                    }
                }
            }

            
        }
    ?>

	
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=summer_camp_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('summer_camp_demo_import_completed')) : ?>
        <form method="post">
        <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 Summer Camp','summer-camp'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','summer-camp'); ?>" 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?")){
			    document.forms[0].submit();
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>
