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

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

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

            // ------- Create Nav Menu --------
            $medical_doctor_menuname = 'Main Menus';
            $medical_doctor_bpmenulocation = 'primary';
            $medical_doctor_menu_exists = wp_get_nav_menu_object($medical_doctor_menuname);

            if (!$medical_doctor_menu_exists) {
                $medical_doctor_menu_id = wp_create_nav_menu($medical_doctor_menuname);

                // Create Home Page
                $medical_doctor_home_title = 'Home';
                $medical_doctor_home = array(
                    'post_type' => 'page',
                    'post_title' => $medical_doctor_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $medical_doctor_home_id = wp_insert_post($medical_doctor_home);
                // Assign Home Page Template
                add_post_meta($medical_doctor_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', $medical_doctor_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($medical_doctor_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'medical-doctor'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $medical_doctor_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $medical_doctor_pages_title = 'Pages';
                $medical_doctor_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.';
                $medical_doctor_pages = array(
                    'post_type' => 'page',
                    'post_title' => $medical_doctor_pages_title,
                    'post_content' => $medical_doctor_pages_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'pages'
                );
                $medical_doctor_pages_id = wp_insert_post($medical_doctor_pages);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($medical_doctor_menu_id, 0, array(
                    'menu-item-title' => __('Pages', 'medical-doctor'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url' => home_url('/pages/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $medical_doctor_pages_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

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

        // Topbar settings
        set_theme_mod( 'medical_doctor_top_header', true );
        set_theme_mod('medical_doctor_booking_text', 'Book Online');
        set_theme_mod('medical_doctor_booking_url', '#');
        set_theme_mod('medical_doctor_topbar_text', 'You can request appointment in 24 hours');
        set_theme_mod('medical_doctor_call', '+012 345 6789');

        //Header
        set_theme_mod('medical_doctor_topbar_button_text', 'Make Appointment');
        set_theme_mod('medical_doctor_topbar_button_link', '#');

        //Social Icon
        set_theme_mod('medical_doctor_facebook_link', '#');
        set_theme_mod('medical_doctor_linkedin_link', '#');
        set_theme_mod('medical_doctor_twitter_link', '#');
        set_theme_mod('medical_doctor_intagram_link', '#');

        //Slider
        set_theme_mod( 'medical_doctor_show_slider', true );
        set_theme_mod( 'medical_doctor_slider_small_title', 'COMMIT TO BE FIT' );
        set_theme_mod( 'medical_doctor_slider_button_label', 'READ MORE' );
        set_theme_mod( 'medical_doctor_slider_button_link', '#' );
        set_theme_mod( 'medical_doctor_slider_button_label2', 'CONTACT US' );
        set_theme_mod( 'medical_doctor_slider_button_link2', '#' );

        $medical_doctor_slider_titles = [
            'Healthy Habits Are Your Heart\'s Desire',
            'Your Health, Our Commitment to Care',
            'Building a Healthier Future Together',
            'Expert Care for Every Stage of Life',
        ];        
        for($medical_doctor_i=1;$medical_doctor_i<=4;$medical_doctor_i++){
            $medical_doctor_slider_title = $medical_doctor_slider_titles[$medical_doctor_i - 1];
            $medical_doctor_slider_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s';
            // Create post object
            $medical_doctor_my_post = array(
            'post_title'    => wp_strip_all_tags( $medical_doctor_slider_title ),
            'post_content'  => $medical_doctor_slider_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
            );

            // Insert the post into the database
            $medical_doctor_post_id = wp_insert_post( $medical_doctor_my_post );

            if ($medical_doctor_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('medical_doctor_slidersettings_page' . $medical_doctor_i, $medical_doctor_post_id);

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

                $medical_doctor_image_id = media_sideload_image($medical_doctor_image_url, $medical_doctor_post_id, null, 'id');

                    if (!is_wp_error($medical_doctor_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($medical_doctor_post_id, $medical_doctor_image_id);
                    }
            }
        } 
        
        //Services Section
        set_theme_mod( 'medical_doctor_show_service_section', true );
        set_theme_mod( 'medical_doctor_small_title', 'HOW WE HELP' );
        set_theme_mod( 'medical_doctor_section_title', 'Departments & Core-Services' );

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

        // Define post category names and post titles
        $medical_doctor_category_names = ['postcategory1', 'postcategory2', 'postcategory3'];
        $medical_doctor_title_array = [
            ["Cardiology", "Gastroenterologist", "Neurology"],
            ["Cardiology", "Gastroenterologist", "Neurology"],
            ["Cardiology", "Gastroenterologist", "Neurology"]
        ];

        $medical_doctor_content_array = [
            [
                "We care for the growing needs of our community. we build systems.", 
                "We care for the growing needs of our community. we build systems.", 
                "We care for the growing needs of our community. we build systems."
            ],
            [
                "We care for the growing needs of our community. we build systems.", 
                "We care for the growing needs of our community. we build systems.", 
                "We care for the growing needs of our community. we build systems."
            ],
            [
                "We care for the growing needs of our community. we build systems.", 
                "We care for the growing needs of our community. we build systems.", 
                "We care for the growing needs of our community. we build systems."
            ]
        ];

        foreach ($medical_doctor_category_names as $medical_doctor_index => $medical_doctor_category_name) {
            // Create or retrieve the post category term ID
            $medical_doctor_category_term = term_exists($medical_doctor_category_name, 'category');
            if (!$medical_doctor_category_term) {
                $medical_doctor_category_term = wp_insert_term($medical_doctor_category_name, 'category');
            }

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

            $medical_doctor_category_id = isset($medical_doctor_category_term['term_id']) ? (int)$medical_doctor_category_term['term_id'] : $medical_doctor_category_term;

            for ($medical_doctor_i = 0; $medical_doctor_i < 4; $medical_doctor_i++) {
                // Ensure the required data exists
                if (empty($medical_doctor_title_array[$medical_doctor_index][$medical_doctor_i]) || 
                    empty($medical_doctor_content_array[$medical_doctor_index][$medical_doctor_i])) {
                    continue; // Skip if title or content is missing
                }

                // Create the post
                $medical_doctor_post_data = [
                    'post_title'    => wp_strip_all_tags($medical_doctor_title_array[$medical_doctor_index][$medical_doctor_i]),
                    'post_content'  => $medical_doctor_content_array[$medical_doctor_index][$medical_doctor_i],
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Default WordPress post type
                ];
                $medical_doctor_post_id = wp_insert_post($medical_doctor_post_data);

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

                // Assign category to the post
                wp_set_post_categories($medical_doctor_post_id, [$medical_doctor_category_id]);

                // Handle the featured image
                $medical_doctor_image_url = get_template_directory_uri() . '/images/services' . ($medical_doctor_i + 1) . '.png';
                $medical_doctor_image_id = media_sideload_image($medical_doctor_image_url, $medical_doctor_post_id, null, 'id');

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

                // Set the featured image
                set_post_thumbnail($medical_doctor_post_id, $medical_doctor_image_id);
            }
        }

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

        }
    ?>

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

