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

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

            // ------- Create Nav Menu --------
            $gym_training_coach_menuname = 'Main Menus';
            $gym_training_coach_bpmenulocation = 'primary';
            $gym_training_coach_menu_exists = wp_get_nav_menu_object($gym_training_coach_menuname);

            if (!$gym_training_coach_menu_exists) {
                $gym_training_coach_menu_id = wp_create_nav_menu($gym_training_coach_menuname);

                // Create Home Page
                $gym_training_coach_home_title = 'Home';
                $gym_training_coach_home = array(
                    'post_type' => 'page',
                    'post_title' => $gym_training_coach_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $gym_training_coach_home_id = wp_insert_post($gym_training_coach_home);
                // Assign Home Page Template
                add_post_meta($gym_training_coach_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', $gym_training_coach_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($gym_training_coach_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'gym-training-coach'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $gym_training_coach_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $gym_training_coach_about_title = 'About Us';
                $gym_training_coach_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.';
                $gym_training_coach_about = array(
                    'post_type' => 'page',
                    'post_title' => $gym_training_coach_about_title,
                    'post_content' => $gym_training_coach_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $gym_training_coach_about_id = wp_insert_post($gym_training_coach_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($gym_training_coach_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'gym-training-coach'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $gym_training_coach_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $gym_training_coach_blog_page_title = 'Blog';
                $gym_training_coach_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($gym_training_coach_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$gym_training_coach_blog_page_query->have_posts()) {
                    $gym_training_coach_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $gym_training_coach_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $gym_training_coach_blog_page_id = wp_insert_post($gym_training_coach_blog_page);
                    update_option('page_for_posts', $gym_training_coach_blog_page_id);
                    wp_update_nav_menu_item($gym_training_coach_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'gym-training-coach'),
                    'menu-item-url'        => get_permalink($gym_training_coach_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $gym_training_coach_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                   ));
                }

                // Create Pages Page with Dummy Content
                $gym_training_coach_contact_title = 'Contact Us';
                $gym_training_coach_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.';
                $gym_training_coach_contact = array(
                    'post_type' => 'page',
                    'post_title' => $gym_training_coach_contact_title,
                    'post_content' => $gym_training_coach_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $gym_training_coach_contact_id = wp_insert_post($gym_training_coach_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($gym_training_coach_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'gym-training-coach'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $gym_training_coach_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($gym_training_coach_bpmenulocation)) {
                    $gym_training_coach_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($gym_training_coach_locations)) {
                        $gym_training_coach_locations = array();
                    }
                    $gym_training_coach_locations[$gym_training_coach_bpmenulocation] = $gym_training_coach_menu_id;
                    set_theme_mod('nav_menu_locations', $gym_training_coach_locations);
                }
            }

            //Header
            set_theme_mod('gym_training_coach_email_address', 'example@gmail.com');
            set_theme_mod('gym_training_coach_topbar_location', '77 Franklin st, San Francisco');
            set_theme_mod('gym_training_coach_header_btn_text', 'Call Us');
            set_theme_mod('gym_training_coach_phone_number', '+123 456 789');

            //Social Icons
            set_theme_mod('gym_training_coach_facebook_url', 'https://www.facebook.com');
            set_theme_mod('gym_training_coach_twitter_url', 'https://www.twitter.com');
            set_theme_mod('gym_training_coach_instagram_url', 'https://instagram.com');
            set_theme_mod('gym_training_coach_pinterest_url', 'https://www.pinterest.com');

            //Slider
            set_theme_mod('gym_training_coach_slider_hide', true);
            set_theme_mod('gym_training_coach_show_slider_icon', true);
            set_theme_mod('gym_training_coach_slider_small_title', 'STRONG IS BEAUTIFUL');
            set_theme_mod('gym_training_coach_slider_button_text', 'Get Started');

            for($gym_training_coach_i=1;$gym_training_coach_i<=4;$gym_training_coach_i++){
                $gym_training_coach_slider_title = array(
                    'FITNESS IS THE SHAPING OF BODY',
                    'STRENGTH IS THE KEY TO SUCCESS',
                    'TRAIN HARD, STAY STRONG',
                    'ACHIEVE YOUR GOALS WITH DEDICATION'
                );                
                $gym_training_coach_slider_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
               
                $gym_training_coach_current_title = $gym_training_coach_slider_title[$gym_training_coach_i - 1];
                // Create post object
                $gym_training_coach_my_post = array(
                'post_title'    => wp_strip_all_tags( $gym_training_coach_current_title ),
                'post_content'  => $gym_training_coach_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
                );

                // Insert the post into the database
                $gym_training_coach_post_id = wp_insert_post( $gym_training_coach_my_post );

                if ($gym_training_coach_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('gym_training_coach_slider' . $gym_training_coach_i, $gym_training_coach_post_id);

                $gym_training_coach_image_url = get_template_directory_uri().'/images/slider'.$gym_training_coach_i.'.png';

                $gym_training_coach_image_id = media_sideload_image($gym_training_coach_image_url, $gym_training_coach_post_id, null, 'id');

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

            //Classes Section
            set_theme_mod('gym_training_coach_product_enable', true);
            set_theme_mod('gym_training_coach_section_title', 'Our Classes');
            set_theme_mod('gym_training_coach_section_text', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.');
            set_theme_mod('gym_training_coach_events_category', 'postcategory1');

            // Define post category names and post titles
            $gym_training_coach_category_names = array('postcategory1', 'postcategory2', 'postcategory3', 'postcategory4');
            $gym_training_coach_title_array = array(
                array("Psycho Training", "Stength Training", "Advance Gym"),
                array("Psycho Training", "Stength Training", "Advance Gym"),
                array("Psycho Training", "Stength Training", "Advance Gym"),
                array("Psycho Training", "Stength Training", "Advance Gym")
            );

            $gym_training_coach_content_array = array(
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                ),
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                ),
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                ),
                array(
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
                )
            );

            foreach ($gym_training_coach_category_names as $gym_training_coach_index => $gym_training_coach_category_name) {
                // Create or retrieve the post category term ID
                $gym_training_coach_term = term_exists($gym_training_coach_category_name, 'category');
                if (!$gym_training_coach_term || is_wp_error($gym_training_coach_term)) {
                    $gym_training_coach_term = wp_insert_term($gym_training_coach_category_name, 'category');
                    if (is_wp_error($gym_training_coach_term)) {
                        error_log('Error creating category: ' . $gym_training_coach_term->get_error_message());
                        continue; // Skip to the next iteration if category creation fails
                    }
                }

                $gym_training_coach_term_id = is_array($gym_training_coach_term) ? $gym_training_coach_term['term_id'] : $gym_training_coach_term;

                for ($gym_training_coach_i = 0; $gym_training_coach_i < 3; $gym_training_coach_i++) {
                    // Create post content
                    $gym_training_coach_title = $gym_training_coach_title_array[$gym_training_coach_index][$gym_training_coach_i];
                    $gym_training_coach_content = $gym_training_coach_content_array[$gym_training_coach_index][$gym_training_coach_i];

                    // Create post object
                    $gym_training_coach_my_post = array(
                        'post_title'    => wp_strip_all_tags($gym_training_coach_title),
                        'post_content'  => $gym_training_coach_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $gym_training_coach_post_id = wp_insert_post($gym_training_coach_my_post);

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

                    // Assign the category to the post
                    wp_set_post_categories($gym_training_coach_post_id, array((int)$gym_training_coach_term_id));

                    // Handle the featured image using media_sideload_image
                    $gym_training_coach_image_url = get_template_directory_uri() . '/images/service' . ($gym_training_coach_i + 1) . '.png';
                    $gym_training_coach_image_id = media_sideload_image($gym_training_coach_image_url, $gym_training_coach_post_id, null, 'id');

                    if (is_wp_error($gym_training_coach_image_id)) {
                        error_log('Error downloading image: ' . $gym_training_coach_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }

                    // Assign featured image to post
                    set_post_thumbnail($gym_training_coach_post_id, $gym_training_coach_image_id);

                }
            }

            // Set the demo import completion flag
    		update_option('gym_training_coach_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.', 'gym-training-coach') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'gym-training-coach') . '</a></span>';
            //end

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=gym-training-coach-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('gym_training_coach_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','gym-training-coach'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','gym-training-coach'); ?>" 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>
