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

        // If the demo import is completed, display the "View Site" button
        if ($event_management_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'event-management') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'event-management') . '</a></span>';
        }
	
        if (isset($_POST['submit'])) {
                    
            // ------- Create Nav Menu --------
            $event_management_menuname = 'Main Menus';
            $event_management_bpmenulocation = 'primary';
            $event_management_menu_exists = wp_get_nav_menu_object($event_management_menuname);

            if (!$event_management_menu_exists) {
                $event_management_menu_id = wp_create_nav_menu($event_management_menuname);

                // Create Home Page
                $event_management_home_title = 'Home';
                $event_management_home = array(
                    'post_type' => 'page',
                    'post_title' => $event_management_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $event_management_home_id = wp_insert_post($event_management_home);
                // Assign Home Page Template
                add_post_meta($event_management_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', $event_management_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($event_management_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'event-management'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $event_management_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $event_management_about_title = 'About Us';
                $event_management_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.';
                $event_management_about = array(
                    'post_type' => 'page',
                    'post_title' => $event_management_about_title,
                    'post_content' => $event_management_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $event_management_about_id = wp_insert_post($event_management_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($event_management_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'event-management'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $event_management_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                /* Create Services Pag* */
                $event_management_services_title = 'Services';
                $event_management_services_content = 'Discover our professional event planning and management services.
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

                $event_management_services = array(
                    'post_type'    => 'page',
                    'post_title'   => $event_management_services_title,
                    'post_content' => $event_management_services_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'services'
                );
                $event_management_services_id = wp_insert_post($event_management_services);

                // Add Services Page to Menu
                wp_update_nav_menu_item($event_management_menu_id, 0, array(
                    'menu-item-title'   => __('Services', 'event-management'),
                    'menu-item-classes' => 'services',
                    'menu-item-url'     => home_url('/services/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $event_management_services_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));


                /* Create Event Pag* */
                $event_management_event_title = 'Event';
                $event_management_event_content = 'Explore upcoming and past events organized by our team.';

                $event_management_event = array(
                    'post_type'    => 'page',
                    'post_title'   => $event_management_event_title,
                    'post_content' => $event_management_event_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'event'
                );
                $event_management_event_id = wp_insert_post($event_management_event);

                // Add Event Page to Menu
                wp_update_nav_menu_item($event_management_menu_id, 0, array(
                    'menu-item-title'   => __('Event', 'event-management'),
                    'menu-item-classes' => 'event',
                    'menu-item-url'     => home_url('/event/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $event_management_event_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));


                /* Create Pricing Pag* */
                $event_management_pricing_title = 'Pricing';
                $event_management_pricing_content = 'View our transparent pricing plans for event management services.';

                $event_management_pricing = array(
                    'post_type'    => 'page',
                    'post_title'   => $event_management_pricing_title,
                    'post_content' => $event_management_pricing_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'pricing'
                );
                $event_management_pricing_id = wp_insert_post($event_management_pricing);

                // Add Pricing Page to Menu
                wp_update_nav_menu_item($event_management_menu_id, 0, array(
                    'menu-item-title'   => __('Pricing', 'event-management'),
                    'menu-item-classes' => 'pricing',
                    'menu-item-url'     => home_url('/pricing/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $event_management_pricing_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                // Create Blog Page 
                $event_management_blog_page_title = 'Blog';
                $event_management_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($event_management_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$event_management_blog_page_query->have_posts()) {
                    $event_management_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $event_management_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $event_management_blog_page_id = wp_insert_post($event_management_blog_page);
                    update_option('page_for_posts', $event_management_blog_page_id);
                    wp_update_nav_menu_item($event_management_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'event-management'),
                    'menu-item-url'        => get_permalink($event_management_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $event_management_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $event_management_contact_title = 'Contact Us';
                $event_management_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.';
                $event_management_contact = array(
                    'post_type' => 'page',
                    'post_title' => $event_management_contact_title,
                    'post_content' => $event_management_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $event_management_contact_id = wp_insert_post($event_management_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($event_management_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'event-management'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $event_management_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($event_management_bpmenulocation)) {
                    $event_management_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($event_management_locations)) {
                        $event_management_locations = array();
                    }
                    $event_management_locations[$event_management_bpmenulocation] = $event_management_menu_id;
                    set_theme_mod('nav_menu_locations', $event_management_locations);
                }
            }

            //Topbar
            set_theme_mod('event_management_email_address', 'xyz123@example.com');
            set_theme_mod('event_management_call', '+1 (123) 456-7890');
            set_theme_mod('event_management_location', 'Glassford Street Glasgow New York, USA');
            set_theme_mod('event_management_request_text', 'Request A Quote');
            set_theme_mod('event_management_request_link', '#');

            //Social Media
            set_theme_mod('event_management_facebook_link', 'https://www.facebook.com');
            set_theme_mod('event_management_twitter_link', 'https://www.twitter.com');
            set_theme_mod('event_management_linkdin_link', 'https://www.linkedin.com');
            set_theme_mod('event_management_instagram_link', 'https://www.instagram.com');
            set_theme_mod('event_management_pintrest_link', 'https://www.pintrest.com');

            // Slider
            set_theme_mod('event_management_slider_hide', true);
            set_theme_mod('event_management_slider_button_text', 'Read More');

            $event_management_slider_titles = array(
                'Life is short, so let\'s celebrate in style!',
                'Make memories that last, it\'s party time!',
                'Turn moments into magic, the party starts now!',
                'Celebrate life, one event at a time!'
            );

            for ($event_management_i = 0; $event_management_i < 4; $event_management_i++) {

                $event_management_slider_content = 'To get the best of your Event you just need to let us know, and we do the rest... ';
                $event_management_slider_title = $event_management_slider_titles[$event_management_i];

                // Create post object
                $event_management_my_post = array(
                    'post_title'    => wp_strip_all_tags($event_management_slider_title),
                    'post_content'  => $event_management_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );

                // Insert the post into the database
                $event_management_post_id = wp_insert_post($event_management_my_post);

                if ($event_management_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('event_management_slider_setting' . ($event_management_i + 1), $event_management_post_id);

                    $event_management_image_url = get_template_directory_uri() . '/images/slider' . ($event_management_i + 1) . '.png';

                    // Handle image upload and set as post thumbnail
                    $event_management_image_id = media_sideload_image($event_management_image_url, $event_management_post_id, null, 'id');

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

            //Events Section
            set_theme_mod('event_management_ourevents_enable', true);
            set_theme_mod('event_management_events_small_title', 'Make Your Parties More Memorable');
            set_theme_mod('event_management_events_section_title', 'Our Successful Recent Events');

            set_theme_mod('event_management_ourevents', 'postcategory3' );
            // Define post category names and post titles
            $event_management_category_names = array('postcategory1', 'postcategory2', 'postcategory3');
            $event_management_title_array = array(
                array("The Wedding Couple", "New Year Party", "The Party Folk"),
                array("The Wedding Couple", "New Year Party", "The Party Folk"),
                array("The Wedding Couple", "New Year Party", "The Party Folk")
            );
            
            $event_management_content_array = array(
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry","Lorem is simply dummy text of printing and typesetting industry"),
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry"),
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry")
            );
            
            foreach ($event_management_category_names as $event_management_index => $event_management_category_name) {
                // Create or retrieve the post category term ID
                $event_management_term = term_exists($event_management_category_name, 'category');
                if ($event_management_term === 0 || $event_management_term === null) {
                    // If the term does not exist, create it
                    $event_management_term = wp_insert_term($event_management_category_name, 'category');
                }
                if (is_wp_error($event_management_term)) {
                    error_log('Error creating category: ' . $event_management_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }
            
                for ($event_management_i = 0; $event_management_i < 3; $event_management_i++) {
                    // Create post content
                    $event_management_title = $event_management_title_array[$event_management_index][$event_management_i];
                    $event_management_content = $event_management_content_array[$event_management_index][$event_management_i];
            
                    // Create post post object
                    $event_management_my_post = array(
                        'post_title'    => wp_strip_all_tags($event_management_title),
                        'post_content'  => $event_management_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                        'post_excerpt'  => substr($event_management_content, 0, 100) . '...', // Add an excerpt
                    );
            
                    // Insert the post into the database
                    $event_management_post_id = wp_insert_post($event_management_my_post);
            
                    if (is_wp_error($event_management_post_id)) {
                        error_log('Error creating post: ' . $event_management_post_id->get_error_message());
                        continue; // Skip to the next post if creation fails
                    }
            
                    // Assign the category to the post
                    wp_set_post_categories($event_management_post_id, array((int)$event_management_term['term_id']));
            
                    // Handle the featured image using media_sideload_image
                    $event_management_image_url = get_template_directory_uri() . '/images/event' . ($event_management_i + 1) . '.png';
                    $event_management_image_id = media_sideload_image($event_management_image_url, $event_management_post_id, null, 'id');
            
                    if (is_wp_error($event_management_image_id)) {
                        error_log('Error downloading image: ' . $event_management_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }
            
                    // Assign featured image to post
                    set_post_thumbnail($event_management_post_id, $event_management_image_id);
            
                }
            }

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

        }
    ?>

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