<div class="theme-offer">
<?php

function jungle_eco_tours_create_customizer_nav_menu() {
    $jungle_eco_tours_menuname = 'Primary';
    $jungle_eco_tours_menulocation = 'primary';
    $jungle_eco_tours_menu_exists = wp_get_nav_menu_object($jungle_eco_tours_menuname);

    if (!$jungle_eco_tours_menu_exists) {
        $jungle_eco_tours_menu_id = wp_create_nav_menu($jungle_eco_tours_menuname);

        wp_update_nav_menu_item($jungle_eco_tours_menu_id, 0, array(
            'menu-item-title' => __('Home', 'jungle-eco-tours'),
            'menu-item-url' => home_url('/'),
            'menu-item-status' => 'publish',
        ));

        wp_update_nav_menu_item($jungle_eco_tours_menu_id, 0, array(
            'menu-item-title' => __('About Us', 'jungle-eco-tours'),
            'menu-item-url' => home_url('/index.php/about-us/'),
            'menu-item-status' => 'publish',
        ));

        wp_update_nav_menu_item($jungle_eco_tours_menu_id, 0, array(
            'menu-item-title' => __('Services', 'jungle-eco-tours'),
            'menu-item-url' => home_url('/index.php/services/'),
            'menu-item-status' => 'publish',
        ));

        wp_update_nav_menu_item($jungle_eco_tours_menu_id, 0, array(
            'menu-item-title' => __('Pages', 'jungle-eco-tours'),
            'menu-item-url' => home_url('/index.php/pages/'),
            'menu-item-status' => 'publish',
        ));

        wp_update_nav_menu_item($jungle_eco_tours_menu_id, 0, array(
            'menu-item-title' => __('Blog', 'jungle-eco-tours'),
            'menu-item-url' => home_url('/index.php/blog/'),
            'menu-item-status' => 'publish',
        ));

        $jungle_eco_tours_locations = get_theme_mod('nav_menu_locations');
        if (!is_array($jungle_eco_tours_locations)) {
            $jungle_eco_tours_locations = array();
        }
        $jungle_eco_tours_locations[$jungle_eco_tours_menulocation] = $jungle_eco_tours_menu_id;
        set_theme_mod('nav_menu_locations', $jungle_eco_tours_locations);
    }
}

if (isset($_POST['submit'])) {
    include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    include_once(ABSPATH . 'wp-admin/includes/plugin-install.php');
    include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
    include_once(ABSPATH . 'wp-admin/includes/file.php');
    include_once(ABSPATH . 'wp-admin/includes/misc.php');

    $jungle_eco_tours_plugins = array(
        array(
            'slug' => 'classic-widgets',
            'file' => 'classic-widgets/classic-widgets.php',
            'download_url' => 'https://downloads.wordpress.org/plugin/classic-widgets.zip'
        )
    );

    foreach ($jungle_eco_tours_plugins as $plugin) {
        $installed_plugins = get_plugins();
        if (!isset($installed_plugins[$plugin['file']])) {
            $upgrader = new Plugin_Upgrader();
            $upgrader->install($plugin['download_url']);
        }

        if (file_exists(WP_PLUGIN_DIR . '/' . $plugin['file']) && !is_plugin_active($plugin['file'])) {
            activate_plugin($plugin['file']);
        }
    }

    // Function to create a page if it doesn't exist
    function create_demo_page($title, $content = '', $template = '') {
        $page_id = jungle_eco_tours_get_page_id_by_title($title); // use existing function

        if (!$page_id) {
            $page_data = array(
                'post_type'    => 'page',
                'post_title'   => $title,
                'post_content' => $content,
                'post_status'  => 'publish',
                'post_author'  => 1,
            );

            $page_id = wp_insert_post($page_data);

            if ($template && !is_wp_error($page_id)) {
                update_post_meta($page_id, '_wp_page_template', $template);
            }
        }

        return $page_id;
    }

    // Create and assign pages
    $jungle_eco_tours_home_id = create_demo_page('Home', '', 'home/home.php');
    update_option('page_on_front', $jungle_eco_tours_home_id);
    update_option('show_on_front', 'page');

    create_demo_page('Pages', '<p>Lorem Ipsum ...</p>');
    create_demo_page('About Us', '<p>Lorem Ipsum ...</p>');
    create_demo_page('Services', '<p>Service description...</p>');

    // Create blog page and assign it to display posts
    $blog_page_id = create_demo_page('Blog');
    update_option('page_for_posts', $blog_page_id);

    // Create the nav menu
    jungle_eco_tours_create_customizer_nav_menu();

        // ------- Set Theme Mods --------
        
        set_theme_mod('jungle_eco_tours_social_media1_heading', 'www.facebook.com');
        set_theme_mod('jungle_eco_tours_social_media2_heading', 'www.instagram.com');
        set_theme_mod('jungle_eco_tours_social_media3_heading', 'www.twitter.com');
        set_theme_mod('jungle_eco_tours_social_media5_heading', 'www.pinterest.com');
        set_theme_mod('jungle_eco_tours_social_media4_heading', 'www.youtube.com');
        set_theme_mod('jungle_eco_tours_social_media6_heading', 'www.linkedin.com');

        // ------- Banner Section --------
        $jungle_eco_tours_banner_headings = array('WELCOME TO JUNGLE SAFARI', 'LION IS THE KING OF THE JUNGLE');
        set_theme_mod('jungle_eco_tours_slider_increase', 2);
        for ($i = 1; $i <= 2; $i++) {
            set_theme_mod("jungle_eco_tours_banner_image$i", get_template_directory_uri() . "/img/banner$i.png");
            set_theme_mod("jungle_eco_tours_banner_heading$i", $jungle_eco_tours_banner_headings[$i - 1]);
            set_theme_mod("jungle_eco_tours_banner_small_head$i", 'DISCOVER A WHOLE NEW WORLD OF WILDLIFE');
            set_theme_mod("jungle_eco_tours_banner_button_link$i", '#');
        }

        // ------- Services Section --------
        set_theme_mod('jungle_eco_tours_about_small_heading', 'About Us');
        set_theme_mod('jungle_eco_tours_about_main_heading', 'THE BEST SAFARI AND ADVENTURE EXPERIANCE');
        set_theme_mod('jungle_eco_tours_about_text1', '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..');
        set_theme_mod('jungle_eco_tours_about_text2', 'Dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur Dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur');
        set_theme_mod('jungle_eco_tours_about_left_image', get_template_directory_uri() . '/img/about1.png');
        set_theme_mod('jungle_eco_tours_about_right_image', get_template_directory_uri() . '/img/about2.png');

        
        echo '<div class="success">Demo Import Successful</div>';
    }
?>

<ul>
    <li>
        <hr>
        <?php if (!isset($_POST['submit'])) : ?>
            <?php echo esc_html__('Click on the below button to get demo content installed.', 'jungle-eco-tours'); ?>
            <br>
            <form id="demo-importer-form" action="" method="POST" onsubmit="return confirm('Do you really want to do this?');">
                <input class="run-btn" type="submit" name="submit" value="<?php echo esc_attr('Run Importer', 'jungle-eco-tours'); ?>">
            </form>
        <?php else: ?>
            <div class="visit">
                <a href="<?php echo esc_url(home_url()); ?>" class="button button-primary button-large run-btn" style="margin-top: 10px;" target="_blank">View Site</a>
            </div>
        <?php endif; ?>
        <hr>
    </li>
</ul>
</div>