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

        // If the demo import is completed, display the "View Site" button
        if ($live_artist_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.', 'live-artist' ) . '</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
                $live_artist_plugin_slug = 'classic-blog-grid';
                $live_artist_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $live_artist_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $live_artist_installed_plugins = get_plugins();
                    if ( ! isset( $live_artist_installed_plugins[ $live_artist_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
                        $live_artist_upgrader = new Plugin_Upgrader();
                        $live_artist_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( $live_artist_plugin_file );
                }
            }
    echo '</div>';
        // ------- Create Main Menu --------
        $live_artist_menuname = 'Primary Menu';
        $live_artist_bpmenulocation = 'primary';
        $live_artist_menu_exists = wp_get_nav_menu_object( $live_artist_menuname );
    
        if ( !$live_artist_menu_exists ) {
            $live_artist_menu_id = wp_create_nav_menu( $live_artist_menuname );

            // Create Home Page
            $live_artist_home_title = 'Home';
            $live_artist_home = array(
                'post_type'    => 'page',
                'post_title'   => $live_artist_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $live_artist_home_id = wp_insert_post($live_artist_home);
            // Assign Home Page Template
            add_post_meta($live_artist_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', $live_artist_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($live_artist_menu_id, 0, array(
                'menu-item-title' => __('Home', 'live-artist'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $live_artist_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $live_artist_about_title = 'About';
            $live_artist_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>';
            $live_artist_about = array(
                'post_type'    => 'page',
                'post_title'   => $live_artist_about_title,
                'post_content' => $live_artist_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'about'
            );
            $live_artist_about_id = wp_insert_post($live_artist_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($live_artist_menu_id, 0, array(
                'menu-item-title' => __('About', 'live-artist'),
                'menu-item-classes' => 'about',
                'menu-item-url' => home_url('/about/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $live_artist_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $live_artist_about_title = 'Music';
            $live_artist_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>';
            $live_artist_about = array(
                'post_type'    => 'page',
                'post_title'   => $live_artist_about_title,
                'post_content' => $live_artist_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'music'
            );
            $live_artist_about_id = wp_insert_post($live_artist_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($live_artist_menu_id, 0, array(
                'menu-item-title' => __('Music', 'live-artist'),
                'menu-item-classes' => 'music',
                'menu-item-url' => home_url('/music/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $live_artist_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $live_artist_about_title = 'Pages';
            $live_artist_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>';
            $live_artist_about = array(
                'post_type'    => 'page',
                'post_title'   => $live_artist_about_title,
                'post_content' => $live_artist_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $live_artist_about_id = wp_insert_post($live_artist_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($live_artist_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'live-artist'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $live_artist_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create a new Blog Page 
            $live_artist_blog_title = 'Blogs';
            $live_artist_blog_content = '<p>Welcome to our blog section! Here you will find the latest updates, news, and insights.</p>';
            $live_artist_blog_page = array(
                'post_type'    => 'page',
                'post_title'   => $live_artist_blog_title,
                'post_content' => $live_artist_blog_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_name'    => 'blogs' // Slug updated to 'blogs'
            );
            $live_artist_blog_id = wp_insert_post($live_artist_blog_page);

            // Set the newly created page as the posts page (blog page)
            update_option('page_for_posts', $live_artist_blog_id);

            // Add Blogs Page to Menu
            wp_update_nav_menu_item($live_artist_menu_id, 0, array(
                'menu-item-title'    => __('Blogs', 'live-artist'),
                'menu-item-classes'  => 'blogs',
                'menu-item-url'      => home_url('/blogs/'),
                'menu-item-status'   => 'publish',
                'menu-item-object-id'=> $live_artist_blog_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( $live_artist_bpmenulocation ) ) {
                $live_artist_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $live_artist_locations ) ) {
                    $live_artist_locations = array();
                }
                $live_artist_locations[ $live_artist_bpmenulocation ] = $live_artist_menu_id;
                set_theme_mod( 'nav_menu_locations', $live_artist_locations );
            }
        }

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

        //Header Section
        set_theme_mod( 'live_artist_cart_btn_link', '#');
        set_theme_mod( 'live_artist_header_btn_text', 'Buy Ticket');
        set_theme_mod( 'live_artist_header_btn_url', '#');

        //Banner Section
        set_theme_mod( 'live_artist_banner_small_title', 'Hello I Am Music Artist');
        set_theme_mod( 'live_artist_small_title_icon', 'fa-solid fa-microphone-lines');
        set_theme_mod( 'live_artist_button_text', 'Explore More');
        set_theme_mod( 'live_artist_banner_artist_name', 'Justin B Rider');
        set_theme_mod( 'live_artist_banner_artist_field', 'Music Artist');
        set_theme_mod( 'live_artist_facebook_link', 'https://www.facebook.com');
        set_theme_mod( 'live_artist_instagram_link', 'https://www.instagram.com');
        set_theme_mod( 'live_artist_pinterest_link', 'https://www.pinterest.com');
        set_theme_mod( 'live_artist_twitter_link', 'https://www.twitter.com');
        set_theme_mod( 'live_artist_dribbble_link', 'https://www.dribbble.com');
        set_theme_mod( 'live_artist_youtube_link', 'https://www.youtube.com');
      
        $live_artist_banner_title = 'Music Is The Shorthand of Emotion';
        $live_artist_banner_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."</p>';
        
        // Create post object
        $live_artist_my_post = array(
            'post_title'   => wp_strip_all_tags($live_artist_banner_title),
            'post_content' => $live_artist_banner_content,
            'post_status'  => 'publish',
            'post_type'    => 'page',
        );
        
        // Insert the post into the database
        $live_artist_post_id = wp_insert_post($live_artist_my_post);
        
        if ($live_artist_post_id) {
            // Set the theme mod for the banner page
            set_theme_mod('live_artist_banner_pageboxes', $live_artist_post_id);
        
            // Include necessary WordPress files
            require_once(ABSPATH . 'wp-admin/includes/media.php');
            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . 'wp-admin/includes/image.php');
        
            // Define the image URL
            $live_artist_image_url = get_template_directory_uri() . '/images/banner.png';
        
            // Download and attach the image to the post
            $live_artist_image_id = media_sideload_image($live_artist_image_url, $live_artist_post_id, null, 'id');
        
            if (!is_wp_error($live_artist_image_id)) {
                // Set the downloaded image as the post's featured image
                set_post_thumbnail($live_artist_post_id, $live_artist_image_id);
            }
        }

        //Album Section
        set_theme_mod( 'live_artist_album_text_icon', 'fa-solid fa-microphone-lines');
        set_theme_mod( 'live_artist_album_text', 'Albums');
        set_theme_mod( 'live_artist_album_title', 'Releases New Songs and Music');

        $live_artist_featured_category_id = wp_create_category('Albums');
        set_theme_mod('live_artist_album_cat', 'Albums');
        
        $live_artist_titles = array('New Classic Music 1', 'New Classic Music 2', 'New Classic Music 3', 'New Classic Music 4', 'New Classic Music 5');
        $live_artist_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."</p>';
        
        for ($live_artist_i = 0; $live_artist_i < 5; $live_artist_i++) { // Fixed loop iteration
            set_theme_mod('live_artist_title' . ($live_artist_i + 1), $live_artist_titles[$live_artist_i]);
        
            $live_artist_my_post = array(
                'post_title'    => wp_strip_all_tags($live_artist_titles[$live_artist_i]),
                'post_content'  => $live_artist_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($live_artist_featured_category_id),
            );
        
            $live_artist_post_id = wp_insert_post($live_artist_my_post);
        
            if (!is_wp_error($live_artist_post_id)) {
                $live_artist_image_url = get_template_directory_uri() . '/images/album' . ($live_artist_i + 1) . '.png';
                $live_artist_image_id = media_sideload_image($live_artist_image_url, $live_artist_post_id, null, 'id');
                if (!is_wp_error($live_artist_image_id)) {
                    set_post_thumbnail($live_artist_post_id, $live_artist_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $live_artist_post_id);
                }
            } else {
                error_log('Failed to create post: ' . print_r($live_artist_post_id, true));
            }
        }        

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