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

        // If the demo import is completed, display the "View Site" button
        if ($transport_movers_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'transport-movers') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'transport-movers') . '</a></span>';
        }
		
        if (isset($_POST['submit'])) {

            // Check if ace-post-type-builder is installed and activated
            if (!is_plugin_active('ace-post-type-builder/ace-post-type-builder.php')) {
                // Install the plugin if it doesn't exist
                $transport_movers_plugin_slug = 'ace-post-type-builder';
                $transport_movers_plugin_file = 'ace-post-type-builder/ace-post-type-builder.php';

                // Check if plugin is installed
                $transport_movers_installed_plugins = get_plugins();
                if (!isset($transport_movers_installed_plugins[$transport_movers_plugin_file])) {
                    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');

                    // Install the plugin
                    $transport_movers_upgrader = new Plugin_Upgrader();
                    $transport_movers_upgrader->install('https://downloads.wordpress.org/plugin/ace-post-type-builder.latest-stable.zip');
                }
                // Activate the plugin
                activate_plugin($transport_movers_plugin_file);
            }
  
            // ------- Create Nav Menu --------
            $transport_movers_menuname = 'Main Menus';
            $transport_movers_bpmenulocation = 'primary';
            $transport_movers_menu_exists = wp_get_nav_menu_object($transport_movers_menuname);

            if (!$transport_movers_menu_exists) {
                $transport_movers_menu_id = wp_create_nav_menu($transport_movers_menuname);

                // Create Home Page
                $transport_movers_home_title = 'Home';
                $transport_movers_home = array(
                    'post_type' => 'page',
                    'post_title' => $transport_movers_home_title,
                    'post_content' => '',
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'home'
                );
                $transport_movers_home_id = wp_insert_post($transport_movers_home);
                // Assign Home Page Template
                add_post_meta($transport_movers_home_id, '_wp_page_template', 'page-template/custom-home-page.php');
                // Update options to set Home Page as the front page
                update_option('page_on_front', $transport_movers_home_id);
                update_option('show_on_front', 'page');
                // Add Home Page to Menu
                wp_update_nav_menu_item($transport_movers_menu_id, 0, array(
                    'menu-item-title' => __('Home', 'transport-movers'),
                    'menu-item-classes' => 'home',
                    'menu-item-url' => home_url('/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $transport_movers_home_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create About Us Page with Dummy Content
                $transport_movers_about_title = 'About Us';
                $transport_movers_about_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>
                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.<br>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which dont look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isnt anything embarrassing hidden in the middle of text.<br>
                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $transport_movers_about = array(
                    'post_type' => 'page',
                    'post_title' => $transport_movers_about_title,
                    'post_content' => $transport_movers_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $transport_movers_about_id = wp_insert_post($transport_movers_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($transport_movers_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'transport-movers'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $transport_movers_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Services Page
                $transport_movers_services_title = 'Services';
                $transport_movers_services_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>
                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s.';

                $transport_movers_services = array(
                    'post_type'    => 'page',
                    'post_title'   => $transport_movers_services_title,
                    'post_content' => $transport_movers_services_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'services'
                );

                $transport_movers_services_id = wp_insert_post($transport_movers_services);

                // Add Services Page to Menu
                wp_update_nav_menu_item($transport_movers_menu_id, 0, array(
                    'menu-item-title'     => __('Services', 'transport-movers'),
                    'menu-item-classes'  => 'services',
                    'menu-item-url'      => home_url('/services/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $transport_movers_services_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

                // Create Shop Page
                $transport_movers_shop_title = 'Shop';
                $transport_movers_shop_content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam...<br>
                This page is used to display products and services offered by our transport and movers company.';

                $transport_movers_shop = array(
                    'post_type'    => 'page',
                    'post_title'   => $transport_movers_shop_title,
                    'post_content' => $transport_movers_shop_content,
                    'post_status'  => 'publish',
                    'post_author'  => 1,
                    'post_slug'    => 'shop'
                );

                $transport_movers_shop_id = wp_insert_post($transport_movers_shop);

                // Add Shop Page to Menu
                wp_update_nav_menu_item($transport_movers_menu_id, 0, array(
                    'menu-item-title'     => __('Shop', 'transport-movers'),
                    'menu-item-classes'  => 'shop',
                    'menu-item-url'      => home_url('/shop/'),
                    'menu-item-status'   => 'publish',
                    'menu-item-object-id'=> $transport_movers_shop_id,
                    'menu-item-object'   => 'page',
                    'menu-item-type'     => 'post_type'
                ));

               // Create Blog Page 
                $transport_movers_blog_page_title = 'Blog';
                $transport_movers_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($transport_movers_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$transport_movers_blog_page_query->have_posts()) {
                    $transport_movers_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $transport_movers_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $transport_movers_blog_page_id = wp_insert_post($transport_movers_blog_page);
                    update_option('page_for_posts', $transport_movers_blog_page_id);

                    wp_update_nav_menu_item($transport_movers_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'transport-movers'),
                    'menu-item-url'        => get_permalink($transport_movers_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $transport_movers_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }

                // Create Pages Page with Dummy Content
                $transport_movers_contact_title = 'Contact Us';
                $transport_movers_contact_content = 'Explore all the contact we have on our website. Find information about our services, company, and more.
                 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 contactly with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br>
                All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.';
                $transport_movers_contact = array(
                    'post_type' => 'page',
                    'post_title' => $transport_movers_contact_title,
                    'post_content' => $transport_movers_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $transport_movers_contact_id = wp_insert_post($transport_movers_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($transport_movers_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'transport-movers'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $transport_movers_contact_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Set the menu location if it's not already set
                if (!has_nav_menu($transport_movers_bpmenulocation)) {
                    $transport_movers_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($transport_movers_locations)) {
                        $transport_movers_locations = array();
                    }
                    $transport_movers_locations[$transport_movers_bpmenulocation] = $transport_movers_menu_id;
                    set_theme_mod('nav_menu_locations', $transport_movers_locations);
                }
            }

            //Contact Us
            set_theme_mod('transport_movers_call', '+123 456 7890');
            set_theme_mod('transport_movers_email', 'xyz123@example.com');
            set_theme_mod('transport_movers_city', '1900 New Your USA ');
            set_theme_mod('transport_movers_location', '254 Street Avenue, Los Angeles,LA 2415');
            set_theme_mod('transport_movers_button_link', '#');

            //Slider Section
            set_theme_mod('transport_movers_slider_button_text', 'Read More');
            set_theme_mod('transport_movers_slider_button_link', '#');

            $transport_movers_slider_titles = array(
                'Fast & Secure Moving Services',
                'Reliable Packing & Unpacking',
                'Safe & Efficient Transportation',
                'Affordable Moving Solutions'
            );            
            for ($transport_movers_i = 0; $transport_movers_i < 4; $transport_movers_i++) {

                $transport_movers_slider_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';
                $transport_movers_slider_title = $transport_movers_slider_titles[$transport_movers_i];

                // Create post object
                $transport_movers_my_post = array(
                    'post_title'    => wp_strip_all_tags($transport_movers_slider_title),
                    'post_content'  => $transport_movers_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );

                // Insert the post into the database
                $transport_movers_post_id = wp_insert_post($transport_movers_my_post);

                if ($transport_movers_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('transport_movers_tab_pages' . ($transport_movers_i + 1), $transport_movers_post_id);

                    $transport_movers_image_url = get_template_directory_uri() . '/images/slider' . ($transport_movers_i + 1) . '.png';

                    // Handle image upload and set as post thumbnail
                    $transport_movers_image_id = media_sideload_image($transport_movers_image_url, $transport_movers_post_id, null, 'id');

                    if (!is_wp_error($transport_movers_image_id)) {
                        // Set the downloaded image as the post's featured image
                        set_post_thumbnail($transport_movers_post_id, $transport_movers_image_id);
                    }
                }
            }

            //Details
            set_theme_mod('transport_movers_time_text', 'TIMING');
            set_theme_mod('transport_movers_time', '8:00 am to 7:30 pm Sunday: Closed');
            set_theme_mod('transport_movers_branchase_text', 'BRANCHES');
            set_theme_mod('transport_movers_branchase', 'Our Total Branches 235');

            //Social Link
            set_theme_mod('transport_movers_facebook', 'https://www.facebook.com');
            set_theme_mod('transport_movers_twitter', 'https://www.twtter.com');
            set_theme_mod('transport_movers_linkdin', 'https://www.linkedin.com');
            set_theme_mod('transport_movers_pinterest', 'https://www.pinterest.com');
            set_theme_mod('transport_movers_instagram', 'https://instagram.com');
            set_theme_mod('transport_movers_youtube', 'https://www.youtube.com');

            //About Section
            set_theme_mod('transport_movers_sec_title', 'ABOUT US');

            $transport_movers_post_title = "WELCOME TO SMART TRANSPORTATION";
            $transport_movers_post_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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.<br/>It was popularised in the 1960s with the release of Letraset sheets containing.<br/>It was popularised in the 1960s with the release of Letraset sheets containing.<br/>It was popularised in the 1960s with the release of Letraset sheets containing.<br/></P>
            ';
            $transport_movers_image_url = get_theme_file_uri() . '/images/about.png';

            // Create post object
            $transport_movers_post_data = array(
                'post_title'    => wp_strip_all_tags($transport_movers_post_title),
                'post_content'  => $transport_movers_post_content,
                'post_status'   => 'publish',
                'post_type'     => 'post', // Set post type to 'post'
            );
            set_theme_mod('transport_movers_project_single_post', $transport_movers_post_title);

            // Insert the post into the database
            $transport_movers_post_id = wp_insert_post($transport_movers_post_data);

            if (is_wp_error($transport_movers_post_id)) {
                error_log('Error creating post: ' . $transport_movers_post_id->get_error_message());
            } else {
                // Handle the featured image
                $transport_movers_image_id = media_sideload_image($transport_movers_image_url, $transport_movers_post_id, null, 'id');

                if (is_wp_error($transport_movers_image_id)) {
                    error_log('Error downloading image: ' . $transport_movers_image_id->get_error_message());
                } else {
                    // Assign featured image to post
                    set_post_thumbnail($transport_movers_post_id, $transport_movers_image_id);
                }
            }

            // Set the demo import completion flag
    		update_option('transport_movers_demo_import_completed', true);
    		// Display success message and "View Site" button
    		echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'transport-movers') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'transport-movers') . '</a></span>';
            //end

        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=transport-movers-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('transport_movers_demo_import_completed')) : ?>
        <form method="post">
        <p><?php esc_html_e('Click the below run importer button to import demo content','transport-movers'); ?></p>
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','transport-movers'); ?>" class="button button-primary button-large">
        </form>
    <?php endif; ?>
    </form>
    <script type="text/javascript">
		function validate(valid) {
            if (confirm("Do you really want to import the theme demo content?")) {
                // Show loader
                document.getElementById('demo-import-loader').style.display = 'block';
                
                // Submit form
                document.forms[0].submit();
            } else {
                return false;
            }
        }
    </script>
</div>
