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

        // If the demo import is completed, display the "View Site" button
        if ($charity_fundraiser_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'charity-fundraiser') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'charity-fundraiser') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'charity-fundraiser') . '</a></span>';
        }
		//POST and update the customizer and other related data of POLITICAL CAMPAIGN
        if (isset($_POST['submit'])) {

            // ------- Create Nav Menu --------
            $charity_fundraiser_menuname = 'Main Menus';
            $charity_fundraiser_bpmenulocation = 'primary';
            $charity_fundraiser_menu_exists = wp_get_nav_menu_object($charity_fundraiser_menuname);

            if (!$charity_fundraiser_menu_exists) {
                $charity_fundraiser_menu_id = wp_create_nav_menu($charity_fundraiser_menuname);

                // Create Home Page
                $charity_fundraiser_home_title = 'Home';
                $charity_fundraiser_home = array(
                    'post_type' => 'page',
                    'post_title' => $charity_fundraiser_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $charity_fundraiser_home_id = wp_insert_post($charity_fundraiser_home);
                // Assign Home Page Template
                add_post_meta($charity_fundraiser_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', $charity_fundraiser_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($charity_fundraiser_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'charity-fundraiser'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $charity_fundraiser_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $charity_fundraiser_pages_title = 'Pages';
                $charity_fundraiser_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.';
                $charity_fundraiser_pages = array(
                    'post_type' => 'page',
                    'post_title' => $charity_fundraiser_pages_title,
                    'post_content' => $charity_fundraiser_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $charity_fundraiser_pages_id = wp_insert_post($charity_fundraiser_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($charity_fundraiser_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'charity-fundraiser'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $charity_fundraiser_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $charity_fundraiser_about_title = 'About Us';
                $charity_fundraiser_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.';
                $charity_fundraiser_about = array(
                    'post_type' => 'page',
                    'post_title' => $charity_fundraiser_about_title,
                    'post_content' => $charity_fundraiser_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $charity_fundraiser_about_id = wp_insert_post($charity_fundraiser_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($charity_fundraiser_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'charity-fundraiser'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $charity_fundraiser_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                // Create Blog Page only if it doesn't exist already
                $charity_fundraiser_existing_blog_page = get_page_by_path('blog');
                if (!$charity_fundraiser_existing_blog_page) {
                    $charity_fundraiser_blog_id = wp_insert_post(array(
                    'post_type'    => 'page',
                    'post_title'   => __('Blog', 'charity-fundraiser'),
                    'post_content' => '',
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'blog'
                ));
                } else {
                    $charity_fundraiser_blog_id = $charity_fundraiser_existing_blog_page->ID;
                }
                // Assign as Posts Page
                update_option('page_for_posts', $charity_fundraiser_blog_id);
                update_option('show_on_front', 'page');
                //Add to Menu (only if not already added)
                $charity_fundraiser_menu_items = wp_get_nav_menu_items($charity_fundraiser_menu_id);
                $charity_fundraiser_blog_in_menu = false;
                if ($charity_fundraiser_menu_items) {
                    foreach ($charity_fundraiser_menu_items as $charity_fundraiser_item) {
                        if ((int) $charity_fundraiser_item->object_id === (int) $charity_fundraiser_blog_id) {
                            $charity_fundraiser_blog_in_menu = true;
                            break;
                        }
                    }
                }
                if (!$charity_fundraiser_blog_in_menu) {
                wp_update_nav_menu_item($charity_fundraiser_menu_id, 0, array(
                    'menu-item-title'       => __('Blog', 'charity-fundraiser'),
                    'menu-item-url'         => home_url('/blog/'),
                    'menu-item-status'      => 'publish',
                    'menu-item-object-id'   => $charity_fundraiser_blog_id,
                    'menu-item-object'      => 'page',
                    'menu-item-type'        => 'post_type',
                    'menu-item-classes'     => 'charity-fundraiser-blog-link'
                ));
                }
                // Set the menu location if it's not already set
                if (!has_nav_menu($charity_fundraiser_bpmenulocation)) {
                    $charity_fundraiser_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($charity_fundraiser_locations)) {
                        $charity_fundraiser_locations = array();
                    }
                    $charity_fundraiser_locations[$charity_fundraiser_bpmenulocation] = $charity_fundraiser_menu_id;
                    set_theme_mod('nav_menu_locations', $charity_fundraiser_locations);
                }
                
        }    

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

        //Header
        set_theme_mod('charity_fundraiser_email_text', 'EMAIL');
        set_theme_mod('charity_fundraiser_email', 'xyz123@example.com');
        set_theme_mod('charity_fundraiser_call_text', 'CALL NOW');
        set_theme_mod('charity_fundraiser_call_number', '123-456-7890');
        set_theme_mod('charity_fundraiser_donate_text', 'DONATE NOW');
        set_theme_mod('charity_fundraiser_donate_link', '#');

        //Social Icon
        set_theme_mod('charity_fundraiser_facebook', '#');
        set_theme_mod('charity_fundraiser_twitter', '#');
        set_theme_mod('charity_fundraiser_pintrest', '#');
        set_theme_mod('charity_fundraiser_insta', '#');
        set_theme_mod('charity_fundraiser_linkdin', '#');
        set_theme_mod('charity_fundraiser_youtube', '#');

        //Slider
        set_theme_mod('charity_fundraiser_slider_hide', true);
        set_theme_mod( 'charity_fundraiser_slider_button_label', 'READ MORE' );
        set_theme_mod( 'charity_fundraiser_slider_button_link', '#' );

        $charity_fundraiser_slider_titles = [
            'Together We Make a Difference',
            'Giving Hope, Changing Lives',
            'Empowering Change Through Generosity',
            'Uniting for a Better Tomorrow',
            ];     

        $charity_fundraiser_slider_contents = [
            'Every donation brings us closer to a brighter future.',
            'Your kindness can change lives and spread hope.',
            'Together, we can make a lasting impact.',
            'Help us create a better tomorrow with your support.',
            ];  


        for($charity_fundraiser_i=1;$charity_fundraiser_i<=4;$charity_fundraiser_i++){
            $charity_fundraiser_slider_title = $charity_fundraiser_slider_titles[$charity_fundraiser_i - 1];
            $charity_fundraiser_slider_content = $charity_fundraiser_slider_contents[$charity_fundraiser_i - 1];
            // Create post object
            $charity_fundraiser_my_post = array(
            'post_title'    => wp_strip_all_tags( $charity_fundraiser_slider_title ),
            'post_content'  => $charity_fundraiser_slider_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
            );

            // Insert the post into the database
            $charity_fundraiser_post_id = wp_insert_post( $charity_fundraiser_my_post );

            if ($charity_fundraiser_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('charity_fundraiser_slider_page' . $charity_fundraiser_i, $charity_fundraiser_post_id);

                $charity_fundraiser_image_url = get_template_directory_uri().'/images/slider'.$charity_fundraiser_i.'.png';

                $charity_fundraiser_image_id = media_sideload_image($charity_fundraiser_image_url, $charity_fundraiser_post_id, null, 'id');

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

        // Causes Section
        set_theme_mod('charity_fundraiser_causes_hide', true);
        set_theme_mod('charity_fundraiser_help_title', 'HELP FOR CAUSE');

        set_theme_mod( 'charity_fundraiser_image_post', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit' );
        $charity_fundraiser_service_titles = array(
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
        );
        $charity_fundraiser_service_image_url = get_template_directory_uri() . '/images/causes-left.png';

        for ($charity_fundraiser_i = 1; $charity_fundraiser_i <= 2; $charity_fundraiser_i++) {
            // Create post
            $charity_fundraiser_title = $charity_fundraiser_service_titles[$charity_fundraiser_i - 1];
            $charity_fundraiser_content = "Lorem Ipsum has been the industrys 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 Lorem Ipsum has been the industrys standard dummy text ever since the 1500 Lorem Ipsum has been the industrys standard dummy text ever since the 1500 when an unknown printer took";
            $charity_fundraiser_post_data = array(
                'post_title'   => wp_strip_all_tags($charity_fundraiser_title),
                'post_content' => wp_strip_all_tags($charity_fundraiser_content),
                'post_status'  => 'publish',
                'post_type'    => 'post',
            );
            $charity_fundraiser_post_id = wp_insert_post($charity_fundraiser_post_data);

            if (!is_wp_error($charity_fundraiser_post_id)) {
                // Add featured image
                $charity_fundraiser_image_id = media_sideload_image($charity_fundraiser_service_image_url, $charity_fundraiser_post_id, null, 'id');
                if (!is_wp_error($charity_fundraiser_image_id)) {
                    set_post_thumbnail($charity_fundraiser_post_id, $charity_fundraiser_image_id);
                }
                // Save post ID in theme mod
                set_theme_mod('charity_fundraiser_image_post' . $charity_fundraiser_i, $charity_fundraiser_post_id);
            }
        }

        // Insert the post category
        set_theme_mod('charity_fundraiser_causes_title', 'LOREM IPSUM IS SIMPLY');
        set_theme_mod('charity_fundraiser_help_category', 'category1');

        $charity_fundraiser_categories = array('category1', 'category2', 'category3');
        $charity_fundraiser_middle_titles = array(
            array("LOREM IPSUM IS SIMPLY 1", "LOREM IPSUM IS SIMPLY 2", "LOREM IPSUM IS SIMPLY 3"),
            array("LOREM IPSUM IS SIMPLY 4", "LOREM IPSUM IS SIMPLY 5", "LOREM IPSUM IS SIMPLY 6"),
            array("LOREM IPSUM IS SIMPLY 7", "LOREM IPSUM IS SIMPLY 8", "LOREM IPSUM IS SIMPLY 9")
        );
        $charity_fundraiser_content = "Lorem ipsum, dolor sit amet consectetur adipisicing elit.";
        
        foreach ($charity_fundraiser_categories as $charity_fundraiser_index => $charity_fundraiser_category_name) {
            // Create or retrieve category
            $charity_fundraiser_term = term_exists($charity_fundraiser_category_name, 'category');
            if (!$charity_fundraiser_term) {
                $charity_fundraiser_term = wp_insert_term($charity_fundraiser_category_name, 'category');
                if (is_wp_error($charity_fundraiser_term)) {
                    error_log('Error creating category: ' . $charity_fundraiser_term->get_error_message());
                    continue;
                }
            }
            $category_id = is_array($charity_fundraiser_term) ? $charity_fundraiser_term['term_id'] : $charity_fundraiser_term;
        
            // Create posts for the category
            foreach ($charity_fundraiser_middle_titles[$charity_fundraiser_index] as $charity_fundraiser_title_index => $charity_fundraiser_title) {
                $charity_fundraiser_post_data = array(
                    'post_title'   => wp_strip_all_tags($charity_fundraiser_title),
                    'post_content' => $charity_fundraiser_content,
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                );
                $charity_fundraiser_post_id = wp_insert_post($charity_fundraiser_post_data);
        
                if (!is_wp_error($charity_fundraiser_post_id)) {
                    // Assign category
                    wp_set_post_categories($charity_fundraiser_post_id, array($category_id));
        
                    // Set a unique image for each post
                    $charity_fundraiser_image_url = get_template_directory_uri() . '/images/causes-right' . ($charity_fundraiser_index + 1) . '.png';
                    
                    // Log the image URL to ensure it's correct
                    error_log('Image URL: ' . $charity_fundraiser_image_url);
        
                    // Attempt to sideload the image
                    $charity_fundraiser_image_id = media_sideload_image($charity_fundraiser_image_url, $charity_fundraiser_post_id, null, 'id');
                    
                    // Log the result of the sideload
                    error_log('Image ID: ' . print_r($charity_fundraiser_image_id, true));
                    
                    // If no error, set the post thumbnail
                    if (!is_wp_error($charity_fundraiser_image_id)) {
                        set_post_thumbnail($charity_fundraiser_post_id, $charity_fundraiser_image_id);
                    } else {
                        error_log('Error downloading the image: ' . $charity_fundraiser_image_id->get_error_message());
                    }
                } else {
                    error_log('Error creating the post: ' . $charity_fundraiser_post_id->get_error_message());
                }
            }
        }
        
        //Copyright Text
        set_theme_mod( 'charity_fundraiser_footer_text', 'By Themesglance' );

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=charity_fundraiser_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('charity_fundraiser_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.', 'charity-fundraiser' ); ?></p>
            <button id="import-button" type="submit" name="submit" class="import-btn run-import button-large">
                <?php esc_html_e('Run Importer', 'charity-fundraiser'); ?>
                    <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.getElementById('spinner').style.display = 'inline-block';
			}
		    else {
			    return false;
		    }
		}
	</script>
</div>

