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

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


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

    // --- Menu ---
    $advance_portfolio_primary_menu_name = 'Main Menu';
    $advance_portfolio_primary_menu_location = 'primary';
    $advance_portfolio_primary_menu_exists = wp_get_nav_menu_object($advance_portfolio_primary_menu_name);

    if (!$advance_portfolio_primary_menu_exists) {
        // Create the left menu
        $advance_portfolio_primary_menu_id = wp_create_nav_menu($advance_portfolio_primary_menu_name);

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

        // Add Home page to the left menu
        wp_update_nav_menu_item($advance_portfolio_primary_menu_id, 0, array(
            'menu-item-title'     => __('Home', 'advance-portfolio'),
            'menu-item-classes'   => 'home',
            'menu-item-url'       => home_url('/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_portfolio_home_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the About Us page
        $advance_portfolio_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($advance_portfolio_primary_menu_id, 0, array(
            'menu-item-title'     => __('About Us', 'advance-portfolio'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_portfolio_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Skills page
        $advance_portfolio_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Skills',
            '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 Skills page to the left menu
        wp_update_nav_menu_item($advance_portfolio_primary_menu_id, 0, array(
            'menu-item-title'     => __('Skills', 'advance-portfolio'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_portfolio_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Portfolio page
        $advance_portfolio_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Portfolio',
            '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 Portfolio page to the left menu
        wp_update_nav_menu_item($advance_portfolio_primary_menu_id, 0, array(
            'menu-item-title'     => __('Portfolio', 'advance-portfolio'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_portfolio_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Education page
        $advance_portfolio_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Education',
            '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 Education page to the left menu
        wp_update_nav_menu_item($advance_portfolio_primary_menu_id, 0, array(
            'menu-item-title'     => __('Education', 'advance-portfolio'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_portfolio_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Testimonials page
        $advance_portfolio_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Testimonials',
            '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 Testimonials page to the left menu
        wp_update_nav_menu_item($advance_portfolio_primary_menu_id, 0, array(
            'menu-item-title'     => __('Testimonials', 'advance-portfolio'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_portfolio_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

         // Create and assign the Contact page
        $advance_portfolio_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($advance_portfolio_primary_menu_id, 0, array(
            'menu-item-title'     => __('Contact', 'advance-portfolio'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_portfolio_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

     // Assign left menu to its location
        $advance_portfolio_locations = get_theme_mod('nav_menu_locations', array());
        $advance_portfolio_locations[$advance_portfolio_primary_menu_location] = $advance_portfolio_primary_menu_id;
        set_theme_mod('nav_menu_locations', $advance_portfolio_locations);
    }  

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

    //end

    // Social Icon link
    set_theme_mod( 'advance_portfolio_facebook_url', '#' );
    set_theme_mod( 'advance_portfolio_twitter_url', '#' );
    set_theme_mod( 'advance_portfolio_linkedin_url', '#' );
    set_theme_mod( 'advance_portfolio_insta_url', '#' );
    set_theme_mod( 'advance_portfolio_youtube_url', '#' );
    set_theme_mod( 'advance_portfolio_behance_url', '#' );

    // Banner
    $advance_portfolio_slider_title = 'Lorem ipsum elit, dolor sit amet.';
    $advance_portfolio_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipiscin.';
        // Create post object
        $advance_portfolio_my_post = array(
        'post_title'    => wp_strip_all_tags( $advance_portfolio_slider_title ),
        'post_content'  => $advance_portfolio_slider_content,
        'post_status'   => 'publish',
        'post_type'     => 'page',
        );

        // Insert the post into the database
        $advance_portfolio_post_id = wp_insert_post( $advance_portfolio_my_post );

        if ($advance_portfolio_post_id) {
        // Set the theme mod for the slider page
        set_theme_mod('advance_portfolio_page_settings', $advance_portfolio_post_id);

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

        $advance_portfolio_image_id = media_sideload_image($advance_portfolio_image_url, $advance_portfolio_post_id, null, 'id');

        if (!is_wp_error($advance_portfolio_image_id)) {
            // Set the downloaded image as the post's featured image
            set_post_thumbnail($advance_portfolio_post_id, $advance_portfolio_image_id);
        }
    }
 
    // Awesome Portfolio Section
    set_theme_mod( 'advance_portfolio_title', 'Lorem ipsum' );

    // Define post titles, content, and image sizes
    $advance_portfolio_posts_data = array(
        array(
            'title' => 'Lorem Ipsum Post 1',
            'content' => 'Lorem Ipsum is simply dummy text for Post 1.',
            'setting_id' => 'advance_portfolio_awesome_setting',
            'image_url' => get_template_directory_uri() . '/images/image1.png'
        ),
        array(
            'title' => 'Lorem Ipsum Post 2',
            'content' => 'Lorem Ipsum is simply dummy text for Post 2.',
            'setting_id' => 'advance_portfolio_awesome_setting1',
            'image_url' => get_template_directory_uri() . '/images/image2.png'
        ),
        array(
            'title' => 'Lorem Ipsum Post 3',
            'content' => 'Lorem Ipsum is simply dummy text for Post 3.',
            'setting_id' => 'advance_portfolio_awesome_setting2',
            'image_url' => get_template_directory_uri() . '/images/image3.png'
        ),
        array(
            'title' => 'Lorem Ipsum Post 4',
            'content' => 'Lorem Ipsum is simply dummy text for Post 4.',
            'setting_id' => 'advance_portfolio_awesome_setting3',
            'image_url' => get_template_directory_uri() . '/images/image4.png'
        ),
    );

    // Loop through each post data and create the post
    foreach ($advance_portfolio_posts_data as $advance_portfolio_post_data) {
        // Create post object
        $advance_portfolio_post_args = array(
            'post_title'   => wp_strip_all_tags($advance_portfolio_post_data['title']),
            'post_content' => $advance_portfolio_post_data['content'],
            'post_status'  => 'publish',
            'post_type'    => 'post',
        );

        // Insert the post into the database
        $advance_portfolio_post_id = wp_insert_post($advance_portfolio_post_args);

        // Check if the post was successfully created
        if (!is_wp_error($advance_portfolio_post_id)) {
            // Set the theme mod for the post ID
            set_theme_mod($advance_portfolio_post_data['setting_id'], $advance_portfolio_post_id);

            // Media sideload the image
            $advance_portfolio_image_id = media_sideload_image($advance_portfolio_post_data['image_url'], $advance_portfolio_post_id, null, 'id');

            // Check if the image was successfully uploaded
            if (!is_wp_error($advance_portfolio_image_id)) {
                // Set the downloaded image as the post's featured image
                set_post_thumbnail($advance_portfolio_post_id, $advance_portfolio_image_id);
            } else {
                // Handle the error if the image upload failed
                error_log('Image upload failed for ' . $advance_portfolio_post_data['title'] . ': ' . $advance_portfolio_image_id->get_error_message());
            }
        } else {
            // Handle the error if the post creation failed
            error_log('Post creation failed for ' . $advance_portfolio_post_data['title'] . ': ' . $advance_portfolio_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.', 'advance-portfolio' ); ?></p>
        <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=advance_portfolio_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('advance_portfolio_demo_import_completed')) : ?>
            <button type="submit" name="submit" class="run-import">
                    <?php esc_html_e('Run Importer','advance-portfolio'); ?>
                    <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>
