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

        // If the demo import is completed, display the "View Site" button
        if ($shipping_company_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.', 'shipping-company' ) . '</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
                $shipping_company_plugin_slug = 'classic-blog-grid';
                $shipping_company_plugin_file = 'classic-blog-grid/classic-blog-grid.php';
            
                // Check if Classic Blog Grid is installed and activated
                if ( ! is_plugin_active( $shipping_company_plugin_file ) ) {
            
                    // Check if Classic Blog Grid is installed
                    $shipping_company_installed_plugins = get_plugins();
                    if ( ! isset( $shipping_company_installed_plugins[ $shipping_company_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
                        $shipping_company_upgrader = new Plugin_Upgrader();
                        $shipping_company_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( $shipping_company_plugin_file );
                }
            }
        echo '</div>';
        // ------- Create Main Menu --------
        $shipping_company_menuname = 'Primary Menu';
        $shipping_company_bpmenulocation = 'primary';
        $shipping_company_menu_exists = wp_get_nav_menu_object( $shipping_company_menuname );
    
        if ( !$shipping_company_menu_exists ) {
            $shipping_company_menu_id = wp_create_nav_menu( $shipping_company_menuname );

            // Create Home Page
            $shipping_company_home_title = 'Home';
            $shipping_company_home = array(
                'post_type'    => 'page',
                'post_title'   => $shipping_company_home_title,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'home'
            );
            $shipping_company_home_id = wp_insert_post($shipping_company_home);
            // Assign Home Page Template
            add_post_meta($shipping_company_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', $shipping_company_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($shipping_company_menu_id, 0, array(
                'menu-item-title' => __('Home', 'shipping-company'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $shipping_company_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $shipping_company_about_title = 'Pages';
            $shipping_company_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>';
            $shipping_company_about = array(
                'post_type'    => 'page',
                'post_title'   => $shipping_company_about_title,
                'post_content' => $shipping_company_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'pages'
            );
            $shipping_company_about_id = wp_insert_post($shipping_company_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($shipping_company_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'shipping-company'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $shipping_company_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $shipping_company_about_title = 'Services';
            $shipping_company_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>';
            $shipping_company_about = array(
                'post_type'    => 'page',
                'post_title'   => $shipping_company_about_title,
                'post_content' => $shipping_company_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'services'
            );
            $shipping_company_about_id = wp_insert_post($shipping_company_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($shipping_company_menu_id, 0, array(
                'menu-item-title' => __('Services', 'shipping-company'),
                'menu-item-classes' => 'services',
                'menu-item-url' => home_url('/services/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $shipping_company_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $shipping_company_about_title = 'Projects';
            $shipping_company_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>';
            $shipping_company_about = array(
                'post_type'    => 'page',
                'post_title'   => $shipping_company_about_title,
                'post_content' => $shipping_company_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'projects'
            );
            $shipping_company_about_id = wp_insert_post($shipping_company_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($shipping_company_menu_id, 0, array(
                'menu-item-title' => __('Projects', 'shipping-company'),
                'menu-item-classes' => 'projects',
                'menu-item-url' => home_url('/projects/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $shipping_company_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

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

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

            // Add Blogs Page to Menu
            wp_update_nav_menu_item($shipping_company_menu_id, 0, array(
                'menu-item-title'    => __('Blogs', 'shipping-company'),
                'menu-item-classes'  => 'blogs',
                'menu-item-url'      => home_url('/blogs/'),
                'menu-item-status'   => 'publish',
                'menu-item-object-id'=> $shipping_company_blog_id,
                'menu-item-object'   => 'page',
                'menu-item-type'     => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $shipping_company_about_title = 'Contact';
            $shipping_company_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>';
            $shipping_company_about = array(
                'post_type'    => 'page',
                'post_title'   => $shipping_company_about_title,
                'post_content' => $shipping_company_about_content,
                'post_status'  => 'publish',
                'post_author'  => 1,
                'post_slug'    => 'contact'
            );
            $shipping_company_about_id = wp_insert_post($shipping_company_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($shipping_company_menu_id, 0, array(
                'menu-item-title' => __('Contact', 'shipping-company'),
                'menu-item-classes' => 'contact',
                'menu-item-url' => home_url('/contact/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $shipping_company_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( $shipping_company_bpmenulocation ) ) {
                $shipping_company_locations = get_theme_mod( 'nav_menu_locations' );
                if ( empty( $shipping_company_locations ) ) {
                    $shipping_company_locations = array();
                }
                $shipping_company_locations[ $shipping_company_bpmenulocation ] = $shipping_company_menu_id;
                set_theme_mod( 'nav_menu_locations', $shipping_company_locations );
            }
        }

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

        //Topbar Section
        set_theme_mod( 'shipping_company_header_location_text', '120, Rose Garden, CA Road, USA');
        set_theme_mod( 'shipping_company_header_track_order_text', 'Track Your Order');
        set_theme_mod( 'shipping_company_header_time_text', 'Mon - Sat: 8 am - 6 pm');
        set_theme_mod( 'shipping_company_header_btn_text', 'Get A Quote');

        //Banner Section
        set_theme_mod( 'shipping_company_banner', true);
        set_theme_mod( 'shipping_company_button_text', 'Get A Quote');

        // Create a single page 
        $shipping_company_banner_title = 'Seamless Shipping, Limitless Reach';
        $shipping_company_banner_content = 'Streamline your logistics with our all-in-one platform, ensuring seamless transportation and delivery solutions.';

        $shipping_company_post = array(
            'post_title'    => wp_strip_all_tags($shipping_company_banner_title),
            'post_content'  => $shipping_company_banner_content,
            'post_status'   => 'publish',
            'post_type'     => 'page',
        );

        // Insert the post into the database
        $shipping_company_post_id = wp_insert_post($shipping_company_post);

        // If the post was successfully created
        if ($shipping_company_post_id && !is_wp_error($shipping_company_post_id)) {
            // Set the theme mod for the single banner page
            set_theme_mod('shipping_company_banner_pageboxes', $shipping_company_post_id);

            // Set the featured image for the post
            $shipping_company_image_url = get_template_directory_uri() . '/images/banner.png';
            
            // Use media_sideload_image to upload and set the image
            $shipping_company_image_id = media_sideload_image($shipping_company_image_url, $shipping_company_post_id, null, 'id');

            // Check if the image was successfully uploaded and set
            if (!is_wp_error($shipping_company_image_id)) {
                // Set the image as the post's featured image
                set_post_thumbnail($shipping_company_post_id, $shipping_company_image_id);
            } else {
                // Log or handle the error
                error_log("Error setting the featured image: " . $shipping_company_image_id->get_error_message());
            }

            // Set the single created page as the default page for the banner
            set_theme_mod('shipping_company_selected_banner_page', $shipping_company_post_id);

        } else {
            // Log or handle the error if the page creation failed
            error_log("Error creating the page: " . $shipping_company_post_id->get_error_message());
        }

        // Logistic Solution Section
        set_theme_mod('shipping_company_disabled_logistic_solution_section', true);
        set_theme_mod( 'shipping_company_logistic_solution_title', 'The Ultimate Logistic Solution');
        set_theme_mod( 'shipping_company_logistic_btn_text', 'Contact Us');

        $shipping_company_tab_names = array(
            'All', 
            'Packaging & Storage', 
            'Warehousing', 
            'Cargo Service', 
            'Worldwide Transport', 
            'Door to Door Delivery', 
            'Ground Transport'
        );
        
        // Create Logistic category if it doesn't exist
        $shipping_company_logistic_category = get_category_by_slug('logistic');
        if (!$shipping_company_logistic_category) {
            $shipping_company_logistic_category_id = wp_create_category('Logistic');
        } else {
            $shipping_company_logistic_category_id = $shipping_company_logistic_category->term_id;
        }
        
        // Store the Logistic category ID in theme mod
        set_theme_mod('shipping_company_posts_cat', $shipping_company_logistic_category_id);
        
        // Set tab titles using predefined names
        for ($shipping_company_i = 1; $shipping_company_i <= 6; $shipping_company_i++) {
            set_theme_mod('shipping_company_logistic_tab_title' . $shipping_company_i, $shipping_company_tab_names[$shipping_company_i]);
            
            // Assign only the "Logistic" category
            set_theme_mod('shipping_company_posts_cat' . $shipping_company_i, 'logistic');
        }
        
        // Titles and content for posts
        $shipping_company_titles = array(
            'Train Freight',  
            'Ship Freight',  
            'Truck Freight'  
        );                
        $shipping_company_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
        
        // Insert shipping company posts
        for ($shipping_company_i = 0; $shipping_company_i < 3; $shipping_company_i++) {
            set_theme_mod('shipping_company_title' . ($shipping_company_i + 1), $shipping_company_titles[$shipping_company_i]);
        
            $shipping_company_my_post = array(
                'post_title'    => wp_strip_all_tags($shipping_company_titles[$shipping_company_i]),
                'post_content'  => $shipping_company_content,
                'post_status'   => 'publish',
                'post_type'     => 'post',
                'post_category' => array($shipping_company_logistic_category_id), // Ensure the Logistic category is used
            );
        
            $shipping_company_post_id = wp_insert_post($shipping_company_my_post);
        
            if (!is_wp_error($shipping_company_post_id)) {
                $shipping_company_image_url = get_template_directory_uri() . '/images/logistic' . ($shipping_company_i + 1) . '.png';
                $shipping_company_image_id = media_sideload_image($shipping_company_image_url, $shipping_company_post_id, null, 'id');
        
                if (!is_wp_error($shipping_company_image_id)) {
                    set_post_thumbnail($shipping_company_post_id, $shipping_company_image_id);
                } else {
                    error_log('Failed to set post thumbnail for post ID: ' . $shipping_company_post_id);
                }
            } 
        }

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