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

        error_reporting(0);

        // Check if the demo import has been completed
        $fast_food_truck_demo_import_completed = get_option('fast_food_truck_demo_import_completed', false);

        // If the demo import is completed, display the "View Site" button
        if ($fast_food_truck_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'fast-food-truck') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'fast-food-truck') . '</a></span>';
        }
	
        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
                $fast_food_truck_plugin_slug = 'woocommerce';
                $fast_food_truck_plugin_file = 'woocommerce/woocommerce.php';

                // Check if plugin is installed
                $fast_food_truck_installed_plugins = get_plugins();
                if (!isset($fast_food_truck_installed_plugins[$fast_food_truck_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
                    $fast_food_truck_upgrader = new Plugin_Upgrader();
                    $fast_food_truck_upgrader->install('https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($fast_food_truck_plugin_file);
            }

            // ------- Create Nav Menu --------
            $fast_food_truck_menuname = 'Main Menus';
            $fast_food_truck_bpmenulocation = 'primary';
            $fast_food_truck_menu_exists = wp_get_nav_menu_object($fast_food_truck_menuname);

            if (!$fast_food_truck_menu_exists) {
                $fast_food_truck_menu_id = wp_create_nav_menu($fast_food_truck_menuname);

                // Create Home Page
                $fast_food_truck_home_title = 'Home';
                $fast_food_truck_home = array(
                    'post_type' => 'page',
                    'post_title' => $fast_food_truck_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $fast_food_truck_home_id = wp_insert_post($fast_food_truck_home);
                // Assign Home Page Template
                add_post_meta($fast_food_truck_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', $fast_food_truck_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($fast_food_truck_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'fast-food-truck'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $fast_food_truck_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $fast_food_truck_about_title = 'About Us';
                $fast_food_truck_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.';
                $fast_food_truck_about = array(
                    'post_type' => 'page',
                    'post_title' => $fast_food_truck_about_title,
                    'post_content' => $fast_food_truck_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $fast_food_truck_about_id = wp_insert_post($fast_food_truck_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($fast_food_truck_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'fast-food-truck'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $fast_food_truck_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Pages Page with Dummy Content
                $fast_food_truck_shop_title = 'Shop';
                $fast_food_truck_shop_content = 'Explore all the shop 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 shoply 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.';
                $fast_food_truck_shop = array(
                    'post_type' => 'page',
                    'post_title' => $fast_food_truck_shop_title,
                    'post_content' => $fast_food_truck_shop_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'shop'
                );
                $fast_food_truck_shop_id = wp_insert_post($fast_food_truck_shop);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($fast_food_truck_menu_id, 0, array(
                    'menu-item-title' => __('Shop', 'fast-food-truck'),
                    'menu-item-classes' => 'shop',
                    'menu-item-url' => home_url('/shop/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $fast_food_truck_shop_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

               // Create Blog Page 
                $fast_food_truck_blog_page_title = 'Blog';
                $fast_food_truck_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($fast_food_truck_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$fast_food_truck_blog_page_query->have_posts()) {
                    $fast_food_truck_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $fast_food_truck_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $fast_food_truck_blog_page_id = wp_insert_post($fast_food_truck_blog_page);
                    update_option('page_for_posts', $fast_food_truck_blog_page_id);
                    wp_update_nav_menu_item($fast_food_truck_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'fast-food-truck'),
                    'menu-item-url'        => get_permalink($fast_food_truck_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $fast_food_truck_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                   ));
                }

                // Create Pages Page with Dummy Content
                $fast_food_truck_contact_title = 'Contact Us';
                $fast_food_truck_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.';
                $fast_food_truck_contact = array(
                    'post_type' => 'page',
                    'post_title' => $fast_food_truck_contact_title,
                    'post_content' => $fast_food_truck_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $fast_food_truck_contact_id = wp_insert_post($fast_food_truck_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($fast_food_truck_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'fast-food-truck'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $fast_food_truck_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($fast_food_truck_bpmenulocation)) {
                    $fast_food_truck_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($fast_food_truck_locations)) {
                        $fast_food_truck_locations = array();
                    }
                    $fast_food_truck_locations[$fast_food_truck_bpmenulocation] = $fast_food_truck_menu_id;
                    set_theme_mod('nav_menu_locations', $fast_food_truck_locations);
                }
            }

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

            //Header
            set_theme_mod( 'fast_food_truck_phone_text', 'For Delivery, Call Us' );
            set_theme_mod( 'fast_food_truck_phone_number', '+00 123 456 7890' );
            set_theme_mod( 'fast_food_truck_header_btn_text', 'ORDER HERE' );
            set_theme_mod( 'fast_food_truck_header_btn_url', '#' );

            //Social Icon
            set_theme_mod( 'fast_food_truck_facebook_url', 'https://www.facebook.com' );
            set_theme_mod( 'fast_food_truck_twitter_url', 'https://www.twitter.com' );
            set_theme_mod( 'fast_food_truck_youtube_url', 'https://www.youtube.com' );
            set_theme_mod( 'fast_food_truck_vimeo_url', 'https://www.vimeo.com' );

            //Banner Section
            set_theme_mod( 'fast_food_truck_banner_bg_image', esc_url( get_template_directory_uri().'/images/banner.png'));
            set_theme_mod( 'fast_food_truck_banner_title', 'Savor the Road: Culinary Adventures on Wheels!' );
            set_theme_mod( 'fast_food_truck_banner_text', 'Discover delectable flavors that travel with you, one bite at a time.' );
            set_theme_mod( 'fast_food_truck_banner_btn_text', 'ORDER NOW' );
            set_theme_mod( 'fast_food_truck_banner_btn_url', '#' );

            set_theme_mod('fast_food_truck_product_page', 'productcategory1');

            // Define product category names, product titles, and tags
            $fast_food_truck_category_names = array('productcategory1', 'productcategory2', 'productcategory3');
            $fast_food_truck_title_array = array(
                array("Crispy Crust Pizza", "Oven Fired Delights", "Pizzazz Pias"),
                array("Crispy Crust Pizza", "Oven Fired Delights", "Pizzazz Pias"),
                array("Crispy Crust Pizza", "Oven Fired Delights", "Pizzazz Pias")
            );

            foreach ($fast_food_truck_category_names as $fast_food_truck_index => $fast_food_truck_category_name) {
                // Create or retrieve the product category term ID
                $fast_food_truck_term = term_exists($fast_food_truck_category_name, 'product_cat');
                if ($fast_food_truck_term === 0 || $fast_food_truck_term === null) {
                    // If the term does not exist, create it
                    $fast_food_truck_term = wp_insert_term($fast_food_truck_category_name, 'product_cat');
                }

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

                // Loop to create 4 products for each category
                for ($fast_food_truck_i = 0; $fast_food_truck_i < 3; $fast_food_truck_i++) {
                    // Create product content
                    $fast_food_truck_title = $fast_food_truck_title_array[$fast_food_truck_index][$fast_food_truck_i];
                    $fast_food_truck_content = 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.';

                    // Create product post object
                    $fast_food_truck_my_post = array(
                        'post_title'    => wp_strip_all_tags($fast_food_truck_title),
                        'post_content'  => $fast_food_truck_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'product', // Post type set to 'product'
                    );

                    // Insert the product into the database
                    $fast_food_truck_post_id = wp_insert_post($fast_food_truck_my_post);

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

                    // Assign the category to the product
                    wp_set_object_terms($fast_food_truck_post_id, (int)$fast_food_truck_term['term_id'], 'product_cat');

                    // Set product as simple product and assign a price
                    update_post_meta($fast_food_truck_post_id, '_price', '15.99'); // Set a price for the product
                
                    // Handle the featured image using media_sideload_image
                    $fast_food_truck_image_url = get_template_directory_uri() . '/images/banner-dish' . ($fast_food_truck_i + 1) . '.png';
                    $fast_food_truck_image_id = media_sideload_image($fast_food_truck_image_url, $fast_food_truck_post_id, null, 'id');

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

                    // Assign featured image to product
                    set_post_thumbnail($fast_food_truck_post_id, $fast_food_truck_image_id);
                }
            }

            //WooCommerce Variable Product Section   
            set_theme_mod('fast_food_truck_product_section_hide', true);  
            set_theme_mod('fast_food_truck_section_title', 'Our Customers Top Picks');     
            set_theme_mod('fast_food_truck_products_section', 'category1');
        
            $fast_food_truck_category_names = array('category1', 'category2', 'category3');
            $fast_food_truck_title_array = array(
                array("Crispy Crust Pizza", "Oven Fired Delights", "Pizzazz Pias"),
                array("Crispy Crust Pizza", "Oven Fired Delights", "Pizzazz Pias"),
                array("Crispy Crust Pizza", "Oven Fired Delights", "Pizzazz Pias")
            );
            
            $fast_food_truck_product_details = array(
                'regular_price' => 17,
                'price' => 15.99,
                'size' => 'Medium',
                'crust' => 'New Hand Tossed'
            );
            
            foreach ($fast_food_truck_category_names as $fast_food_truck_index => $fast_food_truck_category_name) {
                // Create or retrieve the product category term ID
                $fast_food_truck_term = term_exists($fast_food_truck_category_name, 'product_cat');
                
                // If the term doesn't exist, create it
                if ($fast_food_truck_term === 0 || $fast_food_truck_term === null) {
                    $fast_food_truck_term = wp_insert_term($fast_food_truck_category_name, 'product_cat');
                }
                
                // Check for errors in category creation
                if (is_wp_error($fast_food_truck_term)) {
                    error_log('Error creating category: ' . $fast_food_truck_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }
                
                // Retrieve the category ID for assignment
                $fast_food_truck_term_id = $fast_food_truck_term['term_id'];
                
                // Loop to create products for each category
                for ($fast_food_truck_i = 0; $fast_food_truck_i < 3; $fast_food_truck_i++) {
                    // Create product content
                    $fast_food_truck_title = $fast_food_truck_title_array[$fast_food_truck_index][$fast_food_truck_i];
                    $fast_food_truck_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, quis nostrud exercitation ullamco laboris nis';
                
                    // Create product post object for the variable product
                    $fast_food_truck_my_post = array(
                        'post_title'    => wp_strip_all_tags($fast_food_truck_title),
                        'post_content'  => $fast_food_truck_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'product', // Post type set to 'product'
                    );
                
                    // Insert the variable product into the database
                    $fast_food_truck_post_id = wp_insert_post($fast_food_truck_my_post);
                
                    // Check for errors in product creation
                    if (is_wp_error($fast_food_truck_post_id)) {
                        error_log('Error creating product: ' . $fast_food_truck_post_id->get_error_message());
                        continue; // Skip to the next product if creation fails
                    }
                
                    // Assign the category to the product
                    wp_set_object_terms($fast_food_truck_post_id, array($fast_food_truck_term_id), 'product_cat');
                
                    // Add product attributes for Size and Crust
                    $fast_food_truck_attributes = array(
                        'size' => array(
                            'name' => 'Size',
                            'value' => 'Medium|Large|Extra Large',
                            'position' => 0,
                            'is_visible' => 1,
                            'is_variation' => 1,
                            'is_taxonomy' => 0,
                        ),
                        'crust' => array(
                            'name' => 'Crust',
                            'value' => 'New Hand Tossed|Thin Crust|Stuffed Crust',
                            'position' => 1,
                            'is_visible' => 1,
                            'is_variation' => 1,
                            'is_taxonomy' => 0,
                        ),
                    );
                
                    update_post_meta($fast_food_truck_post_id, '_product_attributes', $fast_food_truck_attributes);
                
                    // Set product type to variable
                    wp_set_object_terms($fast_food_truck_post_id, 'variable', 'product_type');
                
                    // Create variations for the variable product
                    $fast_food_truck_variations = array(
                        'size' => array('Medium', 'Large', 'Extra Large'),
                        'crust' => array('New Hand Tossed', 'Thin Crust', 'Stuffed Crust')
                    );
                
                    foreach ($fast_food_truck_variations['size'] as $fast_food_truck_size) {
                        foreach ($fast_food_truck_variations['crust'] as $fast_food_truck_crust) {
                            $fast_food_truck_variation_data = array(
                                'post_title' => $fast_food_truck_title . ' - ' . $fast_food_truck_size . ' - ' . $fast_food_truck_crust,
                                'post_name'  => 'product-' . $fast_food_truck_post_id . '-variation-' . sanitize_title($fast_food_truck_size . '-' . $fast_food_truck_crust),
                                'post_status' => 'publish',
                                'post_type'   => 'product_variation',
                                'post_parent' => $fast_food_truck_post_id,
                            );
                
                            // Insert the variation post
                            $fast_food_truck_variation_post_id = wp_insert_post($fast_food_truck_variation_data);
                
                            // Set variation attributes
                            update_post_meta($fast_food_truck_variation_post_id, 'attribute_size', $fast_food_truck_size);
                            update_post_meta($fast_food_truck_variation_post_id, 'attribute_crust', $fast_food_truck_crust);
                
                            // Set regular price for the variation
                            update_post_meta($fast_food_truck_variation_post_id, '_regular_price', $fast_food_truck_product_details['price']);
                            update_post_meta($fast_food_truck_variation_post_id, '_price', $fast_food_truck_product_details['price']);
                        }
                    }
                
                    // Handle the featured image using media_sideload_image
                    $fast_food_truck_image_url = get_template_directory_uri() . '/images/dish' . ($fast_food_truck_i + 1) . '.png';
                    $fast_food_truck_image_id = media_sideload_image($fast_food_truck_image_url, $fast_food_truck_post_id, null, 'id');
                
                    // Check if there was an error downloading the image
                    if (is_wp_error($fast_food_truck_image_id)) {
                        error_log('Error downloading image: ' . $fast_food_truck_image_id->get_error_message());
                        continue; // Skip to the next product if image download fails
                    }
                
                    // Assign featured image to product
                    set_post_thumbnail($fast_food_truck_post_id, $fast_food_truck_image_id);
                }
            }
                
            //Copyright Text
            set_theme_mod( 'fast_food_truck_footer_text', 'By Buywptemplate' );

        }
    ?>

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