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

        // If the demo import is completed, display the "View Site" button
        if ($classic_beauty_parlour_demo_import_completed) {
            echo '<br>';
            echo '<div class="success">Demo Import Successful</div>';
            echo '<br>';
            echo '<hr>';
            echo '<br>';
            echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'classic-beauty-parlour' ) . '</span>';
            echo '<br>';
            echo '<br>';
            echo '<br>';
            echo '<div class="view-site-btn">';
            echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
            echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
            echo '</div>';
        }
    if ( isset( $_POST['submit'] ) ) {
    echo '<div class="plugin-notice">';
            // Check if woocommerce is installed and activated
            if (!is_plugin_active('woocommerce/woocommerce.php')) {
                // Install the plugin if it doesn't exist
                $classic_beauty_parlour_plugin_slug = 'woocommerce';
                $classic_beauty_parlour_plugin_file = 'woocommerce/woocommerce.php';

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

            // Check if Classic Blog Grid plugin is installed
            if (!is_plugin_active('classic-blog-grid/classic-blog-grid.php')) {
                // Plugin slug and file path for Classic Blog Grid
                $classic_beauty_parlour_plugin_slug = 'classic-blog-grid';
                $classic_beauty_parlour_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $classic_beauty_parlour_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $classic_beauty_parlour_installed_plugins = get_plugins();
                    if ( ! isset( $classic_beauty_parlour_installed_plugins[ $classic_beauty_parlour_plugin_file ] ) ) {
            
                        // Include necessary files to install plugins
                        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' );
            
                        // Download and install Classic Blog Grid
                        $classic_beauty_parlour_upgrader = new Plugin_Upgrader();
                        $classic_beauty_parlour_upgrader->install( 'https://downloads.wordpress.org/plugin/classic-blog-grid.latest-stable.zip' );
                    }
            
                    // Activate the Classic Blog Grid plugin after installation (if needed)
                    activate_plugin( $classic_beauty_parlour_plugin_file );
                }
            }
    echo '</div>';
        // ------- Create Main Menus --------
        $classic_beauty_parlour_menus = array(
            'primary-left' => 'Primary Left Menu',
            'primary-right' => 'Primary Right Menu',
            'primary-responsive' => 'Primary Responsive Menu',
        );

        $classic_beauty_parlour_menuname = 'Classic Beauty Parlour Menu';
        $classic_beauty_parlour_bpmenulocation = 'primary-left'; 

        $classic_beauty_parlour_menu_id = wp_create_nav_menu($classic_beauty_parlour_menuname);

        // Create Home Page
        $classic_beauty_parlour_home_title = 'Home';
        $classic_beauty_parlour_home = array(
            'post_type'    => 'page',
            'post_title'   => $classic_beauty_parlour_home_title,
            'post_content' => '',
            'post_status'  => 'publish',
            'post_author'  => 1,
        );
        $classic_beauty_parlour_home_id = wp_insert_post($classic_beauty_parlour_home);

        // Assign Home Page Template
        add_post_meta($classic_beauty_parlour_home_id, '_wp_page_template', 'templates/template-home-page.php');

        // Update options to set Home Page as the front page
        update_option('page_on_front', $classic_beauty_parlour_home_id);
        update_option('show_on_front', 'page');

        // Add Home Page to Menu
        wp_update_nav_menu_item($classic_beauty_parlour_menu_id, 0, array(
            'menu-item-title' => __('Home', 'classic-beauty-parlour'),
            'menu-item-classes' => 'home',
            'menu-item-url' => home_url('/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $classic_beauty_parlour_home_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

        // Create a new Page 
        $classic_beauty_parlour_pages_title = 'Pages';
        $classic_beauty_parlour_pages_content = '<p>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, 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 1960s 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.</p>';
        $classic_beauty_parlour_pages = array(
            'post_type'    => 'page',
            'post_title'   => $classic_beauty_parlour_pages_title,
            'post_content' => $classic_beauty_parlour_pages_content,
            'post_status'  => 'publish',
            'post_author'  => 1,
        );
        $classic_beauty_parlour_pages_id = wp_insert_post($classic_beauty_parlour_pages);

        // Add Pages Page to Menu
        wp_update_nav_menu_item($classic_beauty_parlour_menu_id, 0, array(
            'menu-item-title' => __('Pages', 'classic-beauty-parlour'),
            'menu-item-classes' => 'pages',
            'menu-item-url' => home_url('/pages/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $classic_beauty_parlour_pages_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

        // Create About Us Page
        $classic_beauty_parlour_about_title = 'About Us';
        $classic_beauty_parlour_about_content = '<p>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, 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 1960s 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.</p>';
        $classic_beauty_parlour_about = array(
            'post_type'    => 'page',
            'post_title'   => $classic_beauty_parlour_about_title,
            'post_content' => $classic_beauty_parlour_about_content,
            'post_status'  => 'publish',
            'post_author'  => 1,
        );
        $classic_beauty_parlour_about_id = wp_insert_post($classic_beauty_parlour_about);

        // Add About Us Page to Menu
        wp_update_nav_menu_item($classic_beauty_parlour_menu_id, 0, array(
            'menu-item-title' => __('About Us', 'classic-beauty-parlour'),
            'menu-item-classes' => 'about-us',
            'menu-item-url' => home_url('/about-us/'),
            'menu-item-status' => 'publish',
            'menu-item-object-id' => $classic_beauty_parlour_about_id,
            'menu-item-object' => 'page',
            'menu-item-type' => 'post_type'
        ));

        // Assign the menu to the primary location if not already set
        $classic_beauty_parlour_locations = get_theme_mod('nav_menu_locations', array());
        $classic_beauty_parlour_locations[$classic_beauty_parlour_bpmenulocation] = $classic_beauty_parlour_menu_id;
        set_theme_mod('nav_menu_locations', $classic_beauty_parlour_locations);


        //Logo
        set_theme_mod( 'classic_beauty_parlour_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));

        //Homepage Sidebar
        set_theme_mod( 'classic_beauty_parlour_topbar_text_title', 'Lorem Ipsum is simply dummy.');
        set_theme_mod( 'classic_beauty_parlour_topbar_mail_title', 'Email');
        set_theme_mod( 'classic_beauty_parlour_email_address', 'xyz123@example.com');
        set_theme_mod( 'classic_beauty_parlour_topbar_phone_title', 'Contact Us');
        set_theme_mod( 'classic_beauty_parlour_phone_number', '123456789');
        set_theme_mod( 'classic_beauty_parlour_facebook_link', 'https://www.facebook.com');
        set_theme_mod( 'classic_beauty_parlour_twitter_link', 'https://www.twitter.com');
        set_theme_mod( 'classic_beauty_parlour_instagram_link', 'https://www.instagram.com');
        set_theme_mod( 'classic_beauty_parlour_youtube_link', 'https://www.youtube.com');

        //Slider Section
        set_theme_mod( 'classic_beauty_parlour_slider_sub_title', 'Experience The Best Beauty Services');
        set_theme_mod( 'classic_beauty_parlour_button_text', 'Browse More');
        set_theme_mod( 'classic_beauty_parlour_button_link_slider', '#');
        set_theme_mod( 'classic_beauty_parlour_banner_bottom_img', esc_url( get_template_directory_uri().'/images/bottom-img.png'));

        // Create the 'Parlour' category and retrieve its ID
        $classic_beauty_parlour_slider_category_id = wp_create_category('Parlour');

        // Set the slider category in theme mods
        set_theme_mod('classic_beauty_parlour_slider_cat', 'Parlour');

        $classic_beauty_parlour_titles = array(
            'Where Womens Wants To Look There Very Best',
            'Yours Truly, A Beauty Experience Like No Other',
            'Xquisite Beauty Awaits You'
        );        
        // Create three posts and assign them to the 'Parlour' category
        // Define the image URLs
        for ($classic_beauty_parlour_i = 1; $classic_beauty_parlour_i <= 3; $classic_beauty_parlour_i++) {
            $classic_beauty_parlour_title = $classic_beauty_parlour_titles[$classic_beauty_parlour_i - 1];           
            $classic_beauty_parlour_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, 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 1960s 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.';
            
            // Create the post object
            $classic_beauty_parlour_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_beauty_parlour_title),
                'post_content'  => $classic_beauty_parlour_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_beauty_parlour_slider_category_id),  // Use the category ID
            );

            // Insert the post into the database
            $classic_beauty_parlour_post_id = wp_insert_post( $classic_beauty_parlour_my_post );

            if ($classic_beauty_parlour_post_id) {
                // Set the theme mod for the slider page
                set_theme_mod('classic_beauty_parlour_slider_cat' . $classic_beauty_parlour_i, $classic_beauty_parlour_post_id);

                $classic_beauty_parlour_image_url = get_template_directory_uri().'/images/slider'.$classic_beauty_parlour_i.'.png';

                $classic_beauty_parlour_image_id = media_sideload_image($classic_beauty_parlour_image_url, $classic_beauty_parlour_post_id, null, 'id');

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

        //Product Section
        set_theme_mod( 'classic_beauty_parlour_trending_title', 'Trending Products');

        // Set the theme mod for the product category
        set_theme_mod('classic_beauty_parlour_hot_products_cat', 'productcategory1');

        // Define the single product category name, product titles, and tags
        $classic_beauty_parlour_category_name = 'productcategory1';
        $classic_beauty_parlour_titles = array(
            "HD Skin Matte Liquid Foundation",
            "Day And Night Moisturizer Cream",
            "Healthy Spa Concept Body Cream",
            "Beauty Aromatic Handmade Soap"
        );
        
        $classic_beauty_parlour_regular_price = 120; 
        $classic_beauty_parlour_sale_price = 90; 

        // Create or retrieve the product category term ID
        $classic_beauty_parlour_term = term_exists($classic_beauty_parlour_category_name, 'product_cat');
        if (!$classic_beauty_parlour_term) {
            $classic_beauty_parlour_term = wp_insert_term($classic_beauty_parlour_category_name, 'product_cat');
        }

        if (is_wp_error($classic_beauty_parlour_term)) {
            error_log('Error creating category: ' . $classic_beauty_parlour_term->get_error_message());
            return; // Exit if category creation fails
        }

        $classic_beauty_parlour_term_id = is_array($classic_beauty_parlour_term) ? $classic_beauty_parlour_term['term_id'] : $classic_beauty_parlour_term;

        // Loop to create 4 products for the category
        foreach ($classic_beauty_parlour_titles as $classic_beauty_parlour_index => $classic_beauty_parlour_title) {

            // Create product content
            $classic_beauty_parlour_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';

            // Create product post object
            $classic_beauty_parlour_post_id = wp_insert_post(array(
                'post_title'    => wp_strip_all_tags($classic_beauty_parlour_title),
                'post_content'  => $classic_beauty_parlour_content,
                'post_status'   => 'publish',
                'post_type'     => 'product', // Post type set to 'product'
            ));

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

            // Assign the category to the product
            wp_set_object_terms($classic_beauty_parlour_post_id, $classic_beauty_parlour_term_id, 'product_cat');

            // Set product prices
            update_post_meta($classic_beauty_parlour_post_id, '_regular_price', $classic_beauty_parlour_regular_price);
            update_post_meta($classic_beauty_parlour_post_id, '_sale_price', $classic_beauty_parlour_sale_price);
            update_post_meta($classic_beauty_parlour_post_id, '_price', $classic_beauty_parlour_sale_price); // Current price is the sale price

            // Handle the featured image using media_sideload_image
            $classic_beauty_parlour_image_url = get_template_directory_uri() . '/images/Product' . ($classic_beauty_parlour_index + 1) . '.png';
            $classic_beauty_parlour_image_id = media_sideload_image($classic_beauty_parlour_image_url, $classic_beauty_parlour_post_id, null, 'id');

            if (!is_wp_error($classic_beauty_parlour_image_id)) {
                // Assign featured image to product
                set_post_thumbnail($classic_beauty_parlour_post_id, $classic_beauty_parlour_image_id);
            } else {
                error_log('Error downloading image for product: ' . $classic_beauty_parlour_image_id->get_error_message());
            }
        }     

        // Show success message and the "View Site" button
        update_option('classic_beauty_parlour_demo_import_completed', true);
        echo '<br>';
        echo '<div class="success">Demo Import Successful</div>';
        echo '<br>';
        echo '<hr>';
        echo '<br>';
        echo '<span>' . esc_html__( 'You can now visit your site or customize it further.', 'classic-beauty-parlour' ) . '</span>';
        echo '<br>';
    }
     ?>
    <ul>
        <li>
        <?php 
        // Check if the form is submitted
        if ( !isset( $_POST['submit'] ) ) : ?>
            <!-- Show demo importer form only if it's not submitted -->
            <?php if (!get_option('classic_beauty_parlour_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'classic-beauty-parlour' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'classic-beauty-parlour' ); ?></b><br><br>
                <small><b><?php echo esc_html('Please take a backup if your website is already live with data. This importer will overwrite existing data.', 'classic-beauty-parlour' ); ?></b></small><br><br>
                <form id="demo-importer-form" action="" method="POST" onsubmit="return runDemoImport();">
                    <input type="submit" name="submit" value="<?php echo esc_attr('Run Importer','classic-beauty-parlour'); ?>" class="button button-primary button-large">
                </form>
                <script type="text/javascript">
                    function runDemoImport() {
                        if (confirm('Do you really want to do this?')) {
                            document.getElementById('demo-import-loader').style.display = 'block';
                            return true;
                        }
                        return false;
                    }
                </script>
             <?php endif; ?>
         <?php 
        endif; 

        // Show "View Site" button after form submission
        if ( isset( $_POST['submit'] ) ) {
        echo '<div class="view-site-btn">';
        echo '<a href="' . esc_url(home_url()) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">View Site</a>';
        echo '<a href="' . esc_url( admin_url('customize.php') ) . '" class="button button-primary button-large" style="margin-top: 10px;" target="_blank">Customize Demo Content</a>';
        echo '</div>';
        }
        ?>
        </li>
    </ul>
 </div>