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

    // If the demo import is completed, display the "View Site" button
        if ($bb_wedding_bliss_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'bb-wedding-bliss') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '"  class= "run-import view-site" target="_blank">' . esc_html__('VIEW SITE', 'bb-wedding-bliss') . '</a></span>';
        }

    if (isset($_POST['submit'])) {

    // --- Menu ---
    $bb_wedding_bliss_primary_menu_name = 'Main Menu';
    $bb_wedding_bliss_primary_menu_location = 'primary';
    $bb_wedding_bliss_primary_menu_exists = wp_get_nav_menu_object($bb_wedding_bliss_primary_menu_name);

    if (!$bb_wedding_bliss_primary_menu_exists) {
        // Create the left menu
        $bb_wedding_bliss_primary_menu_id = wp_create_nav_menu($bb_wedding_bliss_primary_menu_name);

        // Create and assign the Home page
        $bb_wedding_bliss_home_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Home',
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'home'
        ));
        // Assign template and set as front page
        add_post_meta($bb_wedding_bliss_home_page_id, '_wp_page_template', 'page-template/custom-front-page.php');
        update_option('page_on_front', $bb_wedding_bliss_home_page_id);
        update_option('show_on_front', 'page');

        // Add Home page to the left menu
        wp_update_nav_menu_item($bb_wedding_bliss_primary_menu_id, 0, array(
            'menu-item-title'     => __('Home', 'bb-wedding-bliss'),
            'menu-item-classes'   => 'home',
            'menu-item-url'       => home_url('/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $bb_wedding_bliss_home_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Join Us page
        $bb_wedding_bliss_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Join Us',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Join Us page to the left menu
        wp_update_nav_menu_item($bb_wedding_bliss_primary_menu_id, 0, array(
            'menu-item-title'     => __('Join Us', 'bb-wedding-bliss'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $bb_wedding_bliss_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

         // Create and assign the Story page
        $bb_wedding_bliss_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Story',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Story page to the left menu
        wp_update_nav_menu_item($bb_wedding_bliss_primary_menu_id, 0, array(
            'menu-item-title'     => __('Story', 'bb-wedding-bliss'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $bb_wedding_bliss_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Gallery page
        $bb_wedding_bliss_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Gallery',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Gallery page to the left menu
        wp_update_nav_menu_item($bb_wedding_bliss_primary_menu_id, 0, array(
            'menu-item-title'     => __('Gallery', 'bb-wedding-bliss'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $bb_wedding_bliss_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

         // Create and assign the Bridesmaid page
        $bb_wedding_bliss_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Bridesmaid',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Bridesmaid page to the left menu
        wp_update_nav_menu_item($bb_wedding_bliss_primary_menu_id, 0, array(
            'menu-item-title'     => __('Bridesmaid', 'bb-wedding-bliss'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $bb_wedding_bliss_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // CREATE BLOG PAGE 
        $bb_wedding_bliss_blog_page = get_page_by_path('blog');
            if (!$bb_wedding_bliss_blog_page) {
            $bb_wedding_bliss_blog_page_id = wp_insert_post(array(
                'post_type'   => 'page',
                'post_title'  => 'Blog',
                'post_status' => 'publish',
                'post_name'   => 'blog',
            ));

            } else {
                    $bb_wedding_bliss_blog_page_id = $bb_wedding_bliss_blog_page->ID;
                }
                update_option('page_for_posts', $bb_wedding_bliss_blog_page_id);

                wp_update_nav_menu_item($bb_wedding_bliss_primary_menu_id, 0, array(
                    'menu-item-title'     => __('Blog', 'bb-wedding-bliss'),
                    'menu-item-object-id' => $bb_wedding_bliss_blog_page_id,
                    'menu-item-object'    => 'page',
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',    
        ));

         // Create and assign the More page
        $bb_wedding_bliss_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'More',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add More page to the left menu
        wp_update_nav_menu_item($bb_wedding_bliss_primary_menu_id, 0, array(
            'menu-item-title'     => __('More', 'bb-wedding-bliss'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $bb_wedding_bliss_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Assign left menu to its location
        $bb_wedding_bliss_locations = get_theme_mod('nav_menu_locations', array());
        $bb_wedding_bliss_locations[$bb_wedding_bliss_primary_menu_location] = $bb_wedding_bliss_primary_menu_id;
        set_theme_mod('nav_menu_locations', $bb_wedding_bliss_locations);
    }  

    // Set the demo import completion flag
    		update_option('bb_wedding_bliss_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.', 'bb-wedding-bliss') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="run-import site-btn" target="_blank">' . esc_html__('VIEW SITE', 'bb-wedding-bliss') . '</a></span>';

    //end

    // Social Icons
    set_theme_mod( 'bb_wedding_bliss_youtube_url', '#' );
    set_theme_mod( 'bb_wedding_bliss_facebook_url', '#' );
    set_theme_mod( 'bb_wedding_bliss_twitter_url', '#' );
    set_theme_mod( 'bb_wedding_bliss_rss_url', '#' );
    set_theme_mod( 'bb_wedding_bliss_insta_url', '#' );
    set_theme_mod( 'bb_wedding_bliss_pint_url', '#' );

    // Slider
    for($bb_wedding_bliss_i=1;$bb_wedding_bliss_i<=4;$bb_wedding_bliss_i++){
       $bb_wedding_bliss_slider_title = 'MICCHELL & JOHN';
       $bb_wedding_bliss_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Quis ipsum suspendisse ultrices gravida.';
          // Create post object
       $bb_wedding_bliss_my_post = array(
       'post_title'    => wp_strip_all_tags( $bb_wedding_bliss_slider_title ),
       'post_content'  => $bb_wedding_bliss_slider_content,
       'post_status'   => 'publish',
       'post_type'     => 'page',
       );

       // Insert the post into the database
       $bb_wedding_bliss_post_id = wp_insert_post( $bb_wedding_bliss_my_post );

       if ($bb_wedding_bliss_post_id) {
         // Set the theme mod for the slider page
         set_theme_mod('bb_wedding_bliss_slidersettings_page' . $bb_wedding_bliss_i, $bb_wedding_bliss_post_id);

          $bb_wedding_bliss_image_url = get_template_directory_uri().'/images/slider'.$bb_wedding_bliss_i.'.png';

        $bb_wedding_bliss_image_id = media_sideload_image($bb_wedding_bliss_image_url, $bb_wedding_bliss_post_id, null, 'id');

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

    // Love Story
    $bb_wedding_bliss_post_title = 'Love story';
    $bb_wedding_bliss_post_content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.';

    // Create post object
    $bb_wedding_bliss_my_post = array(
    'post_title'    => wp_strip_all_tags($bb_wedding_bliss_post_title),
    'post_content'  => $bb_wedding_bliss_post_content,
    'post_status'   => 'publish',
    'post_type'     => 'post',
    );

    // Insert the post into the database
    $bb_wedding_bliss_post_id = wp_insert_post($bb_wedding_bliss_my_post);

    // Check if the post was successfully created
    if (!is_wp_error($bb_wedding_bliss_post_id)) {
    // Set the theme mod for the post ID
    set_theme_mod('bb_wedding_bliss_love_post_setting', $bb_wedding_bliss_post_id);

    // Fetch the image URL
    $bb_wedding_bliss_image_url = get_template_directory_uri() . '/images/post-img.png';

    // Media sideload the image
    $bb_wedding_bliss_image_id = media_sideload_image($bb_wedding_bliss_image_url, $bb_wedding_bliss_post_id, null, 'id');

    // Check if the image was successfully uploaded
    if (!is_wp_error($bb_wedding_bliss_image_id)) {
        // Set the downloaded image as the post's featured image
        set_post_thumbnail($bb_wedding_bliss_post_id, $bb_wedding_bliss_image_id);
    } else {
        // Handle the error if the image upload failed
        error_log('Image upload failed: ' . $bb_wedding_bliss_image_id->get_error_message());
    }
    } else {
        // Handle the error if the post creation failed
        error_log('Post creation failed: ' . $bb_wedding_bliss_post_id->get_error_message());
    }   

    // More Events Section
    set_theme_mod( 'bb_wedding_bliss_main_title', 'More events' );
    set_theme_mod( 'bb_wedding_bliss_short_line', 'We Promise, it wont be a boring wedding' );

    // Post Category
    set_theme_mod( 'bb_wedding_bliss_event_setting', 'category1' );

    // Define post category names and post titles
    $bb_wedding_bliss_category_names = array('category1', 'category2', 'category3');
    $bb_wedding_bliss_title_array = array(
        array("Flower Shower", "Wedding Lunch", "Wedding Dance"),
        array("Flower Shower", "Wedding Lunch", "Wedding Dance"),
        array("Flower Shower", "Wedding Lunch", "Wedding Dance")
    );

        foreach ($bb_wedding_bliss_category_names as $bb_wedding_bliss_index => $bb_wedding_bliss_category_name) {
            // Create or retrieve the post category term ID
            $bb_wedding_bliss_term = term_exists($bb_wedding_bliss_category_name, 'category');
            if ($bb_wedding_bliss_term === 0 || $bb_wedding_bliss_term === null) {
                // If the term does not exist, create it
                $bb_wedding_bliss_term = wp_insert_term($bb_wedding_bliss_category_name, 'category');
            }
            if (is_wp_error($bb_wedding_bliss_term)) {
                error_log('Error creating category: ' . $bb_wedding_bliss_term->get_error_message());
                continue; // Skip to the next iteration if category creation fails
            }

            for ($bb_wedding_bliss_i = 0; $bb_wedding_bliss_i < 3; $bb_wedding_bliss_i++) {
                // Create post content
                $bb_wedding_bliss_title = $bb_wedding_bliss_title_array[$bb_wedding_bliss_index][$bb_wedding_bliss_i];
                $bb_wedding_bliss_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever.';

                // Create post post object
                $bb_wedding_bliss_my_post = array(
                    'post_title'    => wp_strip_all_tags($bb_wedding_bliss_title),
                    'post_content'  => $bb_wedding_bliss_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Post type set to 'post'
                );

                // Insert the post into the database
                $bb_wedding_bliss_post_id = wp_insert_post($bb_wedding_bliss_my_post);

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

                // Assign the category to the post
                wp_set_post_categories($bb_wedding_bliss_post_id, array((int)$bb_wedding_bliss_term['term_id']));

                // Handle the featured image using media_sideload_image
                $bb_wedding_bliss_image_url = get_template_directory_uri() . '/images/image' . ($bb_wedding_bliss_i + 1) . '.png';
                $bb_wedding_bliss_image_id = media_sideload_image($bb_wedding_bliss_image_url, $bb_wedding_bliss_post_id, null, 'id');

                if (is_wp_error($bb_wedding_bliss_image_id)) {
                    error_log('Error downloading image: ' . $bb_wedding_bliss_image_id->get_error_message());
                    continue; // Skip to the next post if image download fails
                }
                // Assign featured image to post
                set_post_thumbnail($bb_wedding_bliss_post_id, $bb_wedding_bliss_image_id);
            }
        }
           
    }
    ?>

	 <p class="note"><?php esc_html_e( 'Please Note: If your website is live and already contains data, we recommend creating a backup first. Running this importer will replace your current settings with the custom values from the demo.', 'bb-wedding-bliss' ); ?></p>
        <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=bb_wedding_bliss_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('bb_wedding_bliss_demo_import_completed')) : ?>
            <button type="submit" name="submit" class="run-import">
                    <?php esc_html_e('Run Importer','bb-wedding-bliss'); ?>
                    <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; vertical-align: middle;" />
                    </span>
            </button>
        <?php endif; ?>
        </form>
        <script type="text/javascript">
            function validate(valid) {
                if(confirm("Do you really want to import the theme demo content?")){
                    document.getElementById('spinner').style.display = 'inline-block';
                }
                else {
                    return false;
                }
            }
        </script>
     
</div>
