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

        // If the demo import is completed, display the "View Site" button
        if ($christmas_event_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.', 'christmas-event' ) . '</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 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
            $christmas_event_plugin_slug = 'classic-blog-grid';
            $christmas_event_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
        
            // Check if Classic Blog Grid is installed and activated
            if ( ! is_plugin_active( $christmas_event_plugin_file ) ) {
        
                // Check if Classic Blog Grid is installed
                $christmas_event_installed_plugins = get_plugins();
                if ( ! isset( $christmas_event_installed_plugins[ $christmas_event_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
                    $christmas_event_upgrader = new Plugin_Upgrader();
                    $christmas_event_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( $christmas_event_plugin_file );
            }
        }
    echo '</div>';
        // ------- Create Main Menu --------
        $christmas_event_menuname = 'Primary Menu';
        $christmas_event_bpmenulocation = 'primary';
        $christmas_event_menu_exists = wp_get_nav_menu_object( $christmas_event_menuname );
    
        if ( !$christmas_event_menu_exists ) {
            $christmas_event_menu_id = wp_create_nav_menu( $christmas_event_menuname );

            // Create Home Page
            $christmas_event_home_title = 'Home';
            $christmas_event_home = array(
                'post_type'    => 'page',
                'post_title'   => $christmas_event_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $christmas_event_home_id = wp_insert_post($christmas_event_home);
            // Assign Home Page Template
            add_post_meta($christmas_event_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', $christmas_event_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($christmas_event_menu_id, 0, array(
                'menu-item-title' => __('Home', 'christmas-event'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $christmas_event_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

           // Create Blogs Page with Dummy Content
            $christmas_event_about_title = 'Blogs';
            $christmas_event_about_content = '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...';

            $christmas_event_about = array(
                'post_type'    => 'page',
                'post_title'   => $christmas_event_about_title,
                'post_content' => $christmas_event_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'blogs' 
            );

            $christmas_event_about_id = wp_insert_post($christmas_event_about);

            // Add Blogs Page to Menu
            wp_update_nav_menu_item( $christmas_event_menu_id, 0, array(
                'menu-item-title'     => __( 'Blogs', 'christmas-event' ),
                'menu-item-classes'  => 'blogs',
                'menu-item-object-id'=> $christmas_event_about_id,
                'menu-item-object'   => 'page',
                'menu-item-type'     => 'post_type',
                'menu-item-status'   => 'publish'
            ));

            update_option( 'page_for_posts', $christmas_event_about_id );

            // Create a new Page 
            $christmas_event_pages_title = 'Page';
            $christmas_event_pages_content = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';
            $christmas_event_pages = array(
                'post_type'    => 'page',
                'post_title'   => $christmas_event_pages_title,
                'post_content' => $christmas_event_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'page'
            );
            $christmas_event_pages_id = wp_insert_post($christmas_event_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($christmas_event_menu_id, 0, array(
                'menu-item-title' => __('Page', 'christmas-event'),
                'menu-item-classes' => 'page',
                'menu-item-url' => home_url('/page/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $christmas_event_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $christmas_event_about_title = 'Team';
            $christmas_event_about_content = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';
            $christmas_event_about = array(
                'post_type'    => 'page',
                'post_title'   => $christmas_event_about_title,
                'post_content' => $christmas_event_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'team'
            );
            $christmas_event_about_id = wp_insert_post($christmas_event_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($christmas_event_menu_id, 0, array(
                'menu-item-title' => __('Team', 'christmas-event'),
                'menu-item-classes' => 'team',
                'menu-item-url' => home_url('/team/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $christmas_event_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $christmas_event_about_title = 'Services';
            $christmas_event_about_content = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';
            $christmas_event_about = array(
                'post_type'    => 'page',
                'post_title'   => $christmas_event_about_title,
                'post_content' => $christmas_event_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'services'
            );
            $christmas_event_about_id = wp_insert_post($christmas_event_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($christmas_event_menu_id, 0, array(
                'menu-item-title' => __('Services', 'christmas-event'),
                'menu-item-classes' => 'services',
                'menu-item-url' => home_url('/services/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $christmas_event_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $christmas_event_about_title = 'Contact';
            $christmas_event_about_content = '<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960 with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';
            $christmas_event_about = array(
                'post_type'    => 'page',
                'post_title'   => $christmas_event_about_title,
                'post_content' => $christmas_event_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'contact'
            );
            $christmas_event_about_id = wp_insert_post($christmas_event_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($christmas_event_menu_id, 0, array(
                'menu-item-title' => __('Contact', 'christmas-event'),
                'menu-item-classes' => 'contact',
                'menu-item-url' => home_url('/contact/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $christmas_event_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Assign the menu to the primary location if not already set
            if ( ! has_nav_menu( $christmas_event_bpmenulocation ) ) {
                $christmas_event_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $christmas_event_locations ) ) {
                    $christmas_event_locations = array();
                }
                $christmas_event_locations[ $christmas_event_bpmenulocation ] = $christmas_event_menu_id;
                set_theme_mod( 'nav_menu_locations', $christmas_event_locations );
            }
        }

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

        // Header Section
        set_theme_mod( 'classic_events_contact_us_text', 'Book Now' );
        set_theme_mod( 'classic_events_contact_us_url', '#' );

        //Slider Section
        set_theme_mod( 'classic_events_slider_top_text', 'Let’s Have a Party' );
        set_theme_mod( 'classic_events_button_text', 'Online Request' );
        set_theme_mod( 'classic_events_button_link_slider', '#' );
        
        // Use the child theme's image for the slider background
        set_theme_mod( 'classic_events_slider_background_image_sec', esc_url( get_stylesheet_directory_uri() . '/images/slider-img.png') );
        
        // Create the 'Party' category and retrieve its ID
        $classic_events_slider_category_id = wp_create_category('Party');

        // Set the category in theme mods for the slider section
        set_theme_mod('classic_events_slider_cat', 'Party');

        // Titles for the three demo posts
        $classic_events_titles = array(
            'Sparkle and Cheer this Christmas!',
            'Make This Christmas Unforgettable',
            'Party with Joy and Laughter!'
        );

        // Create three demo posts and assign them to the 'Party' category
        for ($classic_events_i = 1; $classic_events_i <= 3; $classic_events_i++) {
            $classic_events_title = $classic_events_titles[$classic_events_i - 1];
            $classic_events_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.';

            // Prepare the post object
            $classic_events_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_events_title),
                'post_content'  => $classic_events_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_events_slider_category_id),
            );

            // Insert the post into the database
            $classic_events_post_id = wp_insert_post($classic_events_my_post);

            // If the post was successfully created, set the featured image
            if ($classic_events_post_id && !is_wp_error($classic_events_post_id)) {
                // Set the image URL based on the current slider index
                $classic_events_image_url = esc_url(get_theme_file_uri() . '/images/Slider' . $classic_events_i . '.png');
                $classic_events_upload_dir = wp_upload_dir();

                // Download the image data using wp_remote_get()
                $classic_events_response = wp_remote_get($classic_events_image_url);
                if (!is_wp_error($classic_events_response)) {
                    $classic_events_image_data = wp_remote_retrieve_body($classic_events_response);

                    if (!empty($classic_events_image_data)) {
                        // Handle the file upload process
                        $classic_events_image_name = 'slider' . $classic_events_i . '.png';
                        $classic_events_unique_file_name = wp_unique_filename($classic_events_upload_dir['path'], $classic_events_image_name);
                        $classic_events_file = $classic_events_upload_dir['path'] . '/' . $classic_events_unique_file_name;

                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($classic_events_file, $classic_events_image_data);

                        // Check file type and prepare for attachment
                        $classic_events_wp_filetype = wp_check_filetype($classic_events_unique_file_name, null);
                        $classic_events_attachment = array(
                            'post_mime_type' => $classic_events_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($classic_events_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );

                        // Insert the image into the media library and set it as the post's featured image
                        $classic_events_attach_id = wp_insert_attachment($classic_events_attachment, $classic_events_file, $classic_events_post_id);
                        $classic_events_attach_data = wp_generate_attachment_metadata($classic_events_attach_id, $classic_events_file);
                        wp_update_attachment_metadata($classic_events_attach_id, $classic_events_attach_data);
                        set_post_thumbnail($classic_events_post_id, $classic_events_attach_id);
                    }
                }
            }
        }

        // Our Services Section
        set_theme_mod( 'christmas_event_category_sec_text', 'Our Services' );
        set_theme_mod( 'christmas_event_category_sec_title', 'Delivering Christmas decor, gifts, and festive experiences!' );
        set_theme_mod( 'christmas_event_info_btn_text', 'View Info' );
        set_theme_mod( 'christmas_event_info_btn_url', '#' );
        
        function christmas_event_create_default_categories_with_images() {
            $christmas_event_default_categories = array(
                'Christmas Decor' => array(
                    'image' => get_theme_file_uri() . '/images/category1.png'
                ),
                'Gift Wrapping' => array(
                    'image' => get_theme_file_uri() . '/images/category2.png'
                ),
                'Event Planning' => array(
                    'image' => get_theme_file_uri() . '/images/category3.png'
                ),
            );
        
            foreach ($christmas_event_default_categories as $christmas_event_category_name => $christmas_event_category_data) {
                // Check if the category exists; if not, create it
                $christmas_event_term = term_exists($christmas_event_category_name, 'category');
                if ($christmas_event_term === 0 || $christmas_event_term === null) {
                    $christmas_event_term = wp_insert_term($christmas_event_category_name, 'category');
                }
        
                if (!is_wp_error($christmas_event_term)) {
                    // Get the term ID and save the image URL to term meta
                    $category_id = $christmas_event_term['term_id'];
                    update_term_meta($category_id, 'category_image', $christmas_event_category_data['image']);
                    error_log("Category '{$christmas_event_category_name}' created with image '{$christmas_event_category_data['image']}'"); // Debugging log
                } else {
                    error_log("Error creating category '{$christmas_event_category_name}': " . $christmas_event_term->get_error_message()); // Debugging log
                }
            }
        }
        
        // Run the function during demo import
        christmas_event_create_default_categories_with_images();
                    
    //Footer Copyright Text
    set_theme_mod( 'christmas_event_copyright_line', 'Christmas Event WordPress Theme' );

        // Show success message and the "View Site" button
        update_option('christmas_event_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.', 'christmas-event' ) . '</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('christmas_event_demo_import_completed')) : ?>
                <span><?php echo esc_html( 'Click on the below content to get demo content installed.', 'christmas-event' ); ?></span>
                <br><br>
                <hr><br>
                <b class="note"><?php echo esc_html('Note :', 'christmas-event' ); ?></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.', 'christmas-event' ); ?></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','christmas-event'); ?>" 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>