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

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

		//POST and update the customizer and other related data
        if (isset($_POST['submit'])) {

            // ------- Create Nav Menu --------
            $real_estate_broker_menuname = 'Main Menus';
            $real_estate_broker_bpmenulocation = 'primary';
            $real_estate_broker_menu_exists = wp_get_nav_menu_object($real_estate_broker_menuname);

            if (!$real_estate_broker_menu_exists) {
                $real_estate_broker_menu_id = wp_create_nav_menu($real_estate_broker_menuname);

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

                // Create About Us Page with Dummy Content
                $real_estate_broker_about_title = 'About Us';
                $real_estate_broker_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.';
                $real_estate_broker_about = array(
                    'post_type' => 'page',
                    'post_title' => $real_estate_broker_about_title,
                    'post_content' => $real_estate_broker_about_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'about-us'
                );
                $real_estate_broker_about_id = wp_insert_post($real_estate_broker_about);
                // Add About Us Page to Menu
                wp_update_nav_menu_item($real_estate_broker_menu_id, 0, array(
                    'menu-item-title' => __('About Us', 'real-estate-broker'),
                    'menu-item-classes' => 'about-us',
                    'menu-item-url' => home_url('/about-us/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $real_estate_broker_about_id,
                    'menu-item-object' => 'page',
                    'menu-item-type' => 'post_type'
                ));

                // Create Blog Page 
                $real_estate_broker_blog_page_title = 'Blog';
                $real_estate_broker_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($real_estate_broker_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$real_estate_broker_blog_page_query->have_posts()) {
                    $real_estate_broker_blog_page = array(
                    'post_type'   => 'page',
                    'post_title'  => $real_estate_broker_blog_page_title,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    );
                    $real_estate_broker_blog_page_id = wp_insert_post($real_estate_broker_blog_page);
                    update_option('page_for_posts', $real_estate_broker_blog_page_id);
                    wp_update_nav_menu_item($real_estate_broker_menu_id, 0, array(
                    'menu-item-title'      => __('Blog', 'real-estate-broker'),
                    'menu-item-url'        => get_permalink($real_estate_broker_blog_page_id),
                    'menu-item-status'     => 'publish',
                    'menu-item-object-id'  => $real_estate_broker_blog_page_id,
                    'menu-item-object'     => 'page',
                    'menu-item-type'       => 'post_type',
                    ));
                }
                // Create Pages Page with Dummy Content
                $real_estate_broker_contact_title = 'Contact Us';
                $real_estate_broker_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.';
                $real_estate_broker_contact = array(
                    'post_type' => 'page',
                    'post_title' => $real_estate_broker_contact_title,
                    'post_content' => $real_estate_broker_contact_content,
                    'post_status' => 'publish',
                    'post_author' => 1,
                    'post_slug' => 'contact'
                );
                $real_estate_broker_contact_id = wp_insert_post($real_estate_broker_contact);
                // Add Pages Page to Menu
                wp_update_nav_menu_item($real_estate_broker_menu_id, 0, array(
                    'menu-item-title' => __('Contact', 'real-estate-broker'),
                    'menu-item-classes' => 'contact',
                    'menu-item-url' => home_url('/contact/'),
                    'menu-item-status' => 'publish',
                    'menu-item-object-id' => $real_estate_broker_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($real_estate_broker_bpmenulocation)) {
                    $real_estate_broker_locations = get_theme_mod('nav_menu_locations'); // Use 'nav_menu_locations' to get locations array
                    if (empty($real_estate_broker_locations)) {
                        $real_estate_broker_locations = array();
                    }
                    $real_estate_broker_locations[$real_estate_broker_bpmenulocation] = $real_estate_broker_menu_id;
                    set_theme_mod('nav_menu_locations', $real_estate_broker_locations);
                }
            }

            // Set the demo import completion flag
    		update_option('real_estate_broker_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.', 'real-estate-broker') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('VIEW SITE', 'real-estate-broker') . '</a></span>';
            //end

            // Top Bar //
            set_theme_mod( 'real_estate_realtor_email_icon', 'far fa-envelope');
            set_theme_mod( 'real_estate_realtor_email_address', 'xyz123@example.com');
            set_theme_mod( 'real_estate_realtor_location_icon', 'fas fa-map-marker-alt');
            set_theme_mod( 'real_estate_realtor_location_address', '15/A Nest, Tower, NYC');
            set_theme_mod( 'real_estate_broker_phone_number', '+123 456 7890');
            set_theme_mod( 'real_estate_realtor_listing_btn_icon', 'fas fa-home');
            set_theme_mod( 'real_estate_realtor_listing_button_text', 'Add Listing');

            // social icon //
            set_theme_mod('real_estate_realtor_facebook_icon', 'fab fa-facebook-f');
            set_theme_mod('real_estate_realtor_facebook_link', 'https://www.facebook.com');
            set_theme_mod('real_estate_realtor_twitter_icon', 'fab fa-twitter');
            set_theme_mod('real_estate_realtor_twitter_link', 'https://www.twitter.com');
            set_theme_mod('real_estate_realtor_linkdin_icon', 'fab fa-linkedin-in');
            set_theme_mod('real_estate_realtor_linkdin_link', 'https://www.linkedin.com');
            set_theme_mod('real_estate_realtor_instagram_icon', 'fab fa-instagram');
            set_theme_mod('real_estate_realtor_instagram_link', 'https://www.instagram.com');
            set_theme_mod('real_estate_realtor_footer_pintrest_link', 'https://www.pintrest.com');

            // Slider section start
            set_theme_mod('real_estate_realtor_slider_hide', true );
            set_theme_mod( 'real_estate_realtor_slider_small_title', 'Real Estate Agency' );
            set_theme_mod( 'real_estate_realtor_topbar_btn_link', '#' );

            $real_estate_realtor_slider_titles = array(
                'Find Your Perfect Home',
                'Your ideal home is just a click away.',
                'Discover the home of your dreams with us.',
                'Let us help you find a place to call home.',
            );            

            for($real_estate_realtor_i = 0; $real_estate_realtor_i < count($real_estate_realtor_slider_titles); $real_estate_realtor_i++) {
                $real_estate_realtor_slider_content = 'Lorem ipsum is simply dummy text of the printing and typesetting industry.';
                $real_estate_realtor_slider_title = $real_estate_realtor_slider_titles[$real_estate_realtor_i];
            
                // Create post object
                $real_estate_realtor_my_post = array(
                    'post_title'    => wp_strip_all_tags( $real_estate_realtor_slider_title ),
                    'post_content'  => $real_estate_realtor_slider_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'page',
                );

                // Insert the post into the database
                $real_estate_realtor_post_id = wp_insert_post( $real_estate_realtor_my_post );
                
                if ($real_estate_realtor_post_id) {
                    // Set the theme mod for the slider page
                    set_theme_mod('real_estate_realtor_slider_setting' . ($real_estate_realtor_i + 1), $real_estate_realtor_post_id);

                    $real_estate_realtor_image_url = get_theme_file_uri() . '/images/slider' . ($real_estate_realtor_i + 1) . '.png';

                    $real_estate_realtor_image_id = media_sideload_image($real_estate_realtor_image_url, $real_estate_realtor_post_id, null, 'id');

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

            // Popular Properties Section
            set_theme_mod('real_estate_realtor_popular_properties_hide', true );
            set_theme_mod('real_estate_realtor_properties_section_title', 'Popular Properties');
            set_theme_mod('real_estate_realtor_properties_section_text', 'Lorem ipsum doler sit emit, consec tetur cing suspe ndisse suscipt');
            set_theme_mod('real_estate_realtor_properties_counter', '5');

            $real_estate_realtor_tab_text_array = array("All", "Apartments", "Banglows", "Villas", "More");
            $real_estate_realtor_category_names = array("postcategory1", "postcategory2", "postcategory3", "postcategory4", "postcategory5", "postcategory6", "postcategory7");
            $real_estate_realtor_title_array = array(
                array("Sea Breeze Apartment", "Ocean View Flat", "City Heights", "Palm Residence", "Downtown Loft"),
                array("Cozy Bungalow", "Maple Retreat", "Sunset House", "Green Meadow", "Oakwood Cottage"),
                array("Modern Villa", "Hilltop Estate", "Golden Horizon", "Lavender Villa", "Serenity Home"),
                array("Beachside Villa", "Luxury Haven", "Sunrise Manor", "Harbor Villa", "Skyline View"),
                array("Urban Studio", "Compact Living", "Minimalist Nest", "Smart Home", "Metro Suite"),
                array("Elegant Flat", "Prestige Point", "Royal Square", "The Elite", "Golden Tower"),
                array("Lakeview Home", "Forest Edge", "Riverfront Stay", "Meadow Breeze", "Countryside Manor")
            );
            
            $real_estate_realtor_amount_array = array(
                array('$7000', '$6500', '$8000', '$7500', '$7200'),
                array('$6000', '$5800', '$6200', '$6400', '$6100'),
                array('$9000', '$9200', '$8900', '$9300', '$9100'),
                array('$8800', '$8600', '$8700', '$8500', '$8400'),
                array('$5000', '$5200', '$5100', '$5300', '$5400'),
                array('$7800', '$7600', '$7700', '$7500', '$7400'),
                array('$6900', '$6700', '$7100', '$7300', '$7000')
            );
            
            $real_estate_realtor_bed_array = array(
                array('2', '3', '4', '3', '2'),
                array('3', '2', '4', '3', '3'),
                array('5', '4', '5', '6', '4'),
                array('4', '5', '5', '4', '3'),
                array('1', '2', '1', '2', '2'),
                array('3', '4', '3', '4', '3'),
                array('4', '3', '2', '3', '4')
            );
            
            $real_estate_realtor_baths_array = array(
                array('1', '2', '2', '2', '1'),
                array('2', '1', '2', '2', '2'),
                array('3', '2', '3', '3', '2'),
                array('3', '3', '3', '2', '2'),
                array('1', '1', '1', '1', '1'),
                array('2', '2', '2', '2', '2'),
                array('2', '2', '2', '2', '2')
            );
            
            $real_estate_realtor_sqft_array = array(
                array('850', '900', '950', '1000', '875'),
                array('1100', '1050', '1150', '1120', '1080'),
                array('1500', '1600', '1550', '1580', '1520'),
                array('1700', '1650', '1680', '1720', '1740'),
                array('500', '600', '550', '580', '590'),
                array('1200', '1250', '1230', '1280', '1260'),
                array('1400', '1450', '1420', '1440', '1430')
            );           

            for ($real_estate_realtor_tab_index = 1; $real_estate_realtor_tab_index <= 5; $real_estate_realtor_tab_index++) {
                $real_estate_realtor_theme_mod_key = 'real_estate_realtor_properties_tab_text' . $real_estate_realtor_tab_index;
                $real_estate_realtor_theme_mod_value = $real_estate_realtor_tab_text_array[$real_estate_realtor_tab_index - 1];
                set_theme_mod($real_estate_realtor_theme_mod_key, $real_estate_realtor_theme_mod_value);

                // Set the category for this tab
                $real_estate_realtor_current_category = $real_estate_realtor_category_names[$real_estate_realtor_tab_index - 1];
                set_theme_mod('real_estate_realtor_popular_properties_category' . $real_estate_realtor_tab_index, $real_estate_realtor_current_category);

                // Create or retrieve the post category term ID
                $real_estate_realtor_term = term_exists($real_estate_realtor_current_category, 'category');
                if ($real_estate_realtor_term === 0 || $real_estate_realtor_term === null) {
                    // If the term does not exist, create it
                    $real_estate_realtor_term = wp_insert_term($real_estate_realtor_current_category, 'category');
                }
                if (is_wp_error($real_estate_realtor_term)) {
                    error_log('Error creating category: ' . $real_estate_realtor_term->get_error_message());
                    continue; // Skip to the next iteration if category creation fails
                }

                for ($real_estate_realtor_i = 0; $real_estate_realtor_i < 5; $real_estate_realtor_i++) {
                    // Create post content
                    $real_estate_realtor_title = $real_estate_realtor_title_array[$real_estate_realtor_tab_index - 1][$real_estate_realtor_i];
                    $real_estate_realtor_amount = $real_estate_realtor_amount_array[$real_estate_realtor_tab_index - 1][$real_estate_realtor_i];
                    $real_estate_realtor_bed = $real_estate_realtor_bed_array[$real_estate_realtor_tab_index - 1][$real_estate_realtor_i];
                    $real_estate_realtor_baths = $real_estate_realtor_baths_array[$real_estate_realtor_tab_index - 1][$real_estate_realtor_i];
                    $real_estate_realtor_sqft = $real_estate_realtor_sqft_array[$real_estate_realtor_tab_index - 1][$real_estate_realtor_i];
                    
                    $real_estate_realtor_content = 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.';

                    // Create post object
                    $real_estate_realtor_my_post = array(
                        'post_title'    => wp_strip_all_tags($real_estate_realtor_title),
                        'post_content'  => $real_estate_realtor_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $real_estate_realtor_post_id = wp_insert_post($real_estate_realtor_my_post);

                    update_post_meta($real_estate_realtor_post_id, 'real_estate_realtor_property_amount', $real_estate_realtor_amount);
                    update_post_meta($real_estate_realtor_post_id, 'real_estate_realtor_property_sale_rent', 'For Rent'); // Can also be made dynamic
                    update_post_meta($real_estate_realtor_post_id, 'real_estate_realtor_property_bed', $real_estate_realtor_bed);
                    update_post_meta($real_estate_realtor_post_id, 'real_estate_realtor_property_baths', $real_estate_realtor_baths);
                    update_post_meta($real_estate_realtor_post_id, 'real_estate_realtor_property_sqft', $real_estate_realtor_sqft);

                    if (is_wp_error($real_estate_realtor_post_id)) {
                        error_log('Error creating post: ' . $real_estate_realtor_post_id->get_error_message());
                        continue; // Skip to the next post if creation fails
                    }

                    // Assign the category to the post
                    wp_set_post_categories($real_estate_realtor_post_id, array((int)$real_estate_realtor_term['term_id']));

                    // Handle the featured image using media_sideload_image
                    $real_estate_realtor_image_url = get_template_directory_uri() . '/images/post' . ($real_estate_realtor_i + 1) . '.png';
                    $real_estate_realtor_image_id = media_sideload_image($real_estate_realtor_image_url, $real_estate_realtor_post_id, null, 'id');

                    if (is_wp_error($real_estate_realtor_image_id)) {
                        error_log('Error downloading image: ' . $real_estate_realtor_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }

                    // Assign featured image to post
                    set_post_thumbnail($real_estate_realtor_post_id, $real_estate_realtor_image_id);
                }
            }       

            // Location Section   
            set_theme_mod('real_estate_broker_show_location_section', true);
            set_theme_mod('real_estate_broker_location_section_title', 'Find By Locations');
            set_theme_mod('real_estate_broker_location_section_text', 'Lorem ipsum is simply dummy text of the printing and typesetting industry.');
            set_theme_mod('real_estate_broker_location_category', 'category3');

            $real_estate_broker_location_categories = array('category1', 'category2', 'category3');
            $real_estate_broker_location_titles = array_fill(0, 3, array("Brazil", "Tokyo", "New York", "Moscow"));
            $real_estate_broker_location_contents = array_fill(0, 3, array_fill(0, 4, "Lorem Ipsum is simply dummy text of the printing and typesetting industry."));

            foreach ($real_estate_broker_location_categories as $real_estate_broker_index => $real_estate_broker_category_name) {
                $real_estate_broker_category_term = term_exists($real_estate_broker_category_name, 'category') ?: wp_insert_term($real_estate_broker_category_name, 'category');

                if (is_wp_error($real_estate_broker_category_term)) {
                    error_log('Error creating category: ' . $real_estate_broker_category_term->get_error_message());
                    continue;
                }

                foreach ($real_estate_broker_location_titles[$real_estate_broker_index] as $real_estate_broker_i => $real_estate_broker_title) {
                    $real_estate_broker_post_id = wp_insert_post(array(
                        'post_title'    => wp_strip_all_tags($real_estate_broker_title),
                        'post_content'  => $real_estate_broker_location_contents[$real_estate_broker_index][$real_estate_broker_i],
                        'post_status'   => 'publish',
                        'post_type'     => 'post',
                        'post_excerpt'  => substr($real_estate_broker_location_contents[$real_estate_broker_index][$real_estate_broker_i], 0, 100) . '...',
                    ));

                    if (is_wp_error($real_estate_broker_post_id)) {
                        error_log('Error creating post: ' . $real_estate_broker_post_id->get_error_message());
                        continue;
                    }

                    wp_set_post_categories($real_estate_broker_post_id, array((int)$real_estate_broker_category_term['term_id']));

                    $image_url = get_theme_file_uri() . '/images/popular' . ($real_estate_broker_i + 1) . '.png';
                    $image_id = media_sideload_image($image_url, $real_estate_broker_post_id, null, 'id');

                    if (!is_wp_error($image_id)) {
                        set_post_thumbnail($real_estate_broker_post_id, $image_id);
                    } else {
                        error_log('Error downloading image: ' . $image_id->get_error_message());
                    }

                    update_post_meta($real_estate_broker_post_id, 'real_estate_broker_noof_property', '3 Properties');
                    update_post_meta($real_estate_broker_post_id, 'real_estate_broker_property_detail', '3 BHK House for sale at the best price');
                    update_post_meta($real_estate_broker_post_id, 'real_estate_broker_property_price', '5000');
                }
            }

            //Copyright Text
            set_theme_mod( 'real_estate_realtor_footer_text', 'By Buywptemplate' );
        }
    ?>

    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=real-estate-broker-guide-page" method="POST" onsubmit="return validate(this);">
    <?php if (!get_option('real_estate_broker_demo_import_completed')) : ?>
        <div class="demo-btn">
        <h3><?php esc_html_e( 'Click the below run importer button to import demo content', 'real-estate-broker' ); ?></h3>
        <form method="post">
            <input class= "run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer','real-estate-broker'); ?>" class="button button-primary button-large">
        </form>
        </div>
    <?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>
