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

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

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

        // ------- Create Nav Menu --------
        $it_services_menuname = 'Main Menus';
        $it_services_bpmenulocation = 'primary';
        $it_services_menu_exists = wp_get_nav_menu_object($it_services_menuname);

        if (!$it_services_menu_exists) {
            $it_services_menu_id = wp_create_nav_menu($it_services_menuname);

        // Create Home Page
        $it_services_home_title = 'Home';
        $it_services_home = array(
            'post_type' => 'page',
            'post_title' => $it_services_home_title,
            'post_content' => '',
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'home'
        );
        $it_services_home_id = wp_insert_post($it_services_home);
        // Assign Home Page Template
        add_post_meta($it_services_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', $it_services_home_id);
        update_option('show_on_front', 'page');
        // Add Home Page to Menu
        wp_update_nav_menu_item($it_services_menu_id, 0, array(
            'menu-item-title' => __('Home', 'it-services'),
            'menu-item-classes' => 'home',
            'menu-item-url' => home_url('/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $it_services_home_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

        // Create About Us Page with Dummy Content
        $it_services_about_title = 'About Us';
        $it_services_about_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>
        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> 
        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.<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.';
        $it_services_about = array(
            'post_type' => 'page',
            'post_title' => $it_services_about_title,
            'post_content' => $it_services_about_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'about-us'
        );
        $it_services_about_id = wp_insert_post($it_services_about);
        // Add About Us Page to Menu
        wp_update_nav_menu_item($it_services_menu_id, 0, array(
            'menu-item-title' => __('About Us', 'it-services'),
            'menu-item-classes' => 'about-us',
            'menu-item-url' => home_url('/about-us/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $it_services_about_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

        // Services Page
        $it_services_services_title = 'Services';
        $it_services_services_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.';
        $it_services_services_id = wp_insert_post( array(
            'post_type'    => 'page',
            'post_title'   => $it_services_services_title,
            'post_content' => $it_services_services_content,
            'post_name'    => 'services',
            'post_status'  => 'publish',
            'post_author'  => 1
        ) );
        wp_update_nav_menu_item( $it_services_menu_id, 0, array(
            'menu-item-title'     => 'Services',
            'menu-item-object-id' => $it_services_services_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
            'menu-item-status'    => 'publish'
        ) );

        // Works Page
        $it_services_works_title = 'Works';
        $it_services_works_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.';
        $it_services_works_id = wp_insert_post( array(
            'post_type'    => 'page',
            'post_title'   => 'Works',
            'post_content' => $it_services_works_content,
            'post_name'    => 'works',
            'post_status'  => 'publish',
            'post_author'  => 1
        ) );
        wp_update_nav_menu_item( $it_services_menu_id, 0, array(
            'menu-item-title'     => $it_services_works_title,
            'menu-item-object-id' => $it_services_works_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
            'menu-item-status'    => 'publish'
        ) );

         // Create Pages Page with Dummy Content
        $it_services_pages_title = 'Pages';
        $it_services_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.';
        $it_services_pages = array(
            'post_type' => 'page',
            'post_title' => $it_services_pages_title,
            'post_content' => $it_services_pages_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'pages'
        );
        $it_services_pages_id = wp_insert_post($it_services_pages);
        // Add Pages Page to Menu
        wp_update_nav_menu_item($it_services_menu_id, 0, array(
            'menu-item-title' => __('Pages', 'it-services'),
            'menu-item-classes' => 'pages',
            'menu-item-url' => home_url('/pages/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $it_services_pages_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

         // Blog Page (Posts Page)
            $it_services_blog_id = wp_insert_post( array(
                'post_type'    => 'page',
                'post_title'   => 'Blog',
                'post_name'    => 'blog',
                'post_status'  => 'publish',
                'post_author'  => 1
            ) );
            wp_update_nav_menu_item( $it_services_menu_id, 0, array(
                'menu-item-title'     => 'Blog',
                'menu-item-object-id' => $it_services_blog_id,
                'menu-item-object'    => 'page',
                'menu-item-type'      => 'post_type',
                'menu-item-status'    => 'publish'
            ) );

            // Contacts Page
            $it_services_contact_title = 'Contacts';
            $it_services_contact_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.';
            $it_services_contacts_id = wp_insert_post( array(
                'post_type'    => 'page',
                'post_title'   => $it_services_contact_title,
                'post_content' => $it_services_contact_content,
                'post_name'    => 'contacts',
                'post_status'  => 'publish',
                'post_author'  => 1
            ) );
            wp_update_nav_menu_item( $it_services_menu_id, 0, array(
                'menu-item-title'     => 'Contacts',
                'menu-item-object-id' => $it_services_contacts_id,
                'menu-item-object'    => 'page',
                'menu-item-type'      => 'post_type',
                'menu-item-status'    => 'publish'
            ) );

        // Set the menu location if it's not already set
        if (!has_nav_menu($it_services_bpmenulocation)) {
            $it_services_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
            if (empty($it_services_locations)) {
                $it_services_locations = array();
            }
            $it_services_locations[$it_services_bpmenulocation] = $it_services_menu_id;
            set_theme_mod('nav_menu_locations', $it_services_locations);
        } 
        }    

        // Set front page and posts page
            update_option( 'page_on_front', $it_services_home_id );
            update_option( 'show_on_front', 'page' );
            update_option( 'page_for_posts', $it_services_blog_id );

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

        //Topbar
        set_theme_mod( 'it_company_top_header', true ); 
        set_theme_mod( 'it_company_email', 'domain@gmail.com' );
        set_theme_mod( 'it_company_call_number', '123-456-7890' ); 
        set_theme_mod( 'it_company_question',  "184 Mian colin Stree West victoria 807" ); 
        
        //Slider
        set_theme_mod( 'it_company_slider_hide', true );

        $it_company_slider_titles = [
            'Outsourcing Company',
            'Your Tech Partner, Always',
            'Innovating for a Better Tomorrow',
            'We Keep Your Business Connected',
        ];   

        for($it_company_i=1;$it_company_i<=4;$it_company_i++){
            $it_company_slider_title = $it_company_slider_titles[$it_company_i - 1];
            $it_company_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut ';
            // Create post object
            $it_company_my_post = array(
                'post_title'    => wp_strip_all_tags( $it_company_slider_title ),
                'post_content'  => $it_company_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
            );

            // Insert the post into the database
            $it_company_post_id = wp_insert_post( $it_company_my_post );

            if ($it_company_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('it_company_slider_page' . $it_company_i, $it_company_post_id);

                $it_company_image_url = get_theme_file_uri().'/images/slider'.$it_company_i.'.png';

                $it_company_image_id = media_sideload_image($it_company_image_url, $it_company_post_id, null, 'id');

                    if (!is_wp_error($it_company_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($it_company_post_id, $it_company_image_id);
                    }
            }
        } 
        // Enable section and set title
        set_theme_mod('it_services_section_title', 'WHAT DO WE DO?');

        // Create or retrieve the category for "What we do"
        $it_services_category_name = 'what-we-do';
        $it_services_category_term = term_exists($it_services_category_name, 'category');

        if (!$it_services_category_term) {
            $it_services_category_term = wp_insert_term($it_services_category_name, 'category');
        }

        if (!is_wp_error($it_services_category_term)) {
            $it_services_category_id = isset($it_services_category_term['term_id']) ? (int)$it_services_category_term['term_id'] : $it_services_category_term;

            // Link category to customizer setting
            set_theme_mod('it_services_what_we_do_category', $it_services_category_name);

            // Define demo posts (title + content)
            $it_services_posts = array(
                array(
                    'title'   => 'Web Development',
                    'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed diam nonummy nibh euismod tincidunt ut laoreet.',
                    'image'   => get_theme_file_uri('images/web-development.png'),
                ),
                array(
                    'title'   => 'App Development',
                    'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed diam nonummy nibh euismod tincidunt ut laoreet.',
                    'image'   => get_theme_file_uri('images/app-development.png'),
                ),
                array(
                    'title'   => 'Graphic Design',
                    'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed diam nonummy nibh euismod tincidunt ut laoreet.',
                    'image'   => get_theme_file_uri('images/graphic-design.png'),
                ),
            );

            // Create posts
            foreach ($it_services_posts as $it_services_post) {
                $post_id = wp_insert_post(array(
                    'post_title'   => wp_strip_all_tags($it_services_post['title']),
                    'post_content' => $it_services_post['content'],
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                ));

                if (!is_wp_error($post_id)) {
                    // Assign category
                    wp_set_post_categories($post_id, array($it_services_category_id));

                    // Add featured image
                    if (!empty($it_services_post['image'])) {
                        $image_id = media_sideload_image($it_services_post['image'], $post_id, null, 'id');
                        if (!is_wp_error($image_id)) {
                            set_post_thumbnail($post_id, $image_id);
                        }
                    }
                }
            }
        } else {
            error_log('Error creating What We Do category: ' . $it_services_category_term->get_error_message());
        }
            //Copyright Text
            set_theme_mod( 'it_services_footer_text', 'By Themesglance' );
    }
    ?>

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