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

        // If the demo import is completed, display the "View Site" button
        if ($campus_education_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'campus-education') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VSIT SITE', 'campus-education') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'campus-education') . '</a></span>';
        }

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

            // ------- Create Nav Menu --------
            $campus_education_menuname = 'Main Menus';
            $campus_education_bpmenulocation = 'primary';
            $campus_education_menu_exists = wp_get_nav_menu_object($campus_education_menuname);

            if (!$campus_education_menu_exists) {
                $campus_education_menu_id = wp_create_nav_menu($campus_education_menuname);

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

                // Create Pages Page with Dummy Content
                $campus_education_pages_title = 'Pages';
                $campus_education_pages_content = '
                <p>Explore all the pages we have on our website. Find information about our services, company, and more.</p>

                 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.';
                $campus_education_pages = array(
                    'post_type' => 'page',
                    'post_title' => $campus_education_pages_title,
                    'post_content' => $campus_education_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $campus_education_pages_id = wp_insert_post($campus_education_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($campus_education_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'campus-education'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $campus_education_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $campus_education_about_title = 'About Us';
                $campus_education_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.';
                $campus_education_about = array(
                    'post_type' => 'page',
                    'post_title' => $campus_education_about_title,
                    'post_content' => $campus_education_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $campus_education_about_id = wp_insert_post($campus_education_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($campus_education_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'campus-education'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $campus_education_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                // Create Blog Page only if it doesn't exist already
                $campus_education_existing_blog_page = get_page_by_path('blog');
                if (!$campus_education_existing_blog_page) {
                    $campus_education_blog_id = wp_insert_post(array(
                    'post_type'    => 'page',
                    'post_title'   => __('Blog', 'campus-education'),
                    'post_content' => '',
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'blog'
                ));
                } else {
                    $campus_education_blog_id = $campus_education_existing_blog_page->ID;
                }
                // Assign as Posts Page
                update_option('page_for_posts', $campus_education_blog_id);
                update_option('show_on_front', 'page');
                //Add to Menu (only if not already added)
                $campus_education_menu_items = wp_get_nav_menu_items($campus_education_menu_id);
                $campus_education_blog_in_menu = false;
                if ($campus_education_menu_items) {
                    foreach ($campus_education_menu_items as $campus_education_item) {
                        if ((int) $campus_education_item->object_id === (int) $campus_education_blog_id) {
                            $campus_education_blog_in_menu = true;
                            break;
                        }
                    }
                }
                if (!$campus_education_blog_in_menu) {
                wp_update_nav_menu_item($campus_education_menu_id, 0, array(
                    'menu-item-title'       => __('Blog', 'campus-education'),
                    'menu-item-url'         => home_url('/blog/'),
                    'menu-item-status'      => 'publish',
                    'menu-item-object-id'   => $campus_education_blog_id,
                    'menu-item-object'      => 'page',
                    'menu-item-type'        => 'post_type',
                    'menu-item-classes'     => 'campus-education-blog-link'
                ));
                }
                // Set the menu location if it's not already set
                if (!has_nav_menu($campus_education_bpmenulocation)) {
                    $campus_education_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($campus_education_locations)) {
                        $campus_education_locations = array();
                    }
                    $campus_education_locations[$campus_education_bpmenulocation] = $campus_education_menu_id;
                    set_theme_mod('nav_menu_locations', $campus_education_locations);
                }
                
        }    

        // Set the demo import completion flag
        update_option('campus_education_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.', 'campus-education') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'campus-education') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'campus-education') . '</a></span>';
        //end 

        // Topbar settings  
        set_theme_mod('campus_education_top_header', true);
        set_theme_mod('campus_education_email_text', 'Call Us Now');
        set_theme_mod('campus_education_email', '123-456-7890');
        set_theme_mod('campus_education_call_text', 'Send Us Mail');
        set_theme_mod('campus_education_call_number', 'xyz123@example.com');
        set_theme_mod('campus_education_button_text', 'APPLY ONLINE');
        set_theme_mod('campus_education_button_link', '#');

        //Social Icon
        set_theme_mod('campus_education_facebook', '#');
        set_theme_mod('campus_education_twitter', '#');
        set_theme_mod('campus_education_pintrest', '#');
        set_theme_mod('campus_education_insta', '#');
        set_theme_mod('campus_education_linkdin', '#');
        set_theme_mod('campus_education_youtube', '#');

        //Slider
        set_theme_mod('campus_education_slider_hide', true);
        set_theme_mod( 'campus_education_slider_button_label', 'GET STARTED NOW' );
        set_theme_mod( 'campus_education_slider_button_link', '#' );

            $campus_education_slider_titles = [
                   'Empowering Minds, Shaping Futures',
                    'Where Learning Meets Opportunity',
                    'Building Knowledge, Building Success',
                    'Unlock Your Potential with Quality Education',
            ];     

            $campus_education_slider_contents = [
                    'Foster growth and development with an educational experience designed to inspire students and prepare them for a successful future.',
                    'Discover a place where education and real-world opportunities come together to unlock a world of potential for every student.',
                    'Provide students with the tools they need to thrive academically and professionally, ensuring their success in every step of life.',
                    'Give yourself the advantage of a world-class education that nurtures talent, sharpens skills, and creates limitless possibilities.'
            ];  


        for($campus_education_i=1;$campus_education_i<=4;$campus_education_i++){
            $campus_education_slider_title = $campus_education_slider_titles[$campus_education_i - 1];
            $campus_education_slider_content = $campus_education_slider_contents[$campus_education_i - 1];
            // Create post object
            $campus_education_my_post = array(
            'post_title'    => wp_strip_all_tags( $campus_education_slider_title ),
            'post_content'  => $campus_education_slider_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
            );

            // Insert the post into the database
            $campus_education_post_id = wp_insert_post( $campus_education_my_post );

            if ($campus_education_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('campus_education_slider_page' . $campus_education_i, $campus_education_post_id);

                $campus_education_image_url = get_template_directory_uri().'/images/slider'.$campus_education_i.'.png';

                $campus_education_image_id = media_sideload_image($campus_education_image_url, $campus_education_post_id, null, 'id');

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

        //Campus Section
        set_theme_mod( 'campus_education_welcome_hide', true );
        set_theme_mod( 'campus_education_campus_title', 'Welcome To Our Campus' );
        set_theme_mod( 'campus_education_campus_text', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.' );

        // Set the theme mod for the default category
        set_theme_mod('campus_education_campus_category', 'postcategory1');

        // Define post category names and post titles
        $campus_education_category_names = ['postcategory1', 'postcategory2', 'postcategory3'];
        $campus_education_title_array = [
            ["Our Campus Title 1", "Our Campus Title 2", "Our Campus Title 3"],
            ["Our Campus Title 1", "Our Campus Title 2", "Our Campus Title 3"],
            ["Our Campus Title 1", "Our Campus Title 2", "Our Campus Title 3"]
        ];

        $campus_education_content_array = [
            [
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            ],
            [
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            ],
            [
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            ]
        ];

        foreach ($campus_education_category_names as $campus_education_index => $campus_education_category_name) {
            // Create or retrieve the post category term ID
            $campus_education_category_term = term_exists($campus_education_category_name, 'category');
            if (!$campus_education_category_term) {
                $campus_education_category_term = wp_insert_term($campus_education_category_name, 'category');
            }

            if (is_wp_error($campus_education_category_term)) {
                error_log('Error creating category: ' . $campus_education_category_term->get_error_message());
                continue; // Skip if category creation fails
            }

            $campus_education_category_id = isset($campus_education_category_term['term_id']) ? (int)$campus_education_category_term['term_id'] : $campus_education_category_term;

            for ($campus_education_i = 0; $campus_education_i < 4; $campus_education_i++) {
                // Ensure the required data exists
                if (empty($campus_education_title_array[$campus_education_index][$campus_education_i]) || 
                    empty($campus_education_content_array[$campus_education_index][$campus_education_i])) {
                    continue; // Skip if title or content is missing
                }

                // Create the post
                $campus_education_post_data = [
                    'post_title'    => wp_strip_all_tags($campus_education_title_array[$campus_education_index][$campus_education_i]),
                    'post_content'  => $campus_education_content_array[$campus_education_index][$campus_education_i],
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Default WordPress post type
                ];
                $campus_education_post_id = wp_insert_post($campus_education_post_data);

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

                // Assign category to the post
                wp_set_post_categories($campus_education_post_id, [$campus_education_category_id]);

                // Handle the featured image
                $campus_education_image_url = get_template_directory_uri() . '/images/campus' . ($campus_education_i + 1) . '.png';
                $campus_education_image_id = media_sideload_image($campus_education_image_url, $campus_education_post_id, null, 'id');

                if (is_wp_error($campus_education_image_id)) {
                    error_log('Error downloading image: ' . $campus_education_image_id->get_error_message());
                    continue; // Skip if image download fails
                }

                // Set the featured image
                set_post_thumbnail($campus_education_post_id, $campus_education_image_id);
            }
        }

        //Copyright Text
        set_theme_mod( 'campus_education_footer_text', 'By Themesglance' );

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=campus_education_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('campus_education_demo_import_completed')) : ?>
        <div class="demo-btn">
            <p><?php echo  esc_html_e( 'You are about to use the most easy to use and flexible WordPress theme.', 'campus-education' ); ?></p>
            <button id="import-button" type="submit" name="submit" class="import-btn run-import button-large">
                <?php esc_html_e('Run Importer', 'campus-education'); ?>
                    <span id="spinner" style="display: none;">
                        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/spinner.gif" alt="Loading..." style="width:34px; height:34px; margin-left:10px;vertical-align: middle;" />
                    </span>
            </button>
        </div>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
			 if(confirm("Do you really want to import the Demo Theme Content?")){
			    document.forms[0].submit();
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>

