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

    // If the demo import is completed, display the "View Site" button
        if ($creative_one_page_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'creative-one-page') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '"  class= "run-import view-site" target="_blank">' . esc_html__('VIEW SITE', 'creative-one-page') . '</a></span>';
        }

    if (isset($_POST['submit'])) {

    // --- Menu ---
    $creative_one_page_primary_menu_name = 'Main Menu';
    $creative_one_page_primary_menu_location = 'primary';
    $creative_one_page_primary_menu_exists = wp_get_nav_menu_object($creative_one_page_primary_menu_name);

    if (!$creative_one_page_primary_menu_exists) {
        // Create the left menu
        $creative_one_page_primary_menu_id = wp_create_nav_menu($creative_one_page_primary_menu_name);

        // Create and assign the Home page
        $creative_one_page_home_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Home',
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'home'
        ));
        // Assign template and set as front page
        add_post_meta($creative_one_page_home_page_id, '_wp_page_template', 'page-template/custom-front-page.php');
        update_option('page_on_front', $creative_one_page_home_page_id);
        update_option('show_on_front', 'page');

        // Add Home page to the left menu
        wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
            'menu-item-title'     => __('Home', 'creative-one-page'),
            'menu-item-classes'   => 'home',
            'menu-item-url'       => home_url('/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $creative_one_page_home_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the About Us page
        $creative_one_page_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'About Us',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add About Us page to the left menu
        wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
            'menu-item-title'     => __('About Us', 'creative-one-page'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $creative_one_page_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));


        // Create and assign the Services page
        $creative_one_page_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Services',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Services page to the left menu
        wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
            'menu-item-title'     => __('Services', 'creative-one-page'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $creative_one_page_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Work page
        $creative_one_page_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Work',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Work page to the left menu
        wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
            'menu-item-title'     => __('Work', 'creative-one-page'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $creative_one_page_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Pricing Plan page
        $creative_one_page_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Pricing Plan',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Pricing Plan page to the left menu
        wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
            'menu-item-title'     => __('Pricing Plan', 'creative-one-page'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $creative_one_page_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Create and assign the Team page
        $creative_one_page_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Team',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Team page to the left menu
        wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
            'menu-item-title'     => __('Team', 'creative-one-page'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $creative_one_page_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // CREATE BLOG PAGE 
        $creative_one_page_blog_page = get_page_by_path('blog');
            if (!$creative_one_page_blog_page) {
            $creative_one_page_blog_page_id = wp_insert_post(array(
                    'post_type'   => 'page',
                    'post_title'  => 'Blog',
                    'post_status' => 'publish',
                    'post_name'   => 'blog',
                ));

            } else {
                 $creative_one_page_blog_page_id = $creative_one_page_blog_page->ID;
            }
            update_option('page_for_posts', $creative_one_page_blog_page_id);

             wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
                'menu-item-title'     => __('Blog', 'creative-one-page'),
                'menu-item-object-id' => $creative_one_page_blog_page_id,
                'menu-item-object'    => 'page',
                'menu-item-type'      => 'post_type',
                'menu-item-status'    => 'publish',
        ));       

        // Create and assign the Contact Us page
        $creative_one_page_about_us_page_id = wp_insert_post(array(
            'post_type'     => 'page',
            'post_title'    => 'Contact Us',
            'post_content'  => '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. 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.',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_name'     => 'about-us'
        ));

        // Add Contact Us page to the left menu
        wp_update_nav_menu_item($creative_one_page_primary_menu_id, 0, array(
            'menu-item-title'     => __('Contact Us', 'creative-one-page'),
            'menu-item-classes'   => 'about-us',
            'menu-item-url'       => home_url('/about-us/'),
            'menu-item-status'    => 'publish',
            'menu-item-object-id' => $creative_one_page_about_us_page_id,
            'menu-item-object'    => 'page',
            'menu-item-type'      => 'post_type',
        ));

        // Assign left menu to its location
        $creative_one_page_locations = get_theme_mod('nav_menu_locations', array());
        $creative_one_page_locations[$creative_one_page_primary_menu_location] = $creative_one_page_primary_menu_id;
        set_theme_mod('nav_menu_locations', $creative_one_page_locations);
    }  

     // Set the demo import completion flag
    		update_option('creative_one_page_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.', 'creative-one-page') . '</p>';
    		echo '<span><a href="' . esc_url(home_url()) . '" class="run-import site-btn" target="_blank">' . esc_html__('VIEW SITE', 'creative-one-page') . '</a></span>';

    //end

    // Social Icons
    set_theme_mod( 'creative_one_page_facebook_url', '#' );
    set_theme_mod( 'creative_one_page_twitter_url', '#' );    
    set_theme_mod( 'creative_one_page_linkedin_url', '#' );
    set_theme_mod( 'creative_one_page_instagram_url', '#' );

    // Topbar Section
    set_theme_mod( 'creative_one_page_phone1', '+00 123-456-7890' );
    set_theme_mod( 'creative_one_page_mail1', 'support@example.com' );

    // Slider
    for($creative_one_page_i=1;$creative_one_page_i<=4;$creative_one_page_i++){
       $creative_one_page_slider_title = 'LOREM IPSUM IS SIMPLY';
       $creative_one_page_slider_content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
          // Create post object
       $my_post = array(
       'post_title'    => wp_strip_all_tags( $creative_one_page_slider_title ),
       'post_content'  => $creative_one_page_slider_content,
       'post_status'   => 'publish',
       'post_type'     => 'page',
       );

       // Insert the post into the database
       $creative_one_page_post_id = wp_insert_post( $my_post );

       if ($creative_one_page_post_id) {
         // Set the theme mod for the slider page
         set_theme_mod('creative_one_page_slider_page' . $creative_one_page_i, $creative_one_page_post_id);

          $creative_one_page_image_url = get_template_directory_uri().'/images/slider'.$creative_one_page_i.'.png';

        $creative_one_page_image_id = media_sideload_image($creative_one_page_image_url, $creative_one_page_post_id, null, 'id');

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

    // Our Services Settings
    set_theme_mod( 'creative_one_page_our_services_number', '3' );

    $creative_one_page_service_text_array = array("fa-solid fa-key", "fa-solid fa-umbrella", "fa-solid fa-lightbulb");
    $creative_one_page_service_icon_array = array("Our Features Title 1", "Our Features Title 2", "Our Features Title 3");
    for($creative_one_page_i=1; $creative_one_page_i<=3; $creative_one_page_i++) {
        $creative_one_page_theme_mod_key = 'creative_one_page_our_services_icon' . $creative_one_page_i;
        $creative_one_page_theme_mod_value = $creative_one_page_service_text_array[$creative_one_page_i - 1];
        set_theme_mod($creative_one_page_theme_mod_key, $creative_one_page_theme_mod_value);  
        $creative_one_page_theme_mod_key = 'creative_one_page_our_services_title' . $creative_one_page_i;
        $creative_one_page_theme_mod_value = $creative_one_page_service_icon_array[$creative_one_page_i - 1];
        set_theme_mod($creative_one_page_theme_mod_key, $creative_one_page_theme_mod_value); 
        set_theme_mod( 'creative_one_page_our_services_text'.$creative_one_page_i, 'Lorem Ipsum.' );                    
    }  

    // Our Work Settings
    set_theme_mod( 'creative_one_page_projects_title', 'OUR WORKS' );
    set_theme_mod('creative_one_page_tab_number', '5');

    $creative_one_page_tab_text_array = array("All", "BRANDING", "MOBILE APP", "WEB DESIGN", "WEB DEVELOPMENT");
    $creative_one_page_category_names = array("postcategory1", "postcategory2", "postcategory3", "postcategory4", "postcategory5");
    $creative_one_page_title_array = array(
        array("PROJECT TITLE 1", "PROJECT TITLE 2", "PROJECT TITLE 3"),
        array("PROJECT TITLE 1", "PROJECT TITLE 2", "PROJECT TITLE 3"),
        array("PROJECT TITLE 1", "PROJECT TITLE 2", "PROJECT TITLE 3"),
        array("PROJECT TITLE 1", "PROJECT TITLE 2", "PROJECT TITLE 3"),
        array("PROJECT TITLE 1", "PROJECT TITLE 2", "PROJECT TITLE 3")
    );

    for ($creative_one_page_tab_index = 1; $creative_one_page_tab_index <= 5; $creative_one_page_tab_index++) {
        $creative_one_page_theme_mod_key = 'creative_one_page_our_project_tab_title' . $creative_one_page_tab_index;
        $creative_one_page_theme_mod_value = $creative_one_page_tab_text_array[$creative_one_page_tab_index - 1];
        set_theme_mod($creative_one_page_theme_mod_key, $creative_one_page_theme_mod_value);

        // Set the category for this tab
        $creative_one_page_current_category = $creative_one_page_category_names[$creative_one_page_tab_index - 1];
        set_theme_mod('creative_one_page_cate_tab' . $creative_one_page_tab_index, $creative_one_page_current_category);

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

        for ($creative_one_page_i = 0; $creative_one_page_i < 3; $creative_one_page_i++) {
            // Create post content
            $creative_one_page_title = $creative_one_page_title_array[$creative_one_page_tab_index - 1][$creative_one_page_i];
            $creative_one_page_content = 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s.';

            // Create post object
            $creative_one_page_my_post = array(
                'post_title'    => wp_strip_all_tags($creative_one_page_title),
                'post_content'  => $creative_one_page_content,
                'post_status'   => 'publish',
                'post_type'     => 'post', // Post type set to 'post'
            );

            // Insert the post into the database
            $creative_one_page_post_id = wp_insert_post($creative_one_page_my_post);

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

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

            // Handle the featured image using media_sideload_image
            $creative_one_page_image_url = get_template_directory_uri() . '/images/post' . ($creative_one_page_i + 1) . '.png';
            $creative_one_page_image_id = media_sideload_image($creative_one_page_image_url, $creative_one_page_post_id, null, 'id');

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

            // Assign featured image to post
            set_post_thumbnail($creative_one_page_post_id, $creative_one_page_image_id);
        }
    }            
    }
    ?>

	<p class="note"><?php esc_html_e( 'Please Note: If your website is live and already contains data, we recommend creating a backup first. Running this importer will replace your current settings with the custom values from the demo.', 'creative-one-page' ); ?></p>
        <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=creative_one_page_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('creative_one_page_demo_import_completed')) : ?>
            <button type="submit" name="submit" class="run-import">
                    <?php esc_html_e('Run Importer','creative-one-page'); ?>
                    <span id="spinner" style="display: none;">
                        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/images/spinner.gif" alt="Loading..." style="width:34px; height:34px; vertical-align: middle;" />
                    </span>
            </button>
        <?php endif; ?>
        </form>
        <script type="text/javascript">
            function validate(valid) {
                if(confirm("Do you really want to import the theme demo content?")){
                    document.getElementById('spinner').style.display = 'inline-block';
                }
                else {
                    return false;
                }
            }
        </script>
    
</div>
