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

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

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


         // Check if woocommerce is installed and activated
            if (!is_plugin_active('woocommerce/woocommerce.php')) {
                // Install the plugin if it doesn't exist
                $catering_lite_plugin_slug = 'woocommerce';
                $catering_lite_plugin_file = 'woocommerce/woocommerce.php';

                // Check if plugin is installed
                $catering_lite_installed_plugins = get_plugins();
                if (!isset($catering_lite_installed_plugins[$catering_lite_plugin_file])) {
                    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');

                    // Install the plugin
                    $catering_lite_upgrader = new Plugin_Upgrader();
                    $catering_lite_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($catering_lite_plugin_file);
            }

        // ------- Create Nav Menu --------
        $catering_lite_menuname = 'Main Menus';
        $catering_lite_bpmenulocation = 'primary';
        $catering_lite_menu_exists = wp_get_nav_menu_object($catering_lite_menuname);

        if (!$catering_lite_menu_exists) {
            $catering_lite_menu_id = wp_create_nav_menu($catering_lite_menuname);

        // Create Home Page
        $catering_lite_home_title = 'Home';
        $catering_lite_home = array(
            'post_type' => 'page',
            'post_title' => $catering_lite_home_title,
            'post_content' => '',
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'home'
        );
        $catering_lite_home_id = wp_insert_post($catering_lite_home);
        // Assign Home Page Template
        add_post_meta($catering_lite_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', $catering_lite_home_id);
        update_option('show_on_front', 'page');
        // Add Home Page to Menu
        wp_update_nav_menu_item($catering_lite_menu_id, 0, array(
            'menu-item-title' => __('Home', 'catering-lite'),
            'menu-item-classes' => 'home',
            'menu-item-url' => home_url('/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $catering_lite_home_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

        // Create Menu Page with Dummy Content
        $catering_lite_about_title = 'Menu';
        $catering_lite_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.';
        $catering_lite_about = array(
            'post_type' => 'page',
            'post_title' => $catering_lite_about_title,
            'post_content' => $catering_lite_about_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'about-us'
        );
        $catering_lite_about_id = wp_insert_post($catering_lite_about);
        // Add Menu Page to Menu
        wp_update_nav_menu_item($catering_lite_menu_id, 0, array(
            'menu-item-title' => __('Menu', 'catering-lite'),
            'menu-item-classes' => 'about-us',
            'menu-item-url' => home_url('/about-us/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $catering_lite_about_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

        // Truck Locator Page
        $catering_lite_services_title = 'Truck Locator';
        $catering_lite_services_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.';
        $catering_lite_services_id = wp_insert_post( array(
            'post_type'    => 'page',
            'post_title'   => $catering_lite_services_title,
            'post_content' => $catering_lite_services_content,
            'post_name'    => 'services',
            'post_status'  => 'publish',
            'post_author'  => 1
        ) );
        wp_update_nav_menu_item( $catering_lite_menu_id, 0, array(
            'menu-item-title'     => 'Truck Locator',
            'menu-item-object-id' => $catering_lite_services_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
            'menu-item-status'    => 'publish'
        ) );

        // Our Offer Page
        $catering_lite_works_title = 'Our Offer';
        $catering_lite_works_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.';
        $catering_lite_works_id = wp_insert_post( array(
            'post_type'    => 'page',
            'post_title'   => 'Our Offer',
            'post_content' => $catering_lite_works_content,
            'post_name'    => 'works',
            'post_status'  => 'publish',
            'post_author'  => 1
        ) );
        wp_update_nav_menu_item( $catering_lite_menu_id, 0, array(
            'menu-item-title'     => $catering_lite_works_title,
            'menu-item-object-id' => $catering_lite_works_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
            'menu-item-status'    => 'publish'
        ) );

         // Create Catering Service Page with Dummy Content
        $catering_lite_pages_title = 'Catering Service';
        $catering_lite_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.';
        $catering_lite_pages = array(
            'post_type' => 'page',
            'post_title' => $catering_lite_pages_title,
            'post_content' => $catering_lite_pages_content,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_slug' => 'pages'
        );
        $catering_lite_pages_id = wp_insert_post($catering_lite_pages);
        // Add Catering Service Page to Menu
        wp_update_nav_menu_item($catering_lite_menu_id, 0, array(
            'menu-item-title' => __('Catering Service', 'catering-lite'),
            'menu-item-classes' => 'pages',
            'menu-item-url' => home_url('/pages/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $catering_lite_pages_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

         // About Us Page
            $catering_lite_contact_title = 'About Us';
            $catering_lite_contact_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.';
            $catering_lite_contacts_id = wp_insert_post( array(
                'post_type'    => 'page',
                'post_title'   => $catering_lite_contact_title,
                'post_content' => $catering_lite_contact_content,
                'post_name'    => 'contacts',
                'post_status'  => 'publish',
                'post_author'  => 1
            ) );
            wp_update_nav_menu_item( $catering_lite_menu_id, 0, array(
                'menu-item-title'     => 'About Us',
                'menu-item-object-id' => $catering_lite_contacts_id,
                'menu-item-object'    => 'page',
                'menu-item-type'      => 'post_type',
                'menu-item-status'    => 'publish'
            ) );

         // Blog Recepie Page (Posts Page)
            $catering_lite_blog_id = wp_insert_post( array(
                'post_type'    => 'page',
                'post_title'   => 'Blog Recepie',
                'post_name'    => 'blog',
                'post_status'  => 'publish',
                'post_author'  => 1
            ) );
            wp_update_nav_menu_item( $catering_lite_menu_id, 0, array(
                'menu-item-title'     => 'Blog Recepie',
                'menu-item-object-id' => $catering_lite_blog_id,
                'menu-item-object'    => 'page',
                'menu-item-type'      => 'post_type',
                'menu-item-status'    => 'publish'
            ) );


        // Set the menu location if it's not already set
        if (!has_nav_menu($catering_lite_bpmenulocation)) {
            $catering_lite_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
            if (empty($catering_lite_locations)) {
                $catering_lite_locations = array();
            }
            $catering_lite_locations[$catering_lite_bpmenulocation] = $catering_lite_menu_id;
            set_theme_mod('nav_menu_locations', $catering_lite_locations);
        } 
        }    

        // Set front page and posts page
            update_option( 'page_on_front', $catering_lite_home_id );
            update_option( 'show_on_front', 'page' );
            update_option( 'page_for_posts', $catering_lite_blog_id );

        // Set the demo import completion flag
        update_option('catering_lite_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.', 'catering-lite') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="import-btn site-btn" target="_blank">' . esc_html__('VISIT SITE', 'catering-lite') . '</a></span>';
        echo '<span><a href="' . esc_url( admin_url('customize.php')) . '" class="import-btn site-btn" target="_blank">' . esc_html__('CUSTOMIZE', 'catering-lite') . '</a></span>';
        //end 

        //Topbar
        set_theme_mod( 'food_truck_lite_top_header', true ); 
        set_theme_mod( 'food_truck_lite_topbar_text', 'Available section for food truck' );
        set_theme_mod( 'food_truck_lite_call', '123-456-7890' ); 
        set_theme_mod( 'food_truck_lite_phone_icon',  "fas fa-phone pe-2" ); 

        // Header Button 
        set_theme_mod( 'food_truck_lite_topbar_button_text', 'Order Online' ); 
        set_theme_mod( 'food_truck_lite_topbar_button_link', "#" ); 

       //Slider
        set_theme_mod( 'food_truck_lite_show_slider', true );
        set_theme_mod( 'food_truck_lite_slider_button_label', 'Shop Now' );
        set_theme_mod( 'food_truck_lite_slider_button_link', '#' );
        set_theme_mod( 'food_truck_lite_slider_button_label2', 'View More' );
        set_theme_mod( 'food_truck_lite_slider_button_link2', '#' );
        
        $food_truck_lite_slider_titles = [
            'We Serve Quality Food',
            'Delicious Street Food, Anytime Anywhere',
            'Tasty Treats at Every Stop',
            'Your Favorite Dishes on Demand',
        ];   

        for($food_truck_lite_i=1;$food_truck_lite_i<=4;$food_truck_lite_i++){
            $food_truck_lite_slider_title = $food_truck_lite_slider_titles[$food_truck_lite_i - 1];
            $food_truck_lite_slider_content = 'Lorem Ipsum Food has been the heart of every culture since ancient times, when skilled chefs first blended simple ingredients into unforgettable flavors, creating dishes that brought people together around the table.';
            // Create post object
            $food_truck_lite_my_post = array(
                'post_title'    => wp_strip_all_tags( $food_truck_lite_slider_title ),
                'post_content'  => $food_truck_lite_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
            );

            // Insert the post into the database
            $food_truck_lite_post_id = wp_insert_post( $food_truck_lite_my_post );

            if ($food_truck_lite_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('food_truck_lite_slidersettings_page' . $food_truck_lite_i, $food_truck_lite_post_id);

                $food_truck_lite_image_url = get_template_directory_uri().'/images/slider'.$food_truck_lite_i.'.png';

                $food_truck_lite_image_id = media_sideload_image($food_truck_lite_image_url, $food_truck_lite_post_id, null, 'id');

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

        //Product Section
        set_theme_mod( 'catering_lite_small_title', 'Watch Our' );
        set_theme_mod( 'catering_lite_section_title', 'Video Recipe' );
        set_theme_mod( 'catering_lite_viewall_btn_text', 'View All videos' );
        set_theme_mod( 'catering_lite_viewall_btn_url', "#" );

        // Create or retrieve the category for "What we do"
        $catering_lite_category_name = 'Video Recipe';
        $catering_lite_category_term = term_exists($catering_lite_category_name, 'category');

        if (!$catering_lite_category_term) {
            $catering_lite_category_term = wp_insert_term($catering_lite_category_name, 'category');
        }

        if (!is_wp_error($catering_lite_category_term)) {
            $catering_lite_category_id = isset($catering_lite_category_term['term_id']) ? (int)$catering_lite_category_term['term_id'] : $catering_lite_category_term;

            // Link category to customizer setting
            set_theme_mod('catering_lite_recipe_category', $catering_lite_category_name);

            // Define demo posts (title + content)
            $catering_lite_posts = array(
                array(
                    'title'   => ' Colorfull Sandwich',
                    'content' => 'James colins.',
                    'image'   => get_theme_file_uri('/images/image-1.png'),
                ),
                array(
                    'title'   => 'Veg Noodles',
                    'content' => 'James colins',
                    'image'   => get_theme_file_uri('/images/image-4.png'),
                ),
                array(
                    'title'   => 'Fried Momos',
                    'content' => 'James colins',
                    'image'   => get_theme_file_uri('/images/image-2.png'),
                ),
                array(
                    'title'   => 'Manchurian',
                    'content' => 'James colins',
                    'image'   => get_theme_file_uri('/images/image-3.png'),
                ),
                array(
                    'title'   => 'Pasta',
                    'content' => 'James colins',
                    'image'   => get_theme_file_uri('/images/image-4.png'),
                ),
                array(
                    'title'   => 'Hukkka Noodles',
                    'content' => 'James colins',
                    'image'   => get_theme_file_uri('/images/image-5.png'),
                ),
            );

            // Create posts
            foreach ($catering_lite_posts as $catering_lite_post) {
                $post_id = wp_insert_post(array(
                    'post_title'   => wp_strip_all_tags($catering_lite_post['title']),
                    'post_content' => $catering_lite_post['content'],
                    'post_status'  => 'publish',
                    'post_type'    => 'post',
                ));

                if (!is_wp_error($post_id)) {
                    // Assign category
                    wp_set_post_categories($post_id, array($catering_lite_category_id));

                    // Add featured image
                    if (!empty($catering_lite_post['image'])) {
                        $image_id = media_sideload_image($catering_lite_post['image'], $post_id, null, 'id');
                        if (!is_wp_error($image_id)) {
                            set_post_thumbnail($post_id, $image_id);
                        }
                    }
                }
            }
        } else {
            error_log('Error creating What We Do category: ' . $catering_lite_category_term->get_error_message());
        }

       $catering_lite_post_title = "Your Favorite Dishes on Demand";
            $catering_lite_post_content = '<P>Delicious Street Food, Anytime Anywhere <br/></P>';
            $catering_lite_image_url = get_theme_file_uri() . '/images/image-3.png';

            // Create post object
            $catering_lite_post_data = array(
                'post_title'    => wp_strip_all_tags($catering_lite_post_title),
                'post_content'  => $catering_lite_post_content,
                'post_status'   => 'publish',
                'post_type'     => 'post', // Set post type to 'post'
            );
            set_theme_mod('catering_lite_video_post', $catering_lite_post_title);

            // Insert the post into the database
            $catering_lite_post_id = wp_insert_post($catering_lite_post_data);

            if (is_wp_error($catering_lite_post_id)) {
                error_log('Error creating post: ' . $catering_lite_post_id->get_error_message());
            } else {
                // Handle the featured image
                $catering_lite_image_id = media_sideload_image($catering_lite_image_url, $catering_lite_post_id, null, 'id');

                if (is_wp_error($catering_lite_image_id)) {
                    error_log('Error downloading image: ' . $catering_lite_image_id->get_error_message());
                } else {
                    // Assign featured image to post
                    set_post_thumbnail($catering_lite_post_id, $catering_lite_image_id);
                }
            }

            //Copyright Text
            set_theme_mod( 'catering_lite_footer_text', 'By Themesglance' );
    }
    ?>

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