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

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

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

            // ------- Create Nav Menu --------
            $pet_rescue_lite_menuname = 'Main Menus';
            $pet_rescue_lite_bpmenulocation = 'primary';
            $pet_rescue_lite_menu_exists = wp_get_nav_menu_object($pet_rescue_lite_menuname);

            if (!$pet_rescue_lite_menu_exists) {
                $pet_rescue_lite_menu_id = wp_create_nav_menu($pet_rescue_lite_menuname);

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

                // Create About Us Page with Dummy Content
                $pet_rescue_lite_about_title = 'About Us';
                $pet_rescue_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.';
                $pet_rescue_lite_about = array(
                    'post_type' => 'page',
                    'post_title' => $pet_rescue_lite_about_title,
                    'post_content' => $pet_rescue_lite_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $pet_rescue_lite_about_id = wp_insert_post($pet_rescue_lite_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($pet_rescue_lite_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'pet-rescue-lite'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $pet_rescue_lite_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                /*Create Services Page*/
                $pet_rescue_lite_services_title = 'Services';
                $pet_rescue_lite_services_content = 'Discover the services we provide for pet rescue, adoption, and animal care.
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';

                $pet_rescue_lite_services = array(
                    'post_type'    => 'page',
                    'post_title'   => $pet_rescue_lite_services_title,
                    'post_content' => $pet_rescue_lite_services_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'services'
                );
                $pet_rescue_lite_services_id = wp_insert_post($pet_rescue_lite_services);

                // Add Services Page to Menu
                wp_update_nav_menu_item($pet_rescue_lite_menu_id, 0, array(
                    'menu-item-title'   => __('Services', 'pet-rescue-lite'),
                    'menu-item-classes' => 'services',
                    'menu-item-url'     => home_url('/services/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $pet_rescue_lite_services_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));


                /*Create Pages Page*/
                $pet_rescue_lite_pages_title = 'Pages';
                $pet_rescue_lite_pages_content = 'Browse all pages related to our pet rescue organization.
                Lorem Ipsum is simply dummy text of the printing and typesetting industry.';

                $pet_rescue_lite_pages = array(
                    'post_type'    => 'page',
                    'post_title'   => $pet_rescue_lite_pages_title,
                    'post_content' => $pet_rescue_lite_pages_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'pages'
                );
                $pet_rescue_lite_pages_id = wp_insert_post($pet_rescue_lite_pages);

                // Add Pages Page to Menu
                wp_update_nav_menu_item($pet_rescue_lite_menu_id, 0, array(
                    'menu-item-title'   => __('Pages', 'pet-rescue-lite'),
                    'menu-item-classes' => 'pages',
                    'menu-item-url'     => home_url('/pages/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $pet_rescue_lite_pages_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                // Create Blog Page 
                $pet_rescue_lite_blog_page_title = 'Blog';
                $pet_rescue_lite_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($pet_rescue_lite_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$pet_rescue_lite_blog_page_query->have_posts()) {
                    $pet_rescue_lite_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $pet_rescue_lite_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $pet_rescue_lite_blog_page_id = wp_insert_post($pet_rescue_lite_blog_page);
                    update_option('page_for_posts', $pet_rescue_lite_blog_page_id);
                    wp_update_nav_menu_item($pet_rescue_lite_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'pet-rescue-lite'),
                    'menu-item-url'        => get_permalink($pet_rescue_lite_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $pet_rescue_lite_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $pet_rescue_lite_contact_title = 'Contact Us';
                $pet_rescue_lite_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.';
                $pet_rescue_lite_contact = array(
                    'post_type' => 'page',
                    'post_title' => $pet_rescue_lite_contact_title,
                    'post_content' => $pet_rescue_lite_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $pet_rescue_lite_contact_id = wp_insert_post($pet_rescue_lite_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($pet_rescue_lite_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'pet-rescue-lite'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $pet_rescue_lite_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($pet_rescue_lite_bpmenulocation)) {
                    $pet_rescue_lite_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($pet_rescue_lite_locations)) {
                        $pet_rescue_lite_locations = array();
                    }
                    $pet_rescue_lite_locations[$pet_rescue_lite_bpmenulocation] = $pet_rescue_lite_menu_id;
                    set_theme_mod('nav_menu_locations', $pet_rescue_lite_locations);
                }
            }

            //Header
            set_theme_mod('pet_rescue_lite_topbar_text', 'Welcome to Pet Rescue Wordpress Theme');
            set_theme_mod('pet_rescue_lite_phone_number', '+00 123 456 7890');

            //Slider Section
            set_theme_mod('pet_rescue_lite_slider_hide', true);
            set_theme_mod('pet_rescue_lite_slider_small_title', 'WELCOME TO PET SHOP');
            set_theme_mod('pet_rescue_lite_slider_button_text', 'Discover More');

            for($pet_rescue_lite_i=1;$pet_rescue_lite_i<=3;$pet_rescue_lite_i++){
                $pet_rescue_lite_slider_title = array(
                    'We will take care of your pets',
                    'Dedicated to Their Health and Happiness',
                    'A Loving Home Away From Home',
                );                
                $pet_rescue_lite_current_title = $pet_rescue_lite_slider_title[$pet_rescue_lite_i - 1];
                $pet_rescue_lite_slider_content = 'Promoting them continuous improvement of human and technological resources generating competitive profitability';
                   // Create post object
                $pet_rescue_lite_my_post = array(
                'post_title'    => wp_strip_all_tags( $pet_rescue_lite_current_title ),
                'post_content'  => $pet_rescue_lite_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
                );
 
                // Insert the post into the database
                $pet_rescue_lite_post_id = wp_insert_post( $pet_rescue_lite_my_post );
 
                 if ($pet_rescue_lite_post_id) {
                  // Set the theme mod for the slider page
                  set_theme_mod('pet_rescue_lite_slider_setting' . $pet_rescue_lite_i, $pet_rescue_lite_post_id);
 
                   $pet_rescue_lite_image_url = get_template_directory_uri().'/images/slider'.$pet_rescue_lite_i.'.png';
 
                   $pet_rescue_lite_image_id = media_sideload_image($pet_rescue_lite_image_url, $pet_rescue_lite_post_id, null, 'id');
 
                     if (!is_wp_error($pet_rescue_lite_image_id)) {
                         // Set the downloaded image as the post's featured image
                         set_post_thumbnail($pet_rescue_lite_post_id, $pet_rescue_lite_image_id);
                     }
                 }
            }

            //Product Section
            set_theme_mod('pet_rescue_lite_product_enable', true);
            set_theme_mod('pet_rescue_lite_product_section_title', 'Pets For Adoption');
            set_theme_mod('pet_rescue_lite_product_slide', 'productcategory1');

            // Define product category names, product titles, and tags
            $pet_rescue_lite_category_names = array('productcategory1', 'productcategory2', 'productcategory3');
            $pet_rescue_lite_title_array = array(
                array("Lily", "Charlie", "Molly", "Neo"),
                array("Lily", "Charlie", "Molly", "Neo"),
                array("Lily", "Charlie", "Molly", "Neo")
            );

            // Define prices for the products
            $pet_rescue_lite_prices = array(
                450.00, 
                380.00, 
                520.00,
                350.00 
            );

            foreach ($pet_rescue_lite_category_names as $pet_rescue_lite_index => $pet_rescue_lite_category_name) {
                // Create or retrieve the product category term ID
                $pet_rescue_lite_term = term_exists($pet_rescue_lite_category_name, 'product_cat');

                // If the term doesn't exist, create it
                if ($pet_rescue_lite_term === 0 || $pet_rescue_lite_term === null) {
                    $pet_rescue_lite_term = wp_insert_term($pet_rescue_lite_category_name, 'product_cat');
                }

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

                // Retrieve the category ID for assignment
                $pet_rescue_lite_term_id = $pet_rescue_lite_term['term_id'];

                // Loop to create 4 products for each category
                for ($pet_rescue_lite_i = 0; $pet_rescue_lite_i < 4; $pet_rescue_lite_i++) {
                    // Create product content
                    $pet_rescue_lite_title = $pet_rescue_lite_title_array[$pet_rescue_lite_index][$pet_rescue_lite_i];
                    $pet_rescue_lite_content = 'There are many variations of passages of lorem ipsum available';

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

                    // Insert the product into the database
                    $pet_rescue_lite_post_id = wp_insert_post($pet_rescue_lite_my_post);

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

                    // Assign the category to the product
                    wp_set_object_terms($pet_rescue_lite_post_id, array($pet_rescue_lite_term_id), 'product_cat');

                    // Set product as simple product and assign a unique price
                    $product_price = $pet_rescue_lite_prices[$pet_rescue_lite_i];
                    update_post_meta($pet_rescue_lite_post_id, '_regular_price', $product_price); // Assign regular price
                    update_post_meta($pet_rescue_lite_post_id, '_price', $product_price); // Assign current price

                    // Handle the featured image using media_sideload_image
                    $pet_rescue_lite_image_url = get_template_directory_uri() . '/images/product' . ($pet_rescue_lite_i + 1) . '.png';
                    $pet_rescue_lite_image_id = media_sideload_image($pet_rescue_lite_image_url, $pet_rescue_lite_post_id, null, 'id');

                    // Check if there was an error downloading the image
                    if (is_wp_error($pet_rescue_lite_image_id)) {
                        error_log('Error downloading image: ' . $pet_rescue_lite_image_id->get_error_message());
                        continue; // Skip to the next product if image download fails
                    }

                    // Assign featured image to product
                    set_post_thumbnail($pet_rescue_lite_post_id, $pet_rescue_lite_image_id);
                }
            }

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

        }
    ?>

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