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

        // If the demo import is completed, display the "View Site" button
        if ($startup_solutions_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'startup-solutions') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'startup-solutions') . '</a></span>';
        }
		
        if (isset($_POST['submit'])) {

            // ------- Create Nav Menu --------
            $startup_solutions_menuname = 'Main Menus';
            $startup_solutions_bpmenulocation = 'primary';
            $startup_solutions_menu_exists = wp_get_nav_menu_object($startup_solutions_menuname);

            if (!$startup_solutions_menu_exists) {
                $startup_solutions_menu_id = wp_create_nav_menu($startup_solutions_menuname);

                // Create Home Page
                $startup_solutions_home_title = 'Home';
                $startup_solutions_home = array(
                    'post_type' => 'page',
                    'post_title' => $startup_solutions_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $startup_solutions_home_id = wp_insert_post($startup_solutions_home);
                // Assign Home Page Template
                add_post_meta($startup_solutions_home_id, '_wp_page_template', 'page-template/home-page.php');
                // Update options to set Home Page as the front page
                update_option('page_on_front', $startup_solutions_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($startup_solutions_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'startup-solutions'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $startup_solutions_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $startup_solutions_about_title = 'About Us';
                $startup_solutions_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.';
                $startup_solutions_about = array(
                    'post_type' => 'page',
                    'post_title' => $startup_solutions_about_title,
                    'post_content' => $startup_solutions_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $startup_solutions_about_id = wp_insert_post($startup_solutions_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($startup_solutions_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'startup-solutions'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $startup_solutions_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $startup_solutions_blog_page_title = 'Blog';
                $startup_solutions_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($startup_solutions_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$startup_solutions_blog_page_query->have_posts()) {
                    $startup_solutions_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $startup_solutions_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $startup_solutions_blog_page_id = wp_insert_post($startup_solutions_blog_page);
                    update_option('page_for_posts', $startup_solutions_blog_page_id);
                    wp_update_nav_menu_item($startup_solutions_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'startup-solutions'),
                    'menu-item-url'        => get_permalink($startup_solutions_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $startup_solutions_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                   ));
                }

                // Create Pages Page with Dummy Content
                $startup_solutions_contact_title = 'Contact Us';
                $startup_solutions_contact_content = 'Explore all the contact we have on our website. Find information about our services, company, and more.
                 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 contactly 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.';
                $startup_solutions_contact = array(
                    'post_type' => 'page',
                    'post_title' => $startup_solutions_contact_title,
                    'post_content' => $startup_solutions_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $startup_solutions_contact_id = wp_insert_post($startup_solutions_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($startup_solutions_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'startup-solutions'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $startup_solutions_contact_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

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

            //Topbar
            set_theme_mod('startup_solutions_topbar_text', 'For financial peace of mind you can trust Fintech');
            set_theme_mod('startup_solutions_help_text', 'Help');
            set_theme_mod('startup_solutions_topbar_help_link', '#');
            set_theme_mod('startup_solutions_what_we_do_text', 'What we do');
            set_theme_mod('startup_solutions_topbar_what_we_do_link', '#');
            set_theme_mod('startup_solutions_faq_text', 'Faq’s');
            set_theme_mod('startup_solutions_topbar_faq_link', '#');

            //Header
            set_theme_mod('startup_solutions_location_text', 'Location');
            set_theme_mod('startup_solutions_add_location', 'Lorem ipsum dolor sit');
            set_theme_mod('startup_solutions_phone_text', 'Call Us');
            set_theme_mod('startup_solutions_phone_number', '1234567890');
            set_theme_mod('startup_solutions_email_address_text', 'E-mail');
            set_theme_mod('startup_solutions_email_address1', 'xyz123@example.com');
            set_theme_mod('startup_solutions_request_btn', 'REQUEST A QUOTE');
            set_theme_mod('startup_solutions_request_link', '#');

             //Slider
             set_theme_mod('startup_solutions_slider_hide', true);
             set_theme_mod('startup_solutions_slider_button_text', 'READ MORE');
             set_theme_mod('startup_solutions_slider_button_link', '#');
 
             $startup_solutions_slider_titles = array(
                'WE ARE CREATIVE AGENCY',
                'DRIVING INNOVATION FOR YOUR BUSINESS',
                'BUILDING BRANDS THAT INSPIRE',
            );
            
            // Adjust loop to iterate over valid array indices
            for ($startup_solutions_i = 0; $startup_solutions_i < count($startup_solutions_slider_titles); $startup_solutions_i++) {
                $startup_solutions_slider_title = $startup_solutions_slider_titles[$startup_solutions_i];
                $startup_solutions_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ';
                
                // Create post object
                $startup_solutions_my_post = array(
                    'post_title'    => wp_strip_all_tags($startup_solutions_slider_title),
                    'post_content'  => $startup_solutions_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );
            
                // Insert the post into the database
                $startup_solutions_post_id = wp_insert_post($startup_solutions_my_post);
            
                if ($startup_solutions_post_id) {
                    // Set the theme mod for the slider page (add 1 to index for proper numbering)
                    set_theme_mod('startup_solutions_slider_page' . ($startup_solutions_i + 1), $startup_solutions_post_id);
            
                    $startup_solutions_image_url = get_template_directory_uri() . '/images/slider' . ($startup_solutions_i + 1) . '.png';
            
                    // Download the image and associate it with the post
                    $startup_solutions_image_id = media_sideload_image($startup_solutions_image_url, $startup_solutions_post_id, null, 'id');
            
                    if (!is_wp_error($startup_solutions_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($startup_solutions_post_id, $startup_solutions_image_id);
                    }
                }
            }            
 
             //Portfolio Section
             set_theme_mod('startup_solutions_portfolio_hide', true);
             set_theme_mod('startup_solutions_porfolio_section_title', 'PORTFOLIO');
             set_theme_mod('startup_solutions_porfolio_text', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.');
             set_theme_mod('startup_solutions_post_category', 'postcategory1');
 
             // Define post category names and post titles
             $startup_solutions_category_names = array('postcategory1', 'postcategory2', 'postcategory3', 'postcategory4');
             $startup_solutions_title_array = array(
                 array("Duis fermentum felis Retailing", "Duis fermentum felis Retailing", "Duis fermentum felis Retailing"),
                 array("Duis fermentum felis Retailing", "Duis fermentum felis Retailing", "Duis fermentum felis Retailing"),
                 array("Duis fermentum felis Retailing", "Duis fermentum felis Retailing", "Duis fermentum felis Retailing"),
                 array("Duis fermentum felis Retailing", "Duis fermentum felis Retailing", "Duis fermentum felis Retailing")
             );
 
             $startup_solutions_content_array = array(
                 array(
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
                 ),
                 array(
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
                 ),
                 array(
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
                 ),
                 array(
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do",
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
                 )
             );
 
             foreach ($startup_solutions_category_names as $startup_solutions_index => $startup_solutions_category_name) {
                 // Create or retrieve the post category term ID
                 $startup_solutions_term = term_exists($startup_solutions_category_name, 'category');
                 if (!$startup_solutions_term || is_wp_error($startup_solutions_term)) {
                     $startup_solutions_term = wp_insert_term($startup_solutions_category_name, 'category');
                     if (is_wp_error($startup_solutions_term)) {
                         error_log('Error creating category: ' . $startup_solutions_term->get_error_message());
                         continue; // Skip to the next iteration if category creation fails
                     }
                 }
             
                 $startup_solutions_term_id = is_array($startup_solutions_term) ? $startup_solutions_term['term_id'] : $startup_solutions_term;
             
                 for ($startup_solutions_i = 0; $startup_solutions_i < 3; $startup_solutions_i++) {
                     // Create post content
                     $startup_solutions_title = $startup_solutions_title_array[$startup_solutions_index][$startup_solutions_i];
                     $startup_solutions_content = $startup_solutions_content_array[$startup_solutions_index][$startup_solutions_i];
             
                     // Create post object
                     $startup_solutions_my_post = array(
                         'post_title'    => wp_strip_all_tags($startup_solutions_title),
                         'post_content'  => $startup_solutions_content,
                         'post_status'   => 'publish',
                         'post_type'     => 'post', // Post type set to 'post'
                     );
             
                     // Insert the post into the database
                     $startup_solutions_post_id = wp_insert_post($startup_solutions_my_post);
             
                     if (is_wp_error($startup_solutions_post_id)) {
                         error_log('Error creating post: ' . $startup_solutions_post_id->get_error_message());
                         continue; // Skip to the next post if creation fails
                     }
             
                     // Assign the category to the post
                     wp_set_post_categories($startup_solutions_post_id, array((int)$startup_solutions_term_id));
             
                     // Handle the featured image using media_sideload_image
                     $startup_solutions_image_url = get_template_directory_uri() . '/images/service' . ($startup_solutions_i + 1) . '.png';
                     $startup_solutions_image_id = media_sideload_image($startup_solutions_image_url, $startup_solutions_post_id, null, 'id');
             
                     if (is_wp_error($startup_solutions_image_id)) {
                         error_log('Error downloading image: ' . $startup_solutions_image_id->get_error_message());
                         continue; // Skip to the next post if image download fails
                     }
             
                     // Assign featured image to post
                     set_post_thumbnail($startup_solutions_post_id, $startup_solutions_image_id);
             
                     // Add post meta for the icon with specific icons for each post
                     $startup_solutions_icon_classes = array("fas fa-chart-line", "fas fa-thermometer-half", "fas fa-wheelchair");
                     $startup_solutions_selected_icon = isset($startup_solutions_icon_classes[$startup_solutions_i]) ? $startup_solutions_icon_classes[$startup_solutions_i] : $startup_solutions_icon_classes[0];
                     update_post_meta($startup_solutions_post_id, 'startup_solutions_icon', $startup_solutions_selected_icon);
                 }
             }
            
            // Set the demo import completion flag
    		update_option('startup_solutions_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.', 'startup-solutions') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'startup-solutions') . '</a></span>';
            //end

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=startup-solutions-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('startup_solutions_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','startup-solutions'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','startup-solutions'); ?>" class="button button-primary button-large">
        </form>
    <?php endif; ?>
    </form>
	<script type="text/javascript">
		function validate(valid) {
            if (confirm("Do you really want to import the theme demo content?")) {
                // Show loader
                document.getElementById('demo-import-loader').style.display = 'block';
                
                // Submit form
                document.forms[0].submit();
            } else {
                return false;
            }
        }
	</script>
</div>
