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

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


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

    // --- Menu ---
    $advance_it_company_primary_menu_name = 'Main Menu';
    $advance_it_company_primary_menu_location = 'primary';
    $advance_it_company_primary_menu_exists = wp_get_nav_menu_object($advance_it_company_primary_menu_name);

    if (!$advance_it_company_primary_menu_exists) {
        // Create the left menu
        $advance_it_company_primary_menu_id = wp_create_nav_menu($advance_it_company_primary_menu_name);

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

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

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

        // Create and assign the Services page
        $advance_it_company_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($advance_it_company_primary_menu_id, 0, array(
            'menu-item-title'     => __('Services', 'advance-it-company'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $advance_it_company_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

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

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

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

            } else {
                 $advance_it_company_blog_page_id = $advance_it_company_blog_page->ID;
            }
            update_option('page_for_posts', $advance_it_company_blog_page_id);

             wp_update_nav_menu_item($advance_it_company_primary_menu_id, 0, array(
                'menu-item-title'     => __('IT Blog', 'advance-it-company'),
                'menu-item-object-id' => $advance_it_company_blog_page_id,
                'menu-item-object'    => 'page',
                'menu-item-type'      => 'post_type',
                'menu-item-status'    => 'publish',
        ));       

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


        // Assign left menu to its location
        $advance_it_company_locations = get_theme_mod('nav_menu_locations', array());
        $advance_it_company_locations[$advance_it_company_primary_menu_location] = $advance_it_company_primary_menu_id;
        set_theme_mod('nav_menu_locations', $advance_it_company_locations);
    }  

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

    //end

    // Social Icons
    set_theme_mod( 'advance_it_company_facebook_url', '#' );
    set_theme_mod( 'advance_it_company_twitter_url', '#' );    
    set_theme_mod( 'advance_it_company_linkedin_url', '#' );
    set_theme_mod( 'advance_it_company_instagram_url', '#' );
    set_theme_mod( 'advance_it_company_youtube_url', '#' );

    // Topbar Section
    set_theme_mod( 'advance_it_company_phone', 'CALL US' );
    set_theme_mod( 'advance_it_company_phone1', '123-456-7890' );
    set_theme_mod( 'advance_it_company_mail', 'EMAIL' );
    set_theme_mod( 'advance_it_company_mail1', 'support@example.com' );
    set_theme_mod( 'advance_it_company_address', 'ADDRESS' );
    set_theme_mod( 'advance_it_company_address1', '15, Edison Street, Baltimore' );

    // Slider
    for($advance_it_company_i=1;$advance_it_company_i<=4;$advance_it_company_i++){
       $advance_it_company_slider_title = 'LOREM IPSUM DOLOR SIT AMET CONSECTETUR ADIPISCING ELIT';
       $advance_it_company_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
       $advance_it_company_my_post = array(
       'post_title'    => wp_strip_all_tags( $advance_it_company_slider_title ),
       'post_content'  => $advance_it_company_slider_content,
       'post_status'   => 'publish',
       'post_type'     => 'page',
       );

       // Insert the post into the database
       $advance_it_company_post_id = wp_insert_post( $advance_it_company_my_post );

       if ($advance_it_company_post_id) {
         // Set the theme mod for the slider page
         set_theme_mod('advance_it_company_slider_page' . $advance_it_company_i, $advance_it_company_post_id);

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

        $advance_it_company_image_id = media_sideload_image($advance_it_company_image_url, $advance_it_company_post_id, null, 'id');

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

    // How it works Section
    set_theme_mod( 'advance_it_company_title', 'HOW IT WORKS' );

    // Create Posts
    $advance_it_company_post_title = 'LOREM IPSUM DOLOR SIT AMET';
    $advance_it_company_post_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.';

    // Define the image URL (update as needed)
    $advance_it_company_image_url = get_template_directory_uri() . '/images/post-image.png';

    // Define the category name
    $advance_it_company_category_name = 'example-category'; // Replace with your desired category name

    // Check if the category exists, or create it
    $advance_it_company_existing_category = term_exists($advance_it_company_category_name, 'category');
    if ($advance_it_company_existing_category && is_array($advance_it_company_existing_category)) {
        // Get the term ID of the existing category
        $advance_it_company_category_term_id = (int) $advance_it_company_existing_category['term_id'];
    } else {
        // Create the category if it doesn't exist
        $advance_it_company_new_category = wp_insert_term($advance_it_company_category_name, 'category');
        if (!is_wp_error($advance_it_company_new_category)) {
            $advance_it_company_category_term_id = (int) $advance_it_company_new_category['term_id'];
        } else {
            // Log the error and stop if the category cannot be created
            error_log('Error creating category: ' . $advance_it_company_new_category->get_error_message());
            return;
        }
    }

    // Create the post array
    $advance_it_company_my_post = array(
        'post_title'    => wp_strip_all_tags($advance_it_company_post_title),
        'post_content'  => $advance_it_company_post_content,
        'post_status'   => 'publish',
        'post_type'     => 'post',
    );

    // Insert the post into the database
    $advance_it_company_post_id = wp_insert_post($advance_it_company_my_post);

    // Check if the post was created successfully
    if (!is_wp_error($advance_it_company_post_id)) {
        wp_set_post_categories($advance_it_company_post_id, array($advance_it_company_category_term_id));

        $advance_it_company_image_id = media_sideload_image($advance_it_company_image_url, $advance_it_company_post_id, null, 'id');

        if (!is_wp_error($advance_it_company_image_id)) {
            set_post_thumbnail($advance_it_company_post_id, $advance_it_company_image_id);
        } else {
            error_log('Error downloading image: ' . $advance_it_company_image_id->get_error_message());
        }

        set_theme_mod('advance_it_company_setting', $advance_it_company_post_id);
    } else {
        error_log('Post creation failed: ' . $advance_it_company_post_id->get_error_message());
    }

    // Post Category
    set_theme_mod( 'advance_it_company_works_category', 'category1' );
    $advance_it_company_category_names = array('category1', 'category2', 'category3');
    $advance_it_company_title_array = array(
        array("Title 1", "Title 2", "Title 3", "Title 4"),
        array("Title 1", "Title 2", "Title 3", "Title 4"),
        array("Title 1", "Title 2", "Title 3", "Title 4")
    );

    foreach ($advance_it_company_category_names as $advance_it_company_index => $advance_it_company_category_name) {
        $advance_it_company_category_term_id = 0;
        $advance_it_company_existing_term = term_exists($advance_it_company_category_name, 'category');
        if ($advance_it_company_existing_term && is_array($advance_it_company_existing_term)) {
            $advance_it_company_category_term_id = (int)$advance_it_company_existing_term['term_id'];
        } else {
            $advance_it_company_new_term = wp_insert_term($advance_it_company_category_name, 'category');
            if (!is_wp_error($advance_it_company_new_term)) {
                $advance_it_company_category_term_id = (int)$advance_it_company_new_term['term_id'];
            } else {
                error_log('Error creating category: ' . $advance_it_company_new_term->get_error_message());
                continue;
            }
        }

        if (!$advance_it_company_category_term_id) {
            error_log('Error: Could not retrieve or create a valid category term ID for ' . $advance_it_company_category_name);
            continue;
        }

        for ($advance_it_company_i = 0; $advance_it_company_i < 4; $advance_it_company_i++) {
            // Create post content
            $advance_it_company_post_title = $advance_it_company_title_array[$advance_it_company_index][$advance_it_company_i];
            $advance_it_company_post_content = 'Lorem ipsum dolor sit amet consectetur adipiscing.';

            // Create post object
            $advance_it_company_post_data = array(
                'post_title'    => wp_strip_all_tags($advance_it_company_post_title),
                'post_content'  => $advance_it_company_post_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
            );

            $advance_it_company_post_id = wp_insert_post($advance_it_company_post_data);

            if (is_wp_error($advance_it_company_post_id)) {
                error_log('Error creating post: ' . $advance_it_company_post_id->get_error_message());
                continue;
            }

            wp_set_post_categories($advance_it_company_post_id, array($advance_it_company_category_term_id));

            $advance_it_company_image_url = get_template_directory_uri() . '/images/image' . ($advance_it_company_i + 1) . '.png';
            $advance_it_company_image_id = media_sideload_image($advance_it_company_image_url, $advance_it_company_post_id, null, 'id');

            if (is_wp_error($advance_it_company_image_id)) {
                // Log error and skip to the next post if image download fails
                error_log('Error downloading image: ' . $advance_it_company_image_id->get_error_message());
                continue;
            }

            set_post_thumbnail($advance_it_company_post_id, $advance_it_company_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.', 'advance-it-company' ); ?></p>
        <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=advance_it_company_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('advance_it_company_demo_import_completed')) : ?>
            <button type="submit" name="submit" class="run-import">
                    <?php esc_html_e('Run Importer','advance-it-company'); ?>
                    <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>
