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

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

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

    // --- Menu ---
    $ts_mobile_app_primary_menu_name = 'Main Menu';
    $ts_mobile_app_primary_menu_location = 'primary';
    $ts_mobile_app_primary_menu_exists = wp_get_nav_menu_object($ts_mobile_app_primary_menu_name);

    if (!$ts_mobile_app_primary_menu_exists) {
        // Create the left menu
        $ts_mobile_app_primary_menu_id = wp_create_nav_menu($ts_mobile_app_primary_menu_name);

        // Create and assign the Home page
        $ts_mobile_app_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($ts_mobile_app_home_page_id, '_wp_page_template', 'page-template/custom-front-page.php');
        update_option('page_on_front', $ts_mobile_app_home_page_id);
        update_option('show_on_front', 'page');

        // Add Home page to the left menu
        wp_update_nav_menu_item($ts_mobile_app_primary_menu_id, 0, array(
            'menu-item-title'     => __('Home', 'ts-mobile-app'),
            'menu-item-classes'   => 'home',
            'menu-item-url'       => home_url('/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $ts_mobile_app_home_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the About Us page
        $ts_mobile_app_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'About 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 About Us page to the left menu
        wp_update_nav_menu_item($ts_mobile_app_primary_menu_id, 0, array(
            'menu-item-title'     => __('About Us', 'ts-mobile-app'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $ts_mobile_app_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));
        
        // Create and assign the Services page
        $ts_mobile_app_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Services',
            '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 Services page to the left menu
        wp_update_nav_menu_item($ts_mobile_app_primary_menu_id, 0, array(
            'menu-item-title'     => __('Services', 'ts-mobile-app'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $ts_mobile_app_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Features page
        $ts_mobile_app_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Features',
            '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 Features page to the left menu
        wp_update_nav_menu_item($ts_mobile_app_primary_menu_id, 0, array(
            'menu-item-title'     => __('Features', 'ts-mobile-app'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $ts_mobile_app_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

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

            } else {
                    $ts_mobile_app_blog_page_id = $ts_mobile_app_blog_page->ID;
                }
                update_option('page_for_posts', $ts_mobile_app_blog_page_id);

                wp_update_nav_menu_item($ts_mobile_app_primary_menu_id, 0, array(
                    'menu-item-title'     => __('Blog', 'ts-mobile-app'),
                    'menu-item-object-id' => $ts_mobile_app_blog_page_id,
                    'menu-item-object'    => 'page',
                    'menu-item-type'      => 'post_type',
                    'menu-item-status'    => 'publish',    
        ));

        // Create and assign the Page page
        $ts_mobile_app_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Page',
            '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 Page page to the left menu
        wp_update_nav_menu_item($ts_mobile_app_primary_menu_id, 0, array(
            'menu-item-title'     => __('Page', 'ts-mobile-app'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $ts_mobile_app_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Contact page
        $ts_mobile_app_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Contact',
            '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 Contact page to the left menu
        wp_update_nav_menu_item($ts_mobile_app_primary_menu_id, 0, array(
            'menu-item-title'     => __('Contact', 'ts-mobile-app'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $ts_mobile_app_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));


     // Assign left menu to its location
        $ts_mobile_app_locations = get_theme_mod('nav_menu_locations', array());
        $ts_mobile_app_locations[$ts_mobile_app_primary_menu_location] = $ts_mobile_app_primary_menu_id;
        set_theme_mod('nav_menu_locations', $ts_mobile_app_locations);
    }  

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


    // Slider
    set_theme_mod( 'bb_mobile_application_slider_small_title', 'WELCOME TO TS MOBILE APP' );
    for($bb_mobile_application_i=1;$bb_mobile_application_i<=4;$bb_mobile_application_i++){
       $bb_mobile_application_slider_title = 'LOREM IPSUM DOLOR';
       $bb_mobile_application_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.';
          // Create post object
       $bb_mobile_application_my_post = array(
       'post_title'    => wp_strip_all_tags( $bb_mobile_application_slider_title ),
       'post_content'  => $bb_mobile_application_slider_content,
       'post_status'   => 'publish',
       'post_type'     => 'page',
       );

       // Insert the post into the database
       $bb_mobile_application_post_id = wp_insert_post( $bb_mobile_application_my_post );

       if ($bb_mobile_application_post_id) {
         // Set the theme mod for the slider page
         set_theme_mod('bb_mobile_application_slider' . $bb_mobile_application_i, $bb_mobile_application_post_id);

          $bb_mobile_application_image_url = get_theme_file_uri().'/images/slider'.$bb_mobile_application_i.'.png';

        $bb_mobile_application_image_id = media_sideload_image($bb_mobile_application_image_url, $bb_mobile_application_post_id, null, 'id');

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

    // Post Category
    set_theme_mod( 'bb_mobile_application_blogcategory_left_setting', 'category1' );
    set_theme_mod( 'bb_mobile_application_blogcategory_right_setting', 'category2' );
    set_theme_mod( 'ts_mobile_app_category_post', 'category3' );

    // Define post category names and post titles
    $ts_mobile_app_category_names = array('category1', 'category2', 'category3');
    $ts_mobile_app_title_array = array(
        array("DIFFERENT THEME OPTION", "FULLY CUSTOMIZABLE", "UNLIMITED SUPPORT", "RESPONSIVE ALL DEVICE"),
        array("DIFFERENT THEME OPTION", "FULLY CUSTOMIZABLE", "UNLIMITED SUPPORT", "RESPONSIVE ALL DEVICE"),
        array("DIFFERENT THEME OPTION", "FULLY CUSTOMIZABLE", "UNLIMITED SUPPORT", "RESPONSIVE ALL DEVICE")
    );

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

            for ($ts_mobile_app_i = 0; $ts_mobile_app_i < 4; $ts_mobile_app_i++) {
                // Create post content
                $ts_mobile_app_title = $ts_mobile_app_title_array[$ts_mobile_app_index][$ts_mobile_app_i];
                $ts_mobile_app_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
                $ts_mobile_app_my_post = array(
                    'post_title'    => wp_strip_all_tags($ts_mobile_app_title),
                    'post_content'  => $ts_mobile_app_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Post type set to 'post'
                );

                // Insert the post into the database
                $ts_mobile_app_post_id = wp_insert_post($ts_mobile_app_my_post);

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

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

                // Handle the featured image using media_sideload_image
                $ts_mobile_app_image_url =  get_theme_file_uri() . '/images/image' . ($ts_mobile_app_i + 1) . '.png';
                $ts_mobile_app_image_id = media_sideload_image($ts_mobile_app_image_url, $ts_mobile_app_post_id, null, 'id');

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

        // Single Post
        $bb_mobile_application_post_title = 'Lorem Ipsum is simply dummy';
        $bb_mobile_application_post_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, 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.';

        // Create post object
        $bb_mobile_application_my_post = array(
        'post_title'    => wp_strip_all_tags($bb_mobile_application_post_title),
        'post_content'  => $bb_mobile_application_post_content,
        'post_status'   => 'publish',
        'post_type'     => 'post',
        );

        // Insert the post into the database
        $bb_mobile_application_post_id = wp_insert_post($bb_mobile_application_my_post);

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

        // Fetch the image URL
        $bb_mobile_application_image_url = get_template_directory_uri() . '/images/image.png';

        // Media sideload the image
        $bb_mobile_application_image_id = media_sideload_image($bb_mobile_application_image_url, $bb_mobile_application_post_id, null, 'id');

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

        // Service Section
        $ts_mobile_app_post_title = 'ABOUT TS MOBILE APP';
        $ts_mobile_app_post_content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

        // Create post object
        $ts_mobile_app_my_post = array(
        'post_title'    => wp_strip_all_tags($ts_mobile_app_post_title),
        'post_content'  => $ts_mobile_app_post_content,
        'post_status'   => 'publish',
        'post_type'     => 'post',
        );

        // Insert the post into the database
        $ts_mobile_app_post_id = wp_insert_post($ts_mobile_app_my_post);

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

        // Fetch the image URL
        $ts_mobile_app_image_url = get_theme_file_uri() . '/images/post-image.png';

        // Media sideload the image
        $ts_mobile_app_image_id = media_sideload_image($ts_mobile_app_image_url, $ts_mobile_app_post_id, null, 'id');

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

	 <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.', 'ts-mobile-app' ); ?></p>
        <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=ts_mobile_app_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('ts_mobile_app_demo_import_completed')) : ?>
            <button type="submit" name="submit" class="run-import">
                    <?php esc_html_e('Run Importer','ts-mobile-app'); ?>
                    <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>
