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

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

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

        // ------- Create Main Menu --------
        $multipurpose_photography_menuname = 'Primary Menu';
        $multipurpose_photography_bpmenulocation = 'primary';
        $multipurpose_photography_menu_exists = wp_get_nav_menu_object( $multipurpose_photography_menuname );
    
        if ( !$multipurpose_photography_menu_exists ) {
            $multipurpose_photography_menu_id = wp_create_nav_menu( $multipurpose_photography_menuname );

            // Create Home Page
            $multipurpose_photography_home_title = 'Home';
            $multipurpose_photography_home = array(
                'post_type'    => 'page',
                'post_title'   => $multipurpose_photography_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $multipurpose_photography_home_id = wp_insert_post($multipurpose_photography_home);
            // Assign Home Page Template
            add_post_meta($multipurpose_photography_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', $multipurpose_photography_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($multipurpose_photography_menu_id, 0, array(
                'menu-item-title' => __('Home', 'multipurpose-photography'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $multipurpose_photography_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $multipurpose_photography_pages_title = 'Pages';
            $multipurpose_photography_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.';
            $multipurpose_photography_pages = array(
                'post_type'    => 'page',
                'post_title'   => $multipurpose_photography_pages_title,
                'post_content' => $multipurpose_photography_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $multipurpose_photography_pages_id = wp_insert_post($multipurpose_photography_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($multipurpose_photography_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'multipurpose-photography'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $multipurpose_photography_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $multipurpose_photography_about_title = 'About Us';
            $multipurpose_photography_about_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.';
            $multipurpose_photography_about = array(
                'post_type'    => 'page',
                'post_title'   => $multipurpose_photography_about_title,
                'post_content' => $multipurpose_photography_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $multipurpose_photography_about_id = wp_insert_post($multipurpose_photography_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($multipurpose_photography_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'multipurpose-photography'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $multipurpose_photography_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));
            // Create Blog Page only if it doesn't exist already
                $multipurpose_photography_existing_blog_page = get_page_by_path('blog');
                if (!$multipurpose_photography_existing_blog_page) {
                    $multipurpose_photography_blog_id = wp_insert_post(array(
                    'post_type'    => 'page',
                    'post_title'   => __('Blog', 'multipurpose-photography'),
                    'post_content' => '',
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'blog'
                ));
                } else {
                    $multipurpose_photography_blog_id = $multipurpose_photography_existing_blog_page->ID;
                }
                // Assign as Posts Page
                update_option('page_for_posts', $multipurpose_photography_blog_id);
                update_option('show_on_front', 'page');
                //Add to Menu (only if not already added)
                $multipurpose_photography_menu_items = wp_get_nav_menu_items($multipurpose_photography_menu_id);
                $multipurpose_photography_blog_in_menu = false;
                if ($multipurpose_photography_menu_items) {
                    foreach ($multipurpose_photography_menu_items as $multipurpose_photography_item) {
                        if ((int) $multipurpose_photography_item->object_id === (int) $multipurpose_photography_blog_id) {
                            $multipurpose_photography_blog_in_menu = true;
                            break;
                        }
                    }
                }
                if (!$multipurpose_photography_blog_in_menu) {
                wp_update_nav_menu_item($multipurpose_photography_menu_id, 0, array(
                    'menu-item-title'       => __('Blog', 'multipurpose-photography'),
                    'menu-item-url'         => home_url('/blog/'),
                    'menu-item-status'      => 'publish',
                    'menu-item-object-id'   => $multipurpose_photography_blog_id,
                    'menu-item-object'      => 'page',
                    'menu-item-type'        => 'post_type',
                    'menu-item-classes'     => 'multipurpose-photography-blog-link'
                ));
                }
            // Assign the menu to the primary location if not already set
            if ( ! has_nav_menu( $multipurpose_photography_bpmenulocation ) ) {
                $multipurpose_photography_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $multipurpose_photography_locations ) ) {
                    $multipurpose_photography_locations = array();
                }
                $multipurpose_photography_locations[ $multipurpose_photography_bpmenulocation ] = $multipurpose_photography_menu_id;
                set_theme_mod( 'nav_menu_locations', $multipurpose_photography_locations );
            }
        }

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

        //Topbar
        set_theme_mod( 'multipurpose_photography_search_placeholder', 'Search' );
        set_theme_mod( 'multipurpose_photography_call', '123 456 7890' );
        set_theme_mod( 'multipurpose_photography_facebook_url', '#' );
        set_theme_mod( 'multipurpose_photography_twitter_url', '#' );
        set_theme_mod( 'multipurpose_photography_insta_url', '#' );
        set_theme_mod( 'multipurpose_photography_youtube_url', '#' );
        set_theme_mod( 'multipurpose_photography_linkedin_url', '#' );
        set_theme_mod( 'multipurpose_photography_pinterest_url', '#' );
        set_theme_mod( 'multipurpose_photography_tumblr_url', '#' );
        set_theme_mod( 'multipurpose_photography_rss_url', '#' );

        //Header
        set_theme_mod( 'multipurpose_photography_header_button_text', 'Get A Quote' );
        set_theme_mod( 'multipurpose_photography_header_button_link', '#' );

        //Slider Section
        set_theme_mod( 'multipurpose_photography_slider_hide_show', true );
        set_theme_mod( 'multipurpose_photography_slider_button_label', 'READ MORE' );
        set_theme_mod( 'multipurpose_photography_slider_button_link', '#' );

        
            $multipurpose_photography_slider_titles = [
                "Capturing Moments, Creating Memories.",
                "Photography That Tells Your Story.",
                "Preserving Life’s Beautiful Perspectives.",
                "From Lens to Heart, Perfect Shots.",
            ];  

            $multipurpose_photography_slider_contents = [
                'Showcase the beauty of life’s fleeting moments with photography that resonates emotionally',
                'Every picture narrates a unique journey, reflecting personal and timeless stories',
                'Celebrate the world’s wonders through a lens that highlights elegance and charm',
                'Transforming every click into a masterpiece that touches hearts and inspires minds',
            ];  

        for($multipurpose_photography_i=1;$multipurpose_photography_i<=4;$multipurpose_photography_i++){
            $multipurpose_photography_slider_title = $multipurpose_photography_slider_titles[$multipurpose_photography_i - 1];
            $multipurpose_photography_slider_content = $multipurpose_photography_slider_contents[$multipurpose_photography_i - 1];
            // Create post object
            $multipurpose_photography_my_post = array(
            'post_title'    => wp_strip_all_tags( $multipurpose_photography_slider_title ),
            'post_content'  => $multipurpose_photography_slider_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
            );

            // Insert the post into the database
            $multipurpose_photography_post_id = wp_insert_post( $multipurpose_photography_my_post );

            if ($multipurpose_photography_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('multipurpose_photography_slidersettings_page' . $multipurpose_photography_i, $multipurpose_photography_post_id);

                $multipurpose_photography_image_url = get_template_directory_uri().'/images/slider'.$multipurpose_photography_i.'.png';

                $multipurpose_photography_image_id = media_sideload_image($multipurpose_photography_image_url, $multipurpose_photography_post_id, null, 'id');

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

        //Services Section
        
        set_theme_mod( 'multipurpose_photography_service_section_hide_show', true );
        set_theme_mod( 'multipurpose_photography_services_title', 'Services We Provide' );

        // Set the theme mod for the default category
        set_theme_mod('multipurpose_photography_services_category', 'postcategory3');

        // Define post category names and post titles
        $multipurpose_photography_category_names = ['postcategory1', 'postcategory2', 'postcategory3'];
        $multipurpose_photography_title_array = [
            ["Service Title 01", "Service Title 02", "Service Title 03", "Service Title 04"],
            ["Service Title 01", "Service Title 02", "Service Title 03", "Service Title 04"],
            ["Service Title 01", "Service Title 02", "Service Title 03", "Service Title 04"]
        ];

        $multipurpose_photography_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", 
                "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 ($multipurpose_photography_category_names as $multipurpose_photography_index => $multipurpose_photography_category_name) {
            // Create or retrieve the post category term ID
            $multipurpose_photography_category_term = term_exists($multipurpose_photography_category_name, 'category');
            if (!$multipurpose_photography_category_term) {
                $multipurpose_photography_category_term = wp_insert_term($multipurpose_photography_category_name, 'category');
            }

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

            $multipurpose_photography_category_id = isset($multipurpose_photography_category_term['term_id']) ? (int)$multipurpose_photography_category_term['term_id'] : $multipurpose_photography_category_term;

            for ($multipurpose_photography_i = 0; $multipurpose_photography_i < 4; $multipurpose_photography_i++) {
                // Ensure the required data exists
                if (empty($multipurpose_photography_title_array[$multipurpose_photography_index][$multipurpose_photography_i]) || 
                    empty($multipurpose_photography_content_array[$multipurpose_photography_index][$multipurpose_photography_i])) {
                    continue; // Skip if title or content is missing
                }

                // Create the post
                $multipurpose_photography_post_data = [
                    'post_title'    => wp_strip_all_tags($multipurpose_photography_title_array[$multipurpose_photography_index][$multipurpose_photography_i]),
                    'post_content'  => $multipurpose_photography_content_array[$multipurpose_photography_index][$multipurpose_photography_i],
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Default WordPress post type
                ];
                $multipurpose_photography_post_id = wp_insert_post($multipurpose_photography_post_data);

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

                // Assign category to the post
                wp_set_post_categories($multipurpose_photography_post_id, [$multipurpose_photography_category_id]);

                // Handle the featured image
                $multipurpose_photography_image_url = get_template_directory_uri() . '/images/service' . ($multipurpose_photography_i + 1) . '.png';
                $multipurpose_photography_image_id = media_sideload_image($multipurpose_photography_image_url, $multipurpose_photography_post_id, null, 'id');

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

                // Set the featured image
                set_post_thumbnail($multipurpose_photography_post_id, $multipurpose_photography_image_id);
            }
        }

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

        }
    ?>
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=multipurpose_photography_guide" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('multipurpose_photography_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.', 'multipurpose-photography' ); ?></p>
            <button id="import-button" type="submit" name="submit" class="import-btn run-import button-large">
                <?php esc_html_e('Run Importer', 'multipurpose-photography'); ?>
                    <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>
