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

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

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

            // ------- Create Nav Menu --------
            $car_repair_mechanic_menuname = 'Main Menus';
            $car_repair_mechanic_bpmenulocation = 'primary';
            $car_repair_mechanic_menu_exists = wp_get_nav_menu_object($car_repair_mechanic_menuname);

            if (!$car_repair_mechanic_menu_exists) {
                $car_repair_mechanic_menu_id = wp_create_nav_menu($car_repair_mechanic_menuname);

                // Create Home Page
                $car_repair_mechanic_home_title = 'Home';
                $car_repair_mechanic_home = array(
                    'post_type' => 'page',
                    'post_title' => $car_repair_mechanic_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $car_repair_mechanic_home_id = wp_insert_post($car_repair_mechanic_home);
                // Assign Home Page Template
                add_post_meta($car_repair_mechanic_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', $car_repair_mechanic_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($car_repair_mechanic_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'car-repair-mechanic'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $car_repair_mechanic_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $car_repair_mechanic_pages_title = 'Pages';
                $car_repair_mechanic_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.';
                $car_repair_mechanic_pages = array(
                    'post_type' => 'page',
                    'post_title' => $car_repair_mechanic_pages_title,
                    'post_content' => $car_repair_mechanic_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $car_repair_mechanic_pages_id = wp_insert_post($car_repair_mechanic_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($car_repair_mechanic_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'car-repair-mechanic'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $car_repair_mechanic_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $car_repair_mechanic_about_title = 'About Us';
                $car_repair_mechanic_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.';
                $car_repair_mechanic_about = array(
                    'post_type' => 'page',
                    'post_title' => $car_repair_mechanic_about_title,
                    'post_content' => $car_repair_mechanic_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $car_repair_mechanic_about_id = wp_insert_post($car_repair_mechanic_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($car_repair_mechanic_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'car-repair-mechanic'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $car_repair_mechanic_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                // Create Blog Page only if it doesn't exist already
                $car_repair_mechanic_existing_blog_page = get_page_by_path('blog');
                if (!$car_repair_mechanic_existing_blog_page) {
                    $car_repair_mechanic_blog_id = wp_insert_post(array(
                    'post_type'    => 'page',
                    'post_title'   => __('Blog', 'car-repair-mechanic'),
                    'post_content' => '',
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'blog'
                ));
                } else {
                    $car_repair_mechanic_blog_id = $car_repair_mechanic_existing_blog_page->ID;
                }
                // Assign as Posts Page
                update_option('page_for_posts', $car_repair_mechanic_blog_id);
                update_option('show_on_front', 'page');
                //Add to Menu (only if not already added)
                $car_repair_mechanic_menu_items = wp_get_nav_menu_items($car_repair_mechanic_menu_id);
                $car_repair_mechanic_blog_in_menu = false;
                if ($car_repair_mechanic_menu_items) {
                    foreach ($car_repair_mechanic_menu_items as $car_repair_mechanic_item) {
                        if ((int) $car_repair_mechanic_item->object_id === (int) $car_repair_mechanic_blog_id) {
                            $car_repair_mechanic_blog_in_menu = true;
                            break;
                        }
                    }
                }
                if (!$car_repair_mechanic_blog_in_menu) {
                wp_update_nav_menu_item($car_repair_mechanic_menu_id, 0, array(
                    'menu-item-title'       => __('Blog', 'car-repair-mechanic'),
                    'menu-item-url'         => home_url('/blog/'),
                    'menu-item-status'      => 'publish',
                    'menu-item-object-id'   => $car_repair_mechanic_blog_id,
                    'menu-item-object'      => 'page',
                    'menu-item-type'        => 'post_type',
                    'menu-item-classes'     => 'car-repair-mechanic-blog-link'
                ));
                }
                // Set the menu location if it's not already set
                if (!has_nav_menu($car_repair_mechanic_bpmenulocation)) {
                    $car_repair_mechanic_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($car_repair_mechanic_locations)) {
                        $car_repair_mechanic_locations = array();
                    }
                    $car_repair_mechanic_locations[$car_repair_mechanic_bpmenulocation] = $car_repair_mechanic_menu_id;
                    set_theme_mod('nav_menu_locations', $car_repair_mechanic_locations);
                }
            } 
            
        // Set the demo import completion flag
        update_option('car_repair_mechanic_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.', 'car-repair-mechanic') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VIEW SITE', 'car-repair-mechanic') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'car-repair-mechanic') . '</a></span>';
        //end 

        //Topbar
        set_theme_mod( 'car_repair_mechanic_location', 'Lorem ipsum dolor sit amet' );
        set_theme_mod( 'car_repair_mechanic_mail_address', 'xyz123@example.com' );
        set_theme_mod( 'car_repair_mechanic_call', '+123 456 789' );
        set_theme_mod( 'car_repair_mechanic_header_button_text', 'Make Appointment' );
        set_theme_mod( 'car_repair_mechanic_header_button_link', '#' );

        //Slider
        set_theme_mod( 'car_repair_mechanic_show_slider', true );
        set_theme_mod( 'car_repair_mechanic_facebook_url', '#' );
        set_theme_mod( 'car_repair_mechanic_twitter_url', '#' );
        set_theme_mod( 'car_repair_mechanic_instagram_url', '#' );
        set_theme_mod( 'car_repair_mechanic_linkedin_url', '#' );
        set_theme_mod( 'car_repair_mechanic_slider_button_label', 'Read More' );
        set_theme_mod( 'car_repair_mechanic_slider_button_link', '#' );

        $car_repair_mechanic_slider_titles = [
            'Providing a Professional & Reliable Service',
            'Delivering Expert Solutions with Dependable Care.',
            'Ensuring Quality Repairs You Can Trust.',
            'Committed to Precision and Customer Satisfaction.',
        ];  

        for($car_repair_mechanic_i=1;$car_repair_mechanic_i<=4;$car_repair_mechanic_i++){
            $car_repair_mechanic_slider_title = $car_repair_mechanic_slider_titles[$car_repair_mechanic_i - 1];
            $car_repair_mechanic_slider_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s';
            // Create post object
            $car_repair_mechanic_my_post = array(
            'post_title'    => wp_strip_all_tags( $car_repair_mechanic_slider_title ),
            'post_content'  => $car_repair_mechanic_slider_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
            );

            // Insert the post into the database
            $car_repair_mechanic_post_id = wp_insert_post( $car_repair_mechanic_my_post );

            if ($car_repair_mechanic_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('car_repair_mechanic_slidersettings_page' . $car_repair_mechanic_i, $car_repair_mechanic_post_id);

                $car_repair_mechanic_image_url = get_template_directory_uri().'/images/banner'.$car_repair_mechanic_i.'.png';

                $car_repair_mechanic_image_id = media_sideload_image($car_repair_mechanic_image_url, $car_repair_mechanic_post_id, null, 'id');

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

        set_theme_mod( 'car_repair_mechanic_video_post', 'Lorem Ipsum is simply dummy text' );
        $car_repair_mechanic_service_titles = array(
            "Lorem Ipsum is simply dummy text",
            "printing and typesetting industry"
        );
        $car_repair_mechanic_service_image_url = get_template_directory_uri() . '/images/event1.png';

        for ($car_repair_mechanic_i = 1; $car_repair_mechanic_i <= 2; $car_repair_mechanic_i++) {
            // Create post
            $car_repair_mechanic_title = $car_repair_mechanic_service_titles[$car_repair_mechanic_i - 1];
            $car_repair_mechanic_content = "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quisquam velit recusandae unde doloremque, veniam ullam.";
            $car_repair_mechanic_post_data = array(
                'post_title'   => wp_strip_all_tags($car_repair_mechanic_title),
                'post_content' => wp_strip_all_tags($car_repair_mechanic_content),
                'post_status'  => 'publish',
                'post_type'    => 'post',
            );
            $car_repair_mechanic_post_id = wp_insert_post($car_repair_mechanic_post_data);

            if (!is_wp_error($car_repair_mechanic_post_id)) {
                // Add featured image
                $car_repair_mechanic_image_id = media_sideload_image($car_repair_mechanic_service_image_url, $car_repair_mechanic_post_id, null, 'id');
                if (!is_wp_error($car_repair_mechanic_image_id)) {
                    set_post_thumbnail($car_repair_mechanic_post_id, $car_repair_mechanic_image_id);
                }
                // Save post ID in theme mod
                set_theme_mod('car_repair_mechanic_video_post' . $car_repair_mechanic_i, $car_repair_mechanic_post_id);
            }
        }

        //Service Section

        set_theme_mod( 'car_repair_mechanic_show_service_section', true);
        set_theme_mod( 'car_repair_mechanic_section_title', 'Our Services' );
        set_theme_mod( 'car_repair_mechanic_section_text', 'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Animi, laboriosam' );

        // Set the theme mod for the default category
        set_theme_mod('car_repair_mechanic_services_category', 'postcategory1');

        // Define post category names and post titles
        $car_repair_mechanic_category_names = array('postcategory1', 'postcategory2', 'postcategory3');
        $car_repair_mechanic_title_array = array(
            array("Steering & Suspension", "Tire & Wheel", "Oil Change", "Brake Service"),
            array("Steering & Suspension", "Tire & Wheel", "Oil Change", "Brake Service"),
            array("Steering & Suspension", "Tire & Wheel", "Oil Change", "Brake Service")
        );

        $car_repair_mechanic_content_array = array(
            array(
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            ),
            array(
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            ),
            array(
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet", 
                "Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque delectus, quidem id ratione consequatur eveniet"
            )
        );

        foreach ($car_repair_mechanic_category_names as $car_repair_mechanic_index => $car_repair_mechanic_category_name) {
            // Create or retrieve the post category term ID
            $car_repair_mechanic_category_term = term_exists($car_repair_mechanic_category_name, 'category');
            if (!$car_repair_mechanic_category_term) {
                $car_repair_mechanic_category_term = wp_insert_term($car_repair_mechanic_category_name, 'category');
            }

            if (is_wp_error($car_repair_mechanic_category_term)) {
                error_log('Error creating category: ' . $car_repair_mechanic_category_term->get_error_message());
                continue; // Skip if category creation fails
            }

            $car_repair_mechanic_category_id = isset($car_repair_mechanic_category_term['term_id']) ? (int)$car_repair_mechanic_category_term['term_id'] : $car_repair_mechanic_category_term;

            for ($car_repair_mechanic_i = 0; $car_repair_mechanic_i < 4; $car_repair_mechanic_i++) {
                // Ensure the required data exists
                if (!isset($car_repair_mechanic_title_array[$car_repair_mechanic_index][$car_repair_mechanic_i]) || 
                    !isset($car_repair_mechanic_content_array[$car_repair_mechanic_index][$car_repair_mechanic_i])) {
                    continue; // Skip if title or content is missing
                }

                // Create the post
                $car_repair_mechanic_post_data = array(
                    'post_title'    => wp_strip_all_tags($car_repair_mechanic_title_array[$car_repair_mechanic_index][$car_repair_mechanic_i]),
                    'post_content'  => $car_repair_mechanic_content_array[$car_repair_mechanic_index][$car_repair_mechanic_i],
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Default WordPress post type
                );
                $car_repair_mechanic_post_id = wp_insert_post($car_repair_mechanic_post_data);

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

                // Assign category to the post
                wp_set_post_categories($car_repair_mechanic_post_id, array($car_repair_mechanic_category_id));

                // Handle the featured image
                $car_repair_mechanic_image_url = get_template_directory_uri() . '/images/event' . ($car_repair_mechanic_i + 1) . '.png';
                $car_repair_mechanic_image_id = media_sideload_image($car_repair_mechanic_image_url, $car_repair_mechanic_post_id, null, 'id');

                if (is_wp_error($car_repair_mechanic_image_id)) {
                    error_log('Error downloading image: ' . $car_repair_mechanic_image_id->get_error_message());
                    continue; // Skip if image download fails
                }

                // Set the featured image
                set_post_thumbnail($car_repair_mechanic_post_id, $car_repair_mechanic_image_id);
            }
        }

        foreach ($car_repair_mechanic_category_names as $car_repair_mechanic_index => $car_repair_mechanic_category_name) {
            // Create or retrieve the post category term ID
            $car_repair_mechanic_term = term_exists($car_repair_mechanic_category_name, 'category');
            if ($car_repair_mechanic_term === 0 || $car_repair_mechanic_term === null) {
                // If the term does not exist, create it
                $car_repair_mechanic_term = wp_insert_term($car_repair_mechanic_category_name, 'category');
            }
            if (is_wp_error($car_repair_mechanic_term)) {
                error_log('Error creating category: ' . $car_repair_mechanic_term->get_error_message());
                continue; // Skip to the next iteration if category creation fails
            }
        
            for ($car_repair_mechanic_i = 0; $car_repair_mechanic_i < 3; $car_repair_mechanic_i++) {
                // Create post content
                $car_repair_mechanic_title = $car_repair_mechanic_title_array[$car_repair_mechanic_index][$car_repair_mechanic_i];
                $car_repair_mechanic_content = $car_repair_mechanic_content_array[$car_repair_mechanic_index][$car_repair_mechanic_i];
        
                // Create post post object
                $car_repair_mechanic_my_post = array(
                    'post_title'    => wp_strip_all_tags($car_repair_mechanic_title),
                    'post_content'  => $car_repair_mechanic_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Post type set to 'post'
                );
        
                // Insert the post into the database
                $car_repair_mechanic_post_id = wp_insert_post($car_repair_mechanic_my_post);
        
                if (is_wp_error($car_repair_mechanic_post_id)) {
                    error_log('Error creating post: ' . $car_repair_mechanic_post_id->get_error_message());
                    continue; // Skip to the next post if creation fails
                }
        
                // Assign the category to the post
                wp_set_post_categories($car_repair_mechanic_post_id, array((int)$car_repair_mechanic_term['term_id']));
        
                // Handle the featured image using media_sideload_image
                $car_repair_mechanic_image_url = get_template_directory_uri() . '/images/event' . ($car_repair_mechanic_i + 1) . '.png';
                $car_repair_mechanic_image_id = media_sideload_image($car_repair_mechanic_image_url, $car_repair_mechanic_post_id, null, 'id');
        
                if (is_wp_error($car_repair_mechanic_image_id)) {
                    error_log('Error downloading image: ' . $car_repair_mechanic_image_id->get_error_message());
                    continue; // Skip to the next post if image download fails
                }
        
                // Assign featured image to post
                set_post_thumbnail($car_repair_mechanic_post_id, $car_repair_mechanic_image_id);

            }
        }

        //Copyright Text
        set_theme_mod( 'car_repair_mechanic_footer_text', 'By Themesglance' );

        }
    ?>

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