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

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

            // Check if KK Star Ratings plugin is installed and active
            if ( !is_plugin_active('kk-star-ratings/index.php') ) {
                $kk_plugin_slug = 'kk-star-ratings';
                $kk_plugin_file = 'kk-star-ratings/index.php';

                // Load plugin install/activate functions
                include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
                include_once ABSPATH . 'wp-admin/includes/file.php';
                include_once ABSPATH . 'wp-admin/includes/misc.php';
                include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

                // Check if plugin is already downloaded
                $installed_plugins = get_plugins();
                if ( !isset($installed_plugins[$kk_plugin_file]) ) {
                    // Download and install the plugin
                    $upgrader = new Plugin_Upgrader();
                    $upgrader->install('https://downloads.wordpress.org/plugin/kk-star-ratings.latest-stable.zip');
                }

                // Activate the plugin
                activate_plugin($kk_plugin_file);
            }

            // ------- Create Nav Menu --------
            $jungle_safari_menuname = 'Main Menus';
            $jungle_safari_bpmenulocation = 'primary';
            $jungle_safari_menu_exists = wp_get_nav_menu_object($jungle_safari_menuname);

            if (!$jungle_safari_menu_exists) {
                $jungle_safari_menu_id = wp_create_nav_menu($jungle_safari_menuname);

                // Create Home Page
                $jungle_safari_home_title = 'Home';
                $jungle_safari_home = array(
                    'post_type' => 'page',
                    'post_title' => $jungle_safari_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $jungle_safari_home_id = wp_insert_post($jungle_safari_home);
                // Assign Home Page Template
                add_post_meta($jungle_safari_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', $jungle_safari_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($jungle_safari_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'jungle-safari'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $jungle_safari_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $jungle_safari_about_title = 'About Us';
                $jungle_safari_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.';
                $jungle_safari_about = array(
                    'post_type' => 'page',
                    'post_title' => $jungle_safari_about_title,
                    'post_content' => $jungle_safari_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $jungle_safari_about_id = wp_insert_post($jungle_safari_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($jungle_safari_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'jungle-safari'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $jungle_safari_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $jungle_safari_blog_page_title = 'Blog';
                $jungle_safari_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($jungle_safari_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$jungle_safari_blog_page_query->have_posts()) {
                    $jungle_safari_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $jungle_safari_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $jungle_safari_blog_page_id = wp_insert_post($jungle_safari_blog_page);
                    update_option('page_for_posts', $jungle_safari_blog_page_id);

                    wp_update_nav_menu_item($jungle_safari_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'jungle-safari'),
                    'menu-item-url'        => get_permalink($jungle_safari_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $jungle_safari_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Safaris Page with Dummy Content
                $jungle_safari_about_title = 'Safaris';
                $jungle_safari_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.';
                $jungle_safari_about = array(
                    'post_type' => 'page',
                    'post_title' => $jungle_safari_about_title,
                    'post_content' => $jungle_safari_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'safaris'
                );
                $jungle_safari_about_id = wp_insert_post($jungle_safari_about);
                // Add Safaris Page to Menu
                wp_update_nav_menu_item($jungle_safari_menu_id, 0, array(
                    'menu-item-title' => __('Safaris', 'jungle-safari'),
                    'menu-item-classes' => 'safaris',
                    'menu-item-url' => home_url('/safaris/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $jungle_safari_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                 // Create Gallery Page with Dummy Content
                $jungle_safari_about_title = 'Gallery';
                $jungle_safari_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.';
                $jungle_safari_about = array(
                    'post_type' => 'page',
                    'post_title' => $jungle_safari_about_title,
                    'post_content' => $jungle_safari_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'gallery'
                );
                $jungle_safari_about_id = wp_insert_post($jungle_safari_about);
                // Add Gallery Page to Menu
                wp_update_nav_menu_item($jungle_safari_menu_id, 0, array(
                    'menu-item-title' => __('Gallery', 'jungle-safari'),
                    'menu-item-classes' => 'gallery',
                    'menu-item-url' => home_url('/gallery/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $jungle_safari_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $jungle_safari_contact_title = 'Contact Us';
                $jungle_safari_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.';
                $jungle_safari_contact = array(
                    'post_type' => 'page',
                    'post_title' => $jungle_safari_contact_title,
                    'post_content' => $jungle_safari_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $jungle_safari_contact_id = wp_insert_post($jungle_safari_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($jungle_safari_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'jungle-safari'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $jungle_safari_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($jungle_safari_bpmenulocation)) {
                    $jungle_safari_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($jungle_safari_locations)) {
                        $jungle_safari_locations = array();
                    }
                    $jungle_safari_locations[$jungle_safari_bpmenulocation] = $jungle_safari_menu_id;
                    set_theme_mod('nav_menu_locations', $jungle_safari_locations);
                }
            }

            //Header
            set_theme_mod('jungle_safari_header_btn_text', 'Book Now');
            set_theme_mod('jungle_safari_header_btn_url', '#');

            set_theme_mod('jungle_safari_banner_hide', true);
            set_theme_mod('jungle_safari_banner_small_title', 'Wildscape');
            set_theme_mod('jungle_safari_banner_title', 'Journey Deep Into The Wild Unknown');
            set_theme_mod('jungle_safari_banner_desc_text', 'Explore breathtaking landscapes, encounte majestic wildlife, and experience unforgettable moments on our expertly guided safaris designed for nature lovers and advanturers ');
            set_theme_mod('jungle_safari_banner_btn_text', 'Book Now');
            set_theme_mod('jungle_safari_banner_btn_url', '#');
            set_theme_mod('jungle_safari_video_button_url', '#');
            set_theme_mod('jungle_safari_video_bg_img', get_template_directory_uri() . '/images/video-bg-img.png');
            set_theme_mod('jungle_safari_banner_bottom_image1',  get_template_directory_uri() . '/images/bottom-img1.jpg');
            set_theme_mod('jungle_safari_banner_bottom_image2',  get_template_directory_uri() . '/images/bottom-img2.jpg');
            set_theme_mod('jungle_safari_banner_side_image',  get_template_directory_uri() . '/images/banner-img.png');

            // featured section
            set_theme_mod('jungle_safari_section_title', 'Featured safari tours');
            set_theme_mod('jungle_safari_section_description', 'Choose for unforgettable safari experiences tailored for every explorer and nature lover');
            set_theme_mod('jungle_safari_select_post_number', '4');

            $featured_titles = array(
                'Savannah Spirit Safari',
                'Elephant Trails',
                'Predators path',
                'Savannah Skies'
            );

            $featured_locations = array(
                'TANZANIA',
                'TANZANIA',
                'TANZANIA',
                'TANZANIA'
            );

            $featured_prices = array(
                '$2,389.99',
                '$2,389.99',
                '$2,389.99',
                '$2,389.99'
            );

            $featured_price_texts = array(
                'per person',
                'per person',
                'per person',
                'per person'
            );

            // Loop through and create 4 demo posts
            for ($i = 0; $i < 4; $i++) {

                // Create post
                $post_arr = array(
                    'post_title'   => $featured_titles[$i],
                    'post_content' => 'Chase herds, spot predators, and experience endless plains',
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                );

                $post_id = wp_insert_post($post_arr);

                if (!is_wp_error($post_id)) {
                    // Set featured image
                    $image_url = get_template_directory_uri() . '/images/featured' . ($i + 1) . '.jpg';
                    $image_id = media_sideload_image($image_url, $post_id, null, 'id');

                    if (!is_wp_error($image_id)) {
                        set_post_thumbnail($post_id, $image_id);
                    }

                    // Assign post to theme mod setting
                    set_theme_mod('jungle_safari_select_featured_post' . ($i + 1), $post_id);

                    // Assign safari meta: location, price, price text
                    set_theme_mod('jungle_safari_add_safari_location' . ($i + 1), $featured_locations[$i]);
                    set_theme_mod('jungle_safari_add_safari_price' . ($i + 1), $featured_prices[$i]);
                    set_theme_mod('jungle_safari_add_safari_price_text' . ($i + 1), $featured_price_texts[$i]);
                }
            }

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

        }
    ?>

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