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

        // If the demo import is completed, display the "View Site" button
        if ($classic_attorney_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-attorney' ) . '</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 Classic Attorney
    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
                $classic_attorney_plugin_slug = 'classic-blog-grid';
                $classic_attorney_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $classic_attorney_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $classic_attorney_installed_plugins = get_plugins();
                    if ( ! isset( $classic_attorney_installed_plugins[ $classic_attorney_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_attorney_upgrader = new Plugin_Upgrader();
                        $classic_attorney_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_attorney_plugin_file );
                }
            }
        echo '</div>';
        // ------- Create Main Menu --------
        $classic_attorney_menuname = 'Primary Menu';
        $classic_attorney_bpmenulocation = 'primary';
        $classic_attorney_menu_exists = wp_get_nav_menu_object( $classic_attorney_menuname );
    
        if (!$classic_attorney_menu_exists) {
            // Create a new menu
            $classic_attorney_menu_id = wp_create_nav_menu($classic_attorney_menuname);

            // Define pages to be created
            $classic_attorney_pages = array(
                'home' => array(
                    'title' => 'Home',
                    'template' => '/templates/template-home-page.php'
                ),
                'our-attorneys' => array(
                    'title' => 'Our Attorneys',
                    '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>'
                ),
                '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>'
                ),
                'case-results' => array(
                    'title' => 'Case Results',
                    '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>'
                ),
                'blog' => array(
                    'title' => 'Blog',
                    'content' => ''
                ),
            );

            $classic_attorney_page_ids = array();

            // Loop through the pages and create them if they don’t exist
            foreach ($classic_attorney_pages as $classic_attorney_slug => $classic_attorney_data) {
                $classic_attorney_existing_page = get_page_by_path($classic_attorney_slug);

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

                    $classic_attorney_page_id = wp_insert_post($classic_attorney_page_data);

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

                // Store the page IDs
                $classic_attorney_page_ids[$classic_attorney_slug] = $classic_attorney_page_id;
            }

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

            // Define menu items
            $classic_attorney_menu_items = array(
                'home',
                'our-attorneys',
                'about-us',
                'case-results',
                'blog',
            );

            // Add menu items dynamically
            foreach ($classic_attorney_menu_items as $classic_attorney_slug) {
                wp_update_nav_menu_item($classic_attorney_menu_id, 0, array(
                    'menu-item-title' => esc_html($classic_attorney_pages[$classic_attorney_slug]['title']),
                    'menu-item-url' => get_permalink($classic_attorney_page_ids[$classic_attorney_slug]),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $classic_attorney_page_ids[$classic_attorney_slug],
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type',
                ));
            }

            // Assign menu to theme location
            $classic_attorney_locations = get_theme_mod('nav_menu_locations', array());
            $classic_attorney_locations[$classic_attorney_bpmenulocation] = $classic_attorney_menu_id;
            set_theme_mod('nav_menu_locations', $classic_attorney_locations);
        }

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

        //Topbar Section
        set_theme_mod( 'classic_attorney_show_topbar', true);
        set_theme_mod( 'classic_attorney_instagram_link', 'https://www.instagram.com');
        set_theme_mod( 'classic_attorney_facebook_link', 'https://www.facebook.com');
        set_theme_mod( 'classic_attorney_youtube_link', 'https://www.youtube.com');
        set_theme_mod( 'classic_attorney_twitter_link', 'https://www.twitter.com');
        set_theme_mod( 'classic_attorney_topbar_text_title', 'Delivering excellence since 2001.');
        set_theme_mod( 'classic_attorney_phone_text_title', 'Have any question?');
        set_theme_mod( 'classic_attorney_phone_number', '+(528) 456-7592');
        set_theme_mod( 'classic_attorney_header_contact_btn', 'Contact Us');
        set_theme_mod( 'classic_attorney_header_contact_btn_url', '#');
        set_theme_mod( 'classic_attorney_header_login_btn', 'Login');
        set_theme_mod( 'classic_attorney_header_login_btn_url', '#');

        //Slider Section
        set_theme_mod( 'classic_attorney_slider', true);
        set_theme_mod( 'classic_attorney_banner_background_img', esc_url( get_template_directory_uri().'/images/Slider-bg.png'));
        set_theme_mod( 'classic_attorney_slider_subhead', 'Trusted legal experts committed to protecting your rights.');
        set_theme_mod( 'classic_attorney_services_icon', 'fa-solid fa-scale-balanced');

        
        $classic_attorney_slider_category_id = wp_create_category('Slider');
        // Set the category in theme mods for the slider section
        set_theme_mod('classic_attorney_slider_cat', 'Slider'); // Update with correct category ID
        
       // Titles for the three posts
        $classic_attorney_titles = array(
            'Expert Legal Solutions for Your Rights, Justice, and Peace of Mind.',
            'Your justice is our priority.',
            'Experienced attorneys. Proven results.'
        );
       
        // Create three demo posts and assign them to the 'Slider' category
        for ($classic_attorney_i = 1; $classic_attorney_i <= 3; $classic_attorney_i++) {
            $classic_attorney_title = $classic_attorney_titles[$classic_attorney_i - 1];
            $classic_attorney_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
            $classic_attorney_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_attorney_title),
                'post_content'  => $classic_attorney_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_attorney_slider_category_id),
            );
        
            // Insert the post into the database
            $classic_attorney_post_id = wp_insert_post($classic_attorney_my_post);
        }

        //Services Section
        set_theme_mod( 'classic_attorney_disabled_services_section', true);
        set_theme_mod( 'classic_attorney_services_title', 'Services WE Offer');

        // Function to fetch or create a page using WP_Query
        function get_or_create_page_by_title( $classic_attorney_page_title, $classic_attorney_page_content = '' ) {
            $classic_attorney_args = array(
                'post_type'      => 'page',
                'title'          => $classic_attorney_page_title,
                'post_status'    => 'publish',
                'posts_per_page' => 1,
                'fields'         => 'ids'
            );
            $classic_attorney_query = new WP_Query( $classic_attorney_args );

            if ( ! empty( $classic_attorney_query->posts ) ) {
                return $classic_attorney_query->posts[0];
            } else {
                // Create the page if it doesn't exist
                $classic_attorney_page_id = wp_insert_post( array(
                    'post_type'    => 'page',
                    'post_title'   => $classic_attorney_page_title,
                    'post_content' => $classic_attorney_page_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1
                ));
                return $classic_attorney_page_id;
            }
        }

        // Create Page
        $classic_attorney_page_title = 'Trusted Legal Services to Protect Your Rights and Interests.';
        $classic_attorney_page_content = 'At [Your Law Firm Name], we offer expert legal representation in personal injury, criminal defense, family law, business law, real estate, and estate planning. Our experienced attorneys provide strategic solutions tailored to your needs, ensuring strong advocacy and the best possible outcomes. Whether you need legal advice or courtroom representation, we are here to help. Contact us today to discuss your case and protect your rights!';
        $classic_attorney_page_id = get_or_create_page_by_title( $classic_attorney_page_title, $classic_attorney_page_content );

        if ( $classic_attorney_page_id ) {
            set_theme_mod( 'classic_attorney_about_pageboxes', $classic_attorney_page_id );
        } else {
            error_log('Failed to create or fetch the "Welcome to Corporate Business Theme" page.');
        }

        $classic_attorney_image_url = get_template_directory_uri().'/images/page.png';
        $classic_attorney_image_id = media_sideload_image($classic_attorney_image_url, $classic_attorney_page_id, null, 'id');
        if (!is_wp_error($classic_attorney_image_id)) {
            // Set the downloaded image as the post's featured image
            set_post_thumbnail($classic_attorney_page_id, $classic_attorney_image_id);
        }      

        //create category
        $classic_attorney_services_category_id = wp_create_category('services');
        // Set the category in theme mods for the services section
        set_theme_mod('classic_attorney_services_sec_cat', 'services'); // Update with correct category ID
        
        // Titles for the three posts
        $classic_attorney_titles = array(
            'Personal Injury Law',
            'Criminal Defense',
            'Family Law',
            'Business & Corporate Law',
        );        
        // Create three demo posts and assign them to the 'services' category
        for ($classic_attorney_i = 1; $classic_attorney_i <= 4; $classic_attorney_i++) {
            $classic_attorney_title = $classic_attorney_titles[$classic_attorney_i - 1];
            $classic_attorney_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
            $classic_attorney_my_post = array(
                'post_title'    => wp_strip_all_tags($classic_attorney_title),
                'post_content'  => $classic_attorney_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($classic_attorney_services_category_id),
            );
        
            // Insert the post into the database
            $classic_attorney_post_id = wp_insert_post($classic_attorney_my_post);
        }

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