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

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

            // ------- Create Nav Menu --------
            $election_campaign_menuname = 'Main Menus';
            $election_campaign_bpmenulocation = 'primary';
            $election_campaign_menu_exists = wp_get_nav_menu_object($election_campaign_menuname);

            if (!$election_campaign_menu_exists) {
                $election_campaign_menu_id = wp_create_nav_menu($election_campaign_menuname);

                // Create Home Page
                $election_campaign_home_title = 'Home';
                $election_campaign_home = array(
                    'post_type' => 'page',
                    'post_title' => $election_campaign_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $election_campaign_home_id = wp_insert_post($election_campaign_home);
                // Assign Home Page Template
                add_post_meta($election_campaign_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', $election_campaign_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($election_campaign_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'election-campaign'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $election_campaign_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $election_campaign_about_title = 'About Us';
                $election_campaign_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.';
                $election_campaign_about = array(
                    'post_type' => 'page',
                    'post_title' => $election_campaign_about_title,
                    'post_content' => $election_campaign_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $election_campaign_about_id = wp_insert_post($election_campaign_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($election_campaign_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'election-campaign'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $election_campaign_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $election_campaign_blog_page_title = 'Blog';
                $election_campaign_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($election_campaign_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$election_campaign_blog_page_query->have_posts()) {
                    $election_campaign_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $election_campaign_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $election_campaign_blog_page_id = wp_insert_post($election_campaign_blog_page);
                    update_option('page_for_posts', $election_campaign_blog_page_id);
                    wp_update_nav_menu_item($election_campaign_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'election-campaign'),
                    'menu-item-url'        => get_permalink($election_campaign_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $election_campaign_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                   ));
                }

                // Create Pages Page with Dummy Content
                $election_campaign_contact_title = 'Contact Us';
                $election_campaign_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.';
                $election_campaign_contact = array(
                    'post_type' => 'page',
                    'post_title' => $election_campaign_contact_title,
                    'post_content' => $election_campaign_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $election_campaign_contact_id = wp_insert_post($election_campaign_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($election_campaign_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'election-campaign'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $election_campaign_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($election_campaign_bpmenulocation)) {
                    $election_campaign_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($election_campaign_locations)) {
                        $election_campaign_locations = array();
                    }
                    $election_campaign_locations[$election_campaign_bpmenulocation] = $election_campaign_menu_id;
                    set_theme_mod('nav_menu_locations', $election_campaign_locations);
                }
            }

            //Header
            set_theme_mod('election_campaign_location', 'Lorem ipsum dolor sit amet');
            set_theme_mod('election_campaign_enter_time', 'Mon to Fri 10am to 06pm');
            set_theme_mod('election_campaign_topbar_btn_text', 'FREE A REQUEST');
            set_theme_mod('election_campaign_topbar_btn_url', '#');

            //Slider
            set_theme_mod('election_campaign_slider_hide', true);
            set_theme_mod('election_campaign_slider_button_text', 'Read More');
            set_theme_mod('election_campaign_slider_button_link', '#');

            $election_campaign_slider_titles = array(
                'We\'re Ideal For All The People\'s Brighter Future',
                'Building A Stronger Community Together',
                'Your Voice, Our Mission for Change',
            );
            
            for ($election_campaign_i = 0; $election_campaign_i < count($election_campaign_slider_titles); $election_campaign_i++) {
                $election_campaign_slider_title = $election_campaign_slider_titles[$election_campaign_i];
                $election_campaign_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ';
                
                // Create post object
                $election_campaign_my_post = array(
                    'post_title'    => wp_strip_all_tags($election_campaign_slider_title),
                    'post_content'  => $election_campaign_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );
            
                // Insert the post into the database
                $election_campaign_post_id = wp_insert_post($election_campaign_my_post);
            
                if ($election_campaign_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('election_campaign_slider_page' . ($election_campaign_i + 1), $election_campaign_post_id);
            
                    $election_campaign_image_url = get_template_directory_uri() . '/images/slider' . ($election_campaign_i + 1) . '.png';
            
                    $election_campaign_image_id = media_sideload_image($election_campaign_image_url, $election_campaign_post_id, null, 'id');
            
                    if (!is_wp_error($election_campaign_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($election_campaign_post_id, $election_campaign_image_id);
                    }
                }
            }
            
            // Service Section
            set_theme_mod('election_campaign_show_portfolio_section', true);
            set_theme_mod('election_campaign_section_text', 'MISSION AND VISION');
            set_theme_mod('election_campaign_section_title', 'How we can build a better country together!');
            set_theme_mod('election_campaign_practice_button_text', 'Read More');
            set_theme_mod('election_campaign_our_services', 'postcategory1');

            // Define post category names and post titles
            $election_campaign_category_names = array('postcategory1', 'postcategory2', 'postcategory3', 'postcategory4');
            $election_campaign_title_array = array(
                array("Economic Inequality", "Climate Change", "Healthcare Access"),
                array("Economic Inequality", "Climate Change", "Healthcare Access"),
                array("Economic Inequality", "Climate Change", "Healthcare Access"),
                array("Economic Inequality", "Climate Change", "Healthcare Access")
            );

            $election_campaign_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."
                ),
                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."
                ),
                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."
                ),
                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."
                )
            );

            foreach ($election_campaign_category_names as $election_campaign_index => $election_campaign_category_name) {
                // Create or retrieve the post category term ID
                $election_campaign_term = term_exists($election_campaign_category_name, 'category');
                if (!$election_campaign_term || is_wp_error($election_campaign_term)) {
                    $election_campaign_term = wp_insert_term($election_campaign_category_name, 'category');
                    if (is_wp_error($election_campaign_term)) {
                        error_log('Error creating category: ' . $election_campaign_term->get_error_message());
                        continue; // Skip to the next iteration if category creation fails
                    }
                }
            
                $election_campaign_term_id = is_array($election_campaign_term) ? $election_campaign_term['term_id'] : $election_campaign_term;
            
                for ($election_campaign_i = 0; $election_campaign_i < 3; $election_campaign_i++) {
                    // Create post content
                    $election_campaign_title = $election_campaign_title_array[$election_campaign_index][$election_campaign_i];
                    $election_campaign_content = $election_campaign_content_array[$election_campaign_index][$election_campaign_i];
            
                    // Create post object
                    $election_campaign_my_post = array(
                        'post_title'    => wp_strip_all_tags($election_campaign_title),
                        'post_content'  => $election_campaign_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );
            
                    // Insert the post into the database
                    $election_campaign_post_id = wp_insert_post($election_campaign_my_post);
            
                    if (is_wp_error($election_campaign_post_id)) {
                        error_log('Error creating post: ' . $election_campaign_post_id->get_error_message());
                        continue; // Skip to the next post if creation fails
                    }
            
                    // Assign the category to the post
                    wp_set_post_categories($election_campaign_post_id, array((int)$election_campaign_term_id));
            
                    // Handle the featured image using media_sideload_image
                    $election_campaign_image_url = get_template_directory_uri() . '/images/service' . ($election_campaign_i + 1) . '.png';
                    $election_campaign_image_id = media_sideload_image($election_campaign_image_url, $election_campaign_post_id, null, 'id');
            
                    if (is_wp_error($election_campaign_image_id)) {
                        error_log('Error downloading image: ' . $election_campaign_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }
            
                    // Assign featured image to post
                    set_post_thumbnail($election_campaign_post_id, $election_campaign_image_id);
            
                    // Add post meta for the icon with specific icons for each post
                    $election_campaign_icon_classes = array("fas fa-chart-line", "fas fa-thermometer-half", "fas fa-wheelchair");
                    $election_campaign_selected_icon = isset($election_campaign_icon_classes[$election_campaign_i]) ? $election_campaign_icon_classes[$election_campaign_i] : $election_campaign_icon_classes[0];
                    update_post_meta($election_campaign_post_id, 'election_campaign_icon', $election_campaign_selected_icon);
                }
            }
            
            // Set the demo import completion flag
    		update_option('election_campaign_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.', 'election-campaign') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'election-campaign') . '</a></span>';
            //end
   
            //Copyright Text
            set_theme_mod( 'election_campaign_footer_copy', 'By Buywptemplate' );

        }
    ?>

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