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

        // If the demo import is completed, display the "View Site" button
        if ($luxury_interior_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.', 'luxury-interior' ) . '</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
                $luxury_interior_plugin_slug = 'classic-blog-grid';
                $luxury_interior_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $luxury_interior_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $luxury_interior_installed_plugins = get_plugins();
                    if ( ! isset( $luxury_interior_installed_plugins[ $luxury_interior_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
                        $luxury_interior_upgrader = new Plugin_Upgrader();
                        $luxury_interior_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( $luxury_interior_plugin_file );
                }
            }
        echo '</div>';

        // ------- Create Main Menu --------
        $luxury_interior_menuname = 'Primary Menu';
        $luxury_interior_bpmenulocation = 'primary';
        $luxury_interior_menu_exists = wp_get_nav_menu_object( $luxury_interior_menuname );
    
        if ( !$luxury_interior_menu_exists ) {
            $luxury_interior_menu_id = wp_create_nav_menu( $luxury_interior_menuname );

            // Create Home Page
            $luxury_interior_home_title = 'Home';
            $luxury_interior_home = array(
                'post_type'    => 'page',
                'post_title'   => $luxury_interior_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $luxury_interior_home_id = wp_insert_post($luxury_interior_home);
            // Assign Home Page Template
            add_post_meta($luxury_interior_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', $luxury_interior_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($luxury_interior_menu_id, 0, array(
                'menu-item-title' => __('Home', 'luxury-interior'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $luxury_interior_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Page 
            $luxury_interior_pages_title = 'Pages';
            $luxury_interior_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>';
            $luxury_interior_pages = array(
                'post_type'    => 'page',
                'post_title'   => $luxury_interior_pages_title,
                'post_content' => $luxury_interior_pages_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $luxury_interior_pages_id = wp_insert_post($luxury_interior_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($luxury_interior_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'luxury-interior'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $luxury_interior_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $luxury_interior_about_title = 'About Us';
            $luxury_interior_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>';
            $luxury_interior_about = array(
                'post_type'    => 'page',
                'post_title'   => $luxury_interior_about_title,
                'post_content' => $luxury_interior_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about-us'
            );
            $luxury_interior_about_id = wp_insert_post($luxury_interior_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($luxury_interior_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'luxury-interior'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $luxury_interior_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( $luxury_interior_bpmenulocation ) ) {
                $luxury_interior_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $luxury_interior_locations ) ) {
                    $luxury_interior_locations = array();
                }
                $luxury_interior_locations[ $luxury_interior_bpmenulocation ] = $luxury_interior_menu_id;
                set_theme_mod( 'nav_menu_locations', $luxury_interior_locations );
            }
        }

        // Header Section
        set_theme_mod( 'luxury_interior_topbar', true);
        set_theme_mod( 'luxury_interior_the_custom_logo', esc_url( get_template_directory_uri().'/images/Logo.png'));
        set_theme_mod( 'luxury_interior_email_address', 'info@yourmail.com');
        set_theme_mod( 'luxury_interior_fb_link', '#');
        set_theme_mod( 'luxury_interior_twitt_link', '#');
        set_theme_mod( 'luxury_interior_linked_link', '#');
        set_theme_mod( 'luxury_interior_insta_link', '#');
        set_theme_mod( 'luxury_interior_youtube_link', '#');

        //Slider Section
        set_theme_mod( 'luxury_interior_hide_categorysec', true);
        set_theme_mod( 'luxury_interior_button_text', 'OUR SERVICES');
        set_theme_mod( 'luxury_interior_button_link_slider', '#');

        // Create the 'Interior' category and retrieve its ID
        $luxury_interior_slider_category_id = wp_create_category('Interior');

        // Set the category in theme mods for the slider section
        if (!is_wp_error($luxury_interior_slider_category_id)) {
            set_theme_mod('luxury_interior_slidersection', $luxury_interior_slider_category_id);
        }
        $luxury_interior_titles = array(
            'LET YOUR HOME BE UNIQUE & STYLISH',
            'UNIQUE HOME LIVING',
            'STYLE WITH CLASS'
        );
        // Create three demo posts and assign them to the 'Interior' category
        for ($luxury_interior_i = 0; $luxury_interior_i <= 2; $luxury_interior_i++) {
            set_theme_mod('luxury_interior_title' . ($luxury_interior_i + 1), $luxury_interior_titles[$luxury_interior_i]);
    
            // Prepare the post object
            $luxury_interior_my_post = array(
                'post_title'    => wp_strip_all_tags($luxury_interior_titles[$luxury_interior_i]),
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($luxury_interior_slider_category_id),
            );
    
            // Insert the post into the database
            $luxury_interior_post_id = wp_insert_post($luxury_interior_my_post);
    
            // If the post was successfully created, set the featured image
            if (!is_wp_error($luxury_interior_post_id)) {
                $luxury_interior_image_url = get_template_directory_uri() . '/images/slider' . ($luxury_interior_i + 1) . '.png';
                $luxury_interior_image_id = media_sideload_image($luxury_interior_image_url, $luxury_interior_post_id, null, 'id');
                if (!is_wp_error($luxury_interior_image_id)) {
                    set_post_thumbnail($luxury_interior_post_id, $luxury_interior_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $luxury_interior_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($luxury_interior_post_id, true));
            }
        }
        
       //Services Section
       set_theme_mod( 'luxury_interior_disabled_pgboxes', true);
       set_theme_mod( 'luxury_interior_headingtext1', 'OUR BEST SERVICE');
       set_theme_mod( 'luxury_interior_headingtext2', 'BEST SERVICES WERE PROVIDING');

        // Include WordPress Filesystem API
        require_once( ABSPATH . 'wp-admin/includes/file.php' );

        // Initialize the filesystem
        global $wp_filesystem;
        if (false === ($luxury_interior_creds = request_filesystem_credentials('', '', false, false, null))) {
            return; // Exit if credentials are needed
        }

        if (!WP_Filesystem($luxury_interior_creds)) {
            return; // Exit if filesystem could not be initialized
        }

        $luxury_interior_services_category_id = wp_create_category('Services');
        set_theme_mod('luxury_interior_services_cat', $luxury_interior_services_category_id);
        $luxury_interior_service_titles = array('HOME DECORATIONS', 'FURNITURES', 'LIGHTINGS');
        $luxury_interior_service_contents = 'Morbi volutpat nisi a ligula vestibulum placerat. Suspendisse venenatis pulvinar nibh sed convallis....';

        // Define the image URLs for each service
        $luxury_interior_image_urls = array(
            esc_url(get_template_directory_uri() . '/images/interior-services/interior-service1.png'),
            esc_url(get_template_directory_uri() . '/images/interior-services/interior-service2.png'),
            esc_url(get_template_directory_uri() . '/images/interior-services/interior-service3.png'),
        );

        // Loop to create posts for each service
        for ($luxury_interior_i = 0; $luxury_interior_i < count($luxury_interior_service_titles); $luxury_interior_i++) {
                $luxury_interior_title = $luxury_interior_service_titles[$luxury_interior_i];
                $luxury_interior_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
    
                // Create the post object
                $luxury_interior_service_post = array(
                    'post_title'    => wp_strip_all_tags($luxury_interior_title),
                    'post_content'  => $luxury_interior_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'post',
                    'post_category' => array($luxury_interior_services_category_id),
                );
    
                // Insert the post into the database
                $luxury_interior_service_id = wp_insert_post($luxury_interior_service_post);
    
                // If the post was successfully created
                if ($luxury_interior_service_id && !is_wp_error($luxury_interior_service_id)) {
                    // Set the theme mod for selecting this post
                    set_theme_mod('luxury_interior_services_cat' . ($luxury_interior_i + 1), $luxury_interior_service_id);
    
                    // Fetch image data from the corresponding URL
                    $luxury_interior_image_data = file_get_contents($luxury_interior_image_urls[$luxury_interior_i]);
    
                    if ($luxury_interior_image_data !== false) {
                        // Prepare file information
                        $luxury_interior_upload_dir = wp_upload_dir();
                        if (!file_exists($luxury_interior_upload_dir['path'])) {
                            error_log("Upload directory does not exist: " . $luxury_interior_upload_dir['path']);
                            continue; // Skip to the next service
                        }
    
                        // Use a consistent file name for the image
                        $luxury_interior_image_name = 'interior-service' . ($luxury_interior_i + 1) . '.png'; // Example: service1.png
                        $luxury_interior_file = $luxury_interior_upload_dir['path'] . '/' . $luxury_interior_image_name;
    
                        // Use WordPress Filesystem API to write the image data
                        if (!$wp_filesystem->exists($luxury_interior_file)) {
                            if ($wp_filesystem->put_contents($luxury_interior_file, $luxury_interior_image_data, FS_CHMOD_FILE) === false) {
                                error_log("Failed to write image to file: $luxury_interior_file");
                                continue; // Skip to the next service
                            }
                        }
    
                        // Check the file type
                        $luxury_interior_wp_filetype = wp_check_filetype($luxury_interior_image_name, null);
                        if (!$luxury_interior_wp_filetype['type']) {
                            error_log("Failed to determine MIME type for file: $luxury_interior_image_name");
                            continue; // Skip to the next service
                        }
    
                        // Prepare attachment data
                        $luxury_interior_attachment = array(
                            'post_mime_type' => $luxury_interior_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($luxury_interior_image_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
    
                        // Insert the attachment into the media library
                        $luxury_interior_attach_id = wp_insert_attachment($luxury_interior_attachment, $luxury_interior_file, $luxury_interior_service_id);
                        if (is_wp_error($luxury_interior_attach_id)) {
                            error_log("Failed to insert attachment: " . $luxury_interior_attach_id->get_error_message());
                            continue; // Skip to the next service
                        }
    
                        // Generate attachment metadata
                        $luxury_interior_attach_data = wp_generate_attachment_metadata($luxury_interior_attach_id, $luxury_interior_file);
                        if (!$luxury_interior_attach_data) {
                            error_log("Failed to generate attachment metadata for ID: $luxury_interior_attach_id");
                            continue; // Skip to the next service
                        }
    
                        // Update attachment metadata
                        wp_update_attachment_metadata($luxury_interior_attach_id, $luxury_interior_attach_data);
    
                        // Set the attachment as the post's featured image
                        if (!set_post_thumbnail($luxury_interior_service_id, $luxury_interior_attach_id)) {
                            error_log("Failed to set featured image for post ID: $luxury_interior_service_id");
                        } else {
                            error_log("Successfully set featured image for post ID: $luxury_interior_service_id");
                        }
                    } else {
                        error_log("Failed to fetch image data from URL: " . $luxury_interior_image_urls[$luxury_interior_i]);
                    }
                } else {
                    error_log("Failed to create post: " . print_r($luxury_interior_service_id, true));
                }
        }

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