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

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

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

            // ------- Create Nav Menu --------
            $coupons_deals_menuname = 'Main Menus';
            $coupons_deals_bpmenulocation = 'primary';
            $coupons_deals_menu_exists = wp_get_nav_menu_object($coupons_deals_menuname);

            if (!$coupons_deals_menu_exists) {
                $coupons_deals_menu_id = wp_create_nav_menu($coupons_deals_menuname);

                // Create Home Page
                $coupons_deals_home_title = 'Home';
                $coupons_deals_home = array(
                    'post_type' => 'page',
                    'post_title' => $coupons_deals_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $coupons_deals_home_id = wp_insert_post($coupons_deals_home);
                // Assign Home Page Template
                add_post_meta($coupons_deals_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', $coupons_deals_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($coupons_deals_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'coupons-deals'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $coupons_deals_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $coupons_deals_about_title = 'About Us';
                $coupons_deals_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.';
                $coupons_deals_about = array(
                    'post_type' => 'page',
                    'post_title' => $coupons_deals_about_title,
                    'post_content' => $coupons_deals_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $coupons_deals_about_id = wp_insert_post($coupons_deals_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($coupons_deals_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'coupons-deals'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $coupons_deals_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));
                
                /* Create Coupons Page */
                $coupons_deals_coupons_title = 'Coupons';
                $coupons_deals_coupons_content = 'Browse the latest coupons and deals from top brands and stores.
                Lorem ipsum dolor sit amet, consectetur adipiscing elit.';

                $coupons_deals_coupons = array(
                    'post_type'    => 'page',
                    'post_title'   => $coupons_deals_coupons_title,
                    'post_content' => $coupons_deals_coupons_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'coupons'
                );
                $coupons_deals_coupons_id = wp_insert_post($coupons_deals_coupons);

                // Add Coupons Page to Menu
                wp_update_nav_menu_item($coupons_deals_menu_id, 0, array(
                    'menu-item-title'   => __('Coupons', 'coupons-deals'),
                    'menu-item-classes' => 'coupons',
                    'menu-item-url'     => home_url('/coupons/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $coupons_deals_coupons_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                /* Create Categories Page */
                $coupons_deals_categories_title = 'Categories';
                $coupons_deals_categories_content = 'Explore coupon categories to find the best deals quickly.';

                $coupons_deals_categories = array(
                    'post_type'    => 'page',
                    'post_title'   => $coupons_deals_categories_title,
                    'post_content' => $coupons_deals_categories_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'categories'
                );
                $coupons_deals_categories_id = wp_insert_post($coupons_deals_categories);

                // Add Categories Page to Menu
                wp_update_nav_menu_item($coupons_deals_menu_id, 0, array(
                    'menu-item-title'   => __('Categories', 'coupons-deals'),
                    'menu-item-classes' => 'categories',
                    'menu-item-url'     => home_url('/categories/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $coupons_deals_categories_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                /* Create Stores Page */
                $coupons_deals_stores_title = 'Stores';
                $coupons_deals_stores_content = 'Find deals and coupons from popular online and local stores.';

                $coupons_deals_stores = array(
                    'post_type'    => 'page',
                    'post_title'   => $coupons_deals_stores_title,
                    'post_content' => $coupons_deals_stores_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_name'    => 'stores'
                );
                $coupons_deals_stores_id = wp_insert_post($coupons_deals_stores);

                // Add Stores Page to Menu
                wp_update_nav_menu_item($coupons_deals_menu_id, 0, array(
                    'menu-item-title'   => __('Stores', 'coupons-deals'),
                    'menu-item-classes' => 'stores',
                    'menu-item-url'     => home_url('/stores/'),
                    'menu-item-status'  => 'publish',
                    'menu-item-object-id' => $coupons_deals_stores_id,
                    'menu-item-object'  => 'page',
                    'menu-item-type'    => 'post_type'
                ));

                // Create Blog Page 
                $coupons_deals_blog_page_title = 'Blog';
                $coupons_deals_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($coupons_deals_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$coupons_deals_blog_page_query->have_posts()) {
                    $coupons_deals_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $coupons_deals_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $coupons_deals_blog_page_id = wp_insert_post($coupons_deals_blog_page);
                    update_option('page_for_posts', $coupons_deals_blog_page_id);
                    wp_update_nav_menu_item($coupons_deals_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'coupons-deals'),
                    'menu-item-url'        => get_permalink($coupons_deals_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $coupons_deals_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $coupons_deals_contact_title = 'Contact Us';
                $coupons_deals_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.';
                $coupons_deals_contact = array(
                    'post_type' => 'page',
                    'post_title' => $coupons_deals_contact_title,
                    'post_content' => $coupons_deals_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $coupons_deals_contact_id = wp_insert_post($coupons_deals_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($coupons_deals_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'coupons-deals'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $coupons_deals_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($coupons_deals_bpmenulocation)) {
                    $coupons_deals_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($coupons_deals_locations)) {
                        $coupons_deals_locations = array();
                    }
                    $coupons_deals_locations[$coupons_deals_bpmenulocation] = $coupons_deals_menu_id;
                    set_theme_mod('nav_menu_locations', $coupons_deals_locations);
                }
            }

            //Social Media
            set_theme_mod('coupons_deals_facebook_link', 'https://www.facebook.com');
            set_theme_mod('coupons_deals_twitter_link', 'https://www.twitter.com');
            set_theme_mod('coupons_deals_linkdin_link', 'https://www.linkedin.com');
            set_theme_mod('coupons_deals_instagram_link', 'https://instagram.com');
            set_theme_mod('coupons_deals_pintrest_link', 'https://www.pinterest.com');

            //Slider
            set_theme_mod('coupons_deals_slider_hide', true);
            set_theme_mod('coupons_deals_slider_small_title', 'Best Deals!');
            set_theme_mod('coupons_deals_slider_button_text', 'SHOP NOW');
            set_theme_mod('coupons_deals_slider_button_link', '#');
            for($coupons_deals_i=1;$coupons_deals_i<=4;$coupons_deals_i++){
                $coupons_deals_slider_title = array(
                    'Coupon XxL is the best of the best',
                    'Unbeatable Deals at Your Fingertips',
                    'Save More, Shop Smarter Today',
                    'Discover Discounts You’ll Love'
                );                
                $coupons_deals_current_title = $coupons_deals_slider_title[$coupons_deals_i - 1];
                $coupons_deals_slider_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,';
                // Create post object
                $coupons_deals_my_post = array(
                'post_title'    => wp_strip_all_tags( $coupons_deals_current_title ),
                'post_content'  => $coupons_deals_slider_content,
                'post_status'   => 'publish',
                'post_type'     => 'page',
                );
 
                // Insert the post into the database
                $coupons_deals_post_id = wp_insert_post( $coupons_deals_my_post );
 
                if ($coupons_deals_post_id) {
                  // Set the theme mod for the slider page
                  set_theme_mod('coupons_deals_slider_setting' . $coupons_deals_i, $coupons_deals_post_id);
 
                   $coupons_deals_image_url = get_template_directory_uri().'/images/slider'.$coupons_deals_i.'.png';
 
                 $coupons_deals_image_id = media_sideload_image($coupons_deals_image_url, $coupons_deals_post_id, null, 'id');
 
                     if (!is_wp_error($coupons_deals_image_id)) {
                         // Set the downloaded image as the post's featured image
                         set_post_thumbnail($coupons_deals_post_id, $coupons_deals_image_id);
                     }
                 }
            } 

            //Coupon Section
            set_theme_mod('coupons_deals_coupon_text', 'Visa Card Offer - Get Rs 200 OFF on Order of Rs 1499');
            set_theme_mod('coupons_deals_coupon_button_text', 'Shop Now');
            set_theme_mod('coupons_deals_coupon_button_url', '#');
            set_theme_mod('coupons_deals_coupon_image', esc_url( get_template_directory_uri().'/images/right-img.png'));

            //Featured Section
            set_theme_mod('coupons_deals_featured_enable', true);
            set_theme_mod('coupons_deals_featured_section_title', 'Featured Coupons');
            set_theme_mod('coupons_deals_featured_text', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do.');

            set_theme_mod('coupons_deals_featured_coupons', 'postcategory1' );
            // Define post category names and post titles
            $coupons_deals_category_names = array('postcategory1', 'postcategory2', 'postcategory3');
            $coupons_deals_title_array = array(
                array("Only this month 40% discount on all product", "Only this month 40% discount on all product", "Only this month 40% discount on all product"),
                array("Only this month 40% discount on all product", "Only this month 40% discount on all product", "Only this month 40% discount on all product"),
                array("Only this month 40% discount on all product", "Only this month 40% discount on all product", "Only this month 40% discount on all product")
            );
            
            $coupons_deals_content_array = array(
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry","Lorem is simply dummy text of printing and typesetting industry"),
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry"),
                array("Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry", "Lorem is simply dummy text of printing and typesetting industry")
            );
            
            foreach ($coupons_deals_category_names as $coupons_deals_index => $coupons_deals_category_name) {
                // Create or retrieve the post category term ID
                $coupons_deals_term = term_exists($coupons_deals_category_name, 'category');
                if ($coupons_deals_term === 0 || $coupons_deals_term === null) {
                    // If the term does not exist, create it
                    $coupons_deals_term = wp_insert_term($coupons_deals_category_name, 'category');
                }
                if (is_wp_error($coupons_deals_term)) {
                    error_log('Error creating category: ' . $coupons_deals_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }
            
                for ($coupons_deals_i = 0; $coupons_deals_i < 3; $coupons_deals_i++) {
                    // Create post content
                    $coupons_deals_title = $coupons_deals_title_array[$coupons_deals_index][$coupons_deals_i];
                    $coupons_deals_content = $coupons_deals_content_array[$coupons_deals_index][$coupons_deals_i];
            
                    // Create post post object
                    $coupons_deals_my_post = array(
                        'post_title'    => wp_strip_all_tags($coupons_deals_title),
                        'post_content'  => $coupons_deals_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                        'post_excerpt'  => substr($coupons_deals_content, 0, 100) . '...', // Add an excerpt
                    );
            
                    // Insert the post into the database
                    $coupons_deals_post_id = wp_insert_post($coupons_deals_my_post);
            
                    if (is_wp_error($coupons_deals_post_id)) {
                        error_log('Error creating post: ' . $coupons_deals_post_id->get_error_message());
                        continue; // Skip to the next post if creation fails
                    }
            
                    // Assign the category to the post
                    wp_set_post_categories($coupons_deals_post_id, array((int)$coupons_deals_term['term_id']));
            
                    // Handle the featured image using media_sideload_image
                    $coupons_deals_image_url = get_template_directory_uri() . '/images/product' . ($coupons_deals_i + 1) . '.png';
                    $coupons_deals_image_id = media_sideload_image($coupons_deals_image_url, $coupons_deals_post_id, null, 'id');
            
                    if (is_wp_error($coupons_deals_image_id)) {
                        error_log('Error downloading image: ' . $coupons_deals_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }
            
                    // Assign featured image to post
                    set_post_thumbnail($coupons_deals_post_id, $coupons_deals_image_id);
            
                     // Custom metadata for coupons
                     update_post_meta($coupons_deals_post_id, 'coupons_deals_date', "25/12/25");
                }
            }

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

        }
    ?>

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