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

        // If the demo import is completed, display the "View Site" button
        if ($mediquip_plus_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.', 'mediquip-plus' ) . '</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>';
        }
     // POST and update the customizer and other related data of Mediquip Plus
    if ( isset( $_POST['submit'] ) ) {

        // 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
            $mediquip_plus_plugin_slug = 'classic-blog-grid';
            $mediquip_plus_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
        
            // Check if Classic Blog Grid is installed and activated
            if ( ! is_plugin_active( $mediquip_plus_plugin_file ) ) {
        
                // Check if Classic Blog Grid is installed
                $mediquip_plus_installed_plugins = get_plugins();
                if ( ! isset( $mediquip_plus_installed_plugins[ $mediquip_plus_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
                    $mediquip_plus_upgrader = new Plugin_Upgrader();
                    $mediquip_plus_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( $mediquip_plus_plugin_file );
            }
        }

        // ------- Create Main Menu --------
        $mediquip_plus_menuname = 'Primary Menu';
        $mediquip_plus_bpmenulocation = 'primary';
        $mediquip_plus_menu_exists = wp_get_nav_menu_object( $mediquip_plus_menuname );
    
        if (!$mediquip_plus_menu_exists) {
            // Create a new menu
            $mediquip_plus_menu_id = wp_create_nav_menu($mediquip_plus_menuname);

            // Define pages to be created
            $mediquip_plus_pages = array(
                'home' => array(
                    'title' => 'Home',
                    'template' => '/template-home-page.php'
                ),
                'about-us' => array(
                    'title' => 'About Us',
                    '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>'
                ),
                'pages' => array(
                    'title' => '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>'
                ),
                'blogs' => array(
                    'title' => 'Blogs',
                    'content' => ''
                ),
            );

            $mediquip_plus_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($mediquip_plus_pages as $mediquip_plus_slug => $mediquip_plus_data) {
                $mediquip_plus_existing_page = get_page_by_path($mediquip_plus_slug);

                if ($mediquip_plus_existing_page) {
                    // If the page already exists, use its ID
                    $mediquip_plus_page_id = $mediquip_plus_existing_page->ID;
                } else {
                    // Create a new page
                    $mediquip_plus_page_data = array(
                        'post_type'    => 'page',
                        'post_title'   => $mediquip_plus_data['title'],
                        'post_content' => isset($mediquip_plus_data['content']) ? $mediquip_plus_data['content'] : '',
                        'post_status'  => 'publish',
                        'post_author'  => get_current_user_id(), // Set author dynamically
                        'post_name'    => $mediquip_plus_slug,
                    );

                    $mediquip_plus_page_id = wp_insert_post($mediquip_plus_page_data);

                    // Assign custom page template if specified
                    if (!empty($mediquip_plus_data['template'])) {
                        update_post_meta($mediquip_plus_page_id, '_wp_page_template', $mediquip_plus_data['template']);
                    }
                }

                // Store the page IDs
                $mediquip_plus_page_ids[$mediquip_plus_slug] = $mediquip_plus_page_id;
            }

            // Set homepage and blog page
            update_option('page_for_posts', $mediquip_plus_page_ids['blogs']);
            update_option('page_on_front', $mediquip_plus_page_ids['home']);
            update_option('show_on_front', 'page');

            // Define menu items
            $mediquip_plus_menu_items = array(
                'home',
                'about-us',
                'pages',
                'blogs',
            );

            // Add menu items dynamically
            foreach ($mediquip_plus_menu_items as $mediquip_plus_slug) {
                wp_update_nav_menu_item($mediquip_plus_menu_id, 0, array(
                    'menu-item-title' => esc_html($mediquip_plus_pages[$mediquip_plus_slug]['title']),
                    'menu-item-url' => get_permalink($mediquip_plus_page_ids[$mediquip_plus_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $mediquip_plus_page_ids[$mediquip_plus_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $mediquip_plus_locations = get_theme_mod('nav_menu_locations', array());
            $mediquip_plus_locations[$mediquip_plus_bpmenulocation] = $mediquip_plus_menu_id;
            set_theme_mod('nav_menu_locations', $mediquip_plus_locations);
        }

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

        //Header Section
        set_theme_mod( 'mediquip_plus_search_option', true);
        set_theme_mod( 'mediquip_plus_location_text', '320 Willis Avenue');
        set_theme_mod( 'mediquip_plus_location', 'Los Angles, California, US');
        set_theme_mod( 'mediquip_plus_time_text', 'Sun: Emergency Only');
        set_theme_mod( 'mediquip_plus_time', 'Mon-Fri 9:00am to 6:00pm');
        set_theme_mod( 'mediquip_plus_phone_number_text', 'TOLL FREE');
        set_theme_mod( 'mediquip_plus_phone_number', '123 456 7890');

        //Slider Section
        set_theme_mod( 'mediquip_plus_hide_categorysec', true);
        set_theme_mod( 'mediquip_plus_button_text', 'MAKE AN APPOINTMENT');
        
        $mediquip_plus_slider_category_id = wp_create_category('Slider');

        // Set the category in theme mods for the slider section
        set_theme_mod('mediquip_plus_slidersection', $mediquip_plus_slider_category_id); // Update with correct category ID
        
        // Titles for the three posts
        $mediquip_plus_titles = array(
            'Advanced Equipment for Better Care',
            'Reliable Solutions for Medical Professionals',
            'Innovation That Supports Healing'
        );
       
        // Create three demo posts and assign them to the 'Slider' category
        for ($mediquip_plus_i = 1; $mediquip_plus_i <= 3; $mediquip_plus_i++) {
            $mediquip_plus_title = $mediquip_plus_titles[$mediquip_plus_i - 1];
            $mediquip_plus_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.';
        
            // Prepare the post object
            $mediquip_plus_my_post = array(
                'post_title'    => wp_strip_all_tags($mediquip_plus_title),
                'post_content'  => $mediquip_plus_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($mediquip_plus_slider_category_id),
            );
        
            // Insert the post into the database
            $mediquip_plus_post_id = wp_insert_post($mediquip_plus_my_post);
        
            // If the post was successfully created, set the featured image
            if ($mediquip_plus_post_id && !is_wp_error($mediquip_plus_post_id)) {
                // Set the image URL based on the current slider index
                $mediquip_plus_image_url = esc_url(get_stylesheet_directory_uri() . '/images/slider' . $mediquip_plus_i . '.png');
                $mediquip_plus_upload_dir = wp_upload_dir();
        
                // Download the image data using wp_remote_get()
                $mediquip_plus_response = wp_remote_get($mediquip_plus_image_url);
                if (!is_wp_error($mediquip_plus_response)) {
                    $mediquip_plus_image_data = wp_remote_retrieve_body($mediquip_plus_response);
        
                    if (!empty($mediquip_plus_image_data)) {
                        // Handle the file upload process
                        $mediquip_plus_image_name = 'slider' . $mediquip_plus_i . '.png';
                        $mediquip_plus_unique_file_name = wp_unique_filename($mediquip_plus_upload_dir['path'], $mediquip_plus_image_name);
                        $mediquip_plus_file = $mediquip_plus_upload_dir['path'] . '/' . $mediquip_plus_unique_file_name;
        
                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($mediquip_plus_file, $mediquip_plus_image_data);
        
                        // Check file type and prepare for attachment
                        $mediquip_plus_wp_filetype = wp_check_filetype($mediquip_plus_unique_file_name, null);
                        $mediquip_plus_attachment = array(
                            'post_mime_type' => $mediquip_plus_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($mediquip_plus_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
        
                        // Insert the image into the media library and set it as the post's featured image
                        $mediquip_plus_attach_id = wp_insert_attachment($mediquip_plus_attachment, $mediquip_plus_file, $mediquip_plus_post_id);
                        $mediquip_plus_attach_data = wp_generate_attachment_metadata($mediquip_plus_attach_id, $mediquip_plus_file);
                        wp_update_attachment_metadata($mediquip_plus_attach_id, $mediquip_plus_attach_data);
                        set_post_thumbnail($mediquip_plus_post_id, $mediquip_plus_attach_id);
                    }
                }
            }
        }

        //Facility Section
        set_theme_mod( 'mediquip_plus_hidcatfacility', true);
        set_theme_mod( 'mediquip_plus_section_title', 'OUR HOSPITAL FACILITIES');
        
        $mediquip_plus_facility_category_id = wp_create_category('Facility');

        // Set the category in theme mods for the facility section
        set_theme_mod('mediquip_plus_facilities_section', $mediquip_plus_facility_category_id); // Update with correct category ID
        
        // Titles for the three posts
        $mediquip_plus_titles = array(
            'Emergency Care',
            'Cancer Services',
            'Blood Test'
        );        
        // Create three demo posts and assign them to the 'facility' category
        for ($mediquip_plus_i = 1; $mediquip_plus_i <= 3; $mediquip_plus_i++) {
            $mediquip_plus_title = $mediquip_plus_titles[$mediquip_plus_i - 1];
            $mediquip_plus_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.';
        
            // Prepare the post object
            $mediquip_plus_my_post = array(
                'post_title'    => wp_strip_all_tags($mediquip_plus_title),
                'post_content'  => $mediquip_plus_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($mediquip_plus_facility_category_id),
            );
        
            // Insert the post into the database
            $mediquip_plus_post_id = wp_insert_post($mediquip_plus_my_post);
        
            // If the post was successfully created, set the featured image
            if ($mediquip_plus_post_id && !is_wp_error($mediquip_plus_post_id)) {
                // Set the image URL based on the current facility index
                $mediquip_plus_image_url = esc_url(get_stylesheet_directory_uri() . '/images/facility' . $mediquip_plus_i . '.png');
                $mediquip_plus_upload_dir = wp_upload_dir();
        
                // Download the image data using wp_remote_get()
                $mediquip_plus_response = wp_remote_get($mediquip_plus_image_url);
                if (!is_wp_error($mediquip_plus_response)) {
                    $mediquip_plus_image_data = wp_remote_retrieve_body($mediquip_plus_response);
        
                    if (!empty($mediquip_plus_image_data)) {
                        // Handle the file upload process
                        $mediquip_plus_image_name = 'facility' . $mediquip_plus_i . '.png';
                        $mediquip_plus_unique_file_name = wp_unique_filename($mediquip_plus_upload_dir['path'], $mediquip_plus_image_name);
                        $mediquip_plus_file = $mediquip_plus_upload_dir['path'] . '/' . $mediquip_plus_unique_file_name;
        
                        // Save the image file to the uploads directory
                        global $wp_filesystem;
                        WP_Filesystem();
                        $wp_filesystem->put_contents($mediquip_plus_file, $mediquip_plus_image_data);
        
                        // Check file type and prepare for attachment
                        $mediquip_plus_wp_filetype = wp_check_filetype($mediquip_plus_unique_file_name, null);
                        $mediquip_plus_attachment = array(
                            'post_mime_type' => $mediquip_plus_wp_filetype['type'],
                            'post_title'     => sanitize_file_name($mediquip_plus_unique_file_name),
                            'post_content'   => '',
                            'post_status'    => 'inherit',
                        );
        
                        // Insert the image into the media library and set it as the post's featured image
                        $mediquip_plus_attach_id = wp_insert_attachment($mediquip_plus_attachment, $mediquip_plus_file, $mediquip_plus_post_id);
                        $mediquip_plus_attach_data = wp_generate_attachment_metadata($mediquip_plus_attach_id, $mediquip_plus_file);
                        wp_update_attachment_metadata($mediquip_plus_attach_id, $mediquip_plus_attach_data);
                        set_post_thumbnail($mediquip_plus_post_id, $mediquip_plus_attach_id);
                    }
                }
            }
        }

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