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

        // If the demo import is completed, display the "View Site" button
        if ($chartered_accountant_demo_import_completed) {
        echo '<p class="notice-text">' . esc_html__('Your demo import has been completed successfully.', 'chartered-accountant') . '</p>';
        echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'chartered-accountant') . '</a></span>';
        echo '<span><a href="'. esc_url(admin_url('customize.php') ) .'" class="button button-primary demo-btn" target=_blank>'. esc_html__( 'Customize Your Site', 'chartered-accountant' ) .'</a></span>';
        echo '<span><a href="'. esc_url( 'https://preview.vwthemesdemo.com/docs/free-chartered-accountant/' ) .'" class="button button-primary doc-btn" target=_blank>'. esc_html__( 'Free Theme Documentation', 'chartered-accountant' ) .'</a></span>';
    }

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

        // Check if ibtana visual editor is installed and activated
        if (!is_plugin_active('ibtana-visual-editor/plugin.php')) {
          // Install the plugin if it doesn't exist
          $chartered_accountant_plugin_slug = 'ibtana-visual-editor';
          $chartered_accountant_plugin_file = 'ibtana-visual-editor/plugin.php';

          // Check if plugin is installed
          $chartered_accountant_installed_plugins = get_plugins();
          if (!isset($chartered_accountant_installed_plugins[$chartered_accountant_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
              $chartered_accountant_upgrader = new Plugin_Upgrader();
              $chartered_accountant_upgrader->install('https://downloads.wordpress.org/plugin/ibtana-visual-editor.latest-stable.zip');
          }
          // Activate the plugin
          activate_plugin($chartered_accountant_plugin_file);
        }  

        // ------- Create Nav Menu --------
        $chartered_accountant_menuname = 'Main Menus';
        $chartered_accountant_bpmenulocation = 'primary';
        $chartered_accountant_menu_exists = wp_get_nav_menu_object($chartered_accountant_menuname);

        if (!$chartered_accountant_menu_exists) {
            $chartered_accountant_menu_id = wp_create_nav_menu($chartered_accountant_menuname);

            // Create Home Page
            $chartered_accountant_home_title = 'Home';
            $chartered_accountant_home = array(
                'post_type' => 'page',
                'post_title' => $chartered_accountant_home_title,
                'post_content' => '',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'home'
            );
            $chartered_accountant_home_id = wp_insert_post($chartered_accountant_home);
            // Assign Home Page Template
            add_post_meta($chartered_accountant_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', $chartered_accountant_home_id);
            update_option('show_on_front', 'page');
            // Add Home Page to Menu
            wp_update_nav_menu_item($chartered_accountant_menu_id, 0, array(
                'menu-item-title' => __('Home', 'chartered-accountant'),
                'menu-item-classes' => 'home',
                'menu-item-url' => home_url('/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $chartered_accountant_home_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create About Us Page with Dummy Content
            $chartered_accountant_about_title = 'About Us';
            $chartered_accountant_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.';
            $chartered_accountant_about = array(
                'post_type' => 'page',
                'post_title' => $chartered_accountant_about_title,
                'post_content' => $chartered_accountant_about_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'about-us'
            );
            $chartered_accountant_about_id = wp_insert_post($chartered_accountant_about);
            // Add About Us Page to Menu
            wp_update_nav_menu_item($chartered_accountant_menu_id, 0, array(
                'menu-item-title' => __('About Us', 'chartered-accountant'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $chartered_accountant_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Services Page with Dummy Content
            $chartered_accountant_about_title = 'Services';
            $chartered_accountant_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.';
            $chartered_accountant_about = array(
                'post_type' => 'page',
                'post_title' => $chartered_accountant_about_title,
                'post_content' => $chartered_accountant_about_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'about-us'
            );
            $chartered_accountant_about_id = wp_insert_post($chartered_accountant_about);
            // Add Services Page to Menu
            wp_update_nav_menu_item($chartered_accountant_menu_id, 0, array(
                'menu-item-title' => __('Services', 'chartered-accountant'),
                'menu-item-classes' => 'about-us',
                'menu-item-url' => home_url('/about-us/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $chartered_accountant_about_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Pages Page with Dummy Content
            $chartered_accountant_pages_title = 'Pages';
            $chartered_accountant_pages_content = '
            Explore all the pages 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 recently 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.';
            $chartered_accountant_pages = array(
                'post_type' => 'page',
                'post_title' => $chartered_accountant_pages_title,
                'post_content' => $chartered_accountant_pages_content,
                'post_status' => 'publish',
                'post_author' => 1,
                'post_slug' => 'pages'
            );
            $chartered_accountant_pages_id = wp_insert_post($chartered_accountant_pages);
            // Add Pages Page to Menu
            wp_update_nav_menu_item($chartered_accountant_menu_id, 0, array(
                'menu-item-title' => __('Pages', 'chartered-accountant'),
                'menu-item-classes' => 'pages',
                'menu-item-url' => home_url('/pages/'),
                'menu-item-status' => 'publish',
                'menu-item-object-id' => $chartered_accountant_pages_id,
                'menu-item-object' => 'page',
                'menu-item-type' => 'post_type'
            ));

            // Create Blog Page 
                $chartered_accountant_blog_page_title = 'Blog';

                $chartered_accountant_blog_page_query = new WP_Query(array(
                    'post_type'      => 'page',
                    'name'           => sanitize_title($chartered_accountant_blog_page_title),
                    'post_status'    => 'publish',
                    'posts_per_page' => 1
                ));
                if (!$chartered_accountant_blog_page_query->have_posts()) {
                    $chartered_accountant_blog_page = array(
                        'post_type'   => 'page',
                        'post_title'  => $chartered_accountant_blog_page_title,
                        'post_status' => 'publish',
                        'post_author' => 1,
                    );
                    $chartered_accountant_blog_page_id = wp_insert_post($chartered_accountant_blog_page);
                    update_option('page_for_posts', $chartered_accountant_blog_page_id);

                    wp_update_nav_menu_item($chartered_accountant_menu_id, 0, array(
                        'menu-item-title'      => __('Blog', 'chartered-accountant'),
                        'menu-item-url'        => get_permalink($chartered_accountant_blog_page_id),
                        'menu-item-status'     => 'publish',
                        'menu-item-object-id'  => $chartered_accountant_blog_page_id,
                        'menu-item-object'     => 'page',
                        'menu-item-type'       => 'post_type',
                    ));
                }

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

        // Set the demo import completion flag
		update_option('chartered_accountant_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.', 'chartered-accountant') . '</p>';
		echo '<span><a href="' . esc_url(home_url()) . '" class="button button-primary site-btn" target="_blank">' . esc_html__('View Site', 'chartered-accountant') . '</a></span>';
        echo '<span><a href="'. esc_url(admin_url('customize.php') ) .'" class="button button-primary demo-btn" target=_blank>'. esc_html__( 'Customize Your Site', 'chartered-accountant' ) .'</a></span>';
        echo '<span><a href="'. esc_url( 'https://preview.vwthemesdemo.com/docs/free-chartered-accountant/' ) .'" class="button button-primary doc-btn" target=_blank>'. esc_html__( 'Free Theme Documentation', 'chartered-accountant' ) .'</a></span>';
        //end 

        // Topbar
        set_theme_mod( 'chartered_accountant_topbar_button_label', 'Get Start' );
        set_theme_mod( 'chartered_accountant_topbar_button_url', '#' );
        
        // Banner Settings
        set_theme_mod( 'chartered_accountant_banner_small_title', 'Your Chartered Accounted' );
        set_theme_mod( 'chartered_accountant_banner_title', 'Invoice Help Your Business Quickly' );
        set_theme_mod( 'chartered_accountant_banner_text', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s,' );
        set_theme_mod( 'chartered_accountant_banner_button_label', 'Explore More' );
        set_theme_mod( 'chartered_accountant_banner_button_url', '#' );
        set_theme_mod( 'chartered_accountant_banner_image_sec', get_template_directory_uri().'/assets/images/banner-img.png' );
        set_theme_mod( 'chartered_accountant_banner_paid_text', 'Total Invoice Paid' );
        set_theme_mod( 'chartered_accountant_banner_paid_price', '$5,489' );
        set_theme_mod( 'chartered_accountant_banner_tax_icon', 'fa-solid fa-file-lines' );
        set_theme_mod( 'chartered_accountant_banner_tax_price', '$783.00' );
        set_theme_mod( 'chartered_accountant_banner_text_text', 'Every Month Tax Pay' );
        set_theme_mod( 'chartered_accountant_banner_left_bottom_text', 'Chartered' );
        set_theme_mod( 'chartered_accountant_banner_left_top_text', 'Accounted' );

        $chartered_accountant_box_icon = array('fa-solid fa-file-lines', 'fa-solid fa-chart-column', 'fa-solid fa-user-tie');
        $chartered_accountant_box_title = array('International Accounting', 'Accounting Performance', 'Expert Accountants');
        $chartered_accountant_box_text = array('Lorem Ipsum is simply dummy text of the printing and typesetting industry.', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.', 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.');
        for ($chartered_accountant_i = 0; $chartered_accountant_i < 3; $chartered_accountant_i++) {
            set_theme_mod('chartered_accountant_banner_bottom_icon' . ($chartered_accountant_i + 1), $chartered_accountant_box_icon[$chartered_accountant_i]);
            set_theme_mod('chartered_accountant_banner_bottom_title' . ($chartered_accountant_i + 1), $chartered_accountant_box_title[$chartered_accountant_i]);
            set_theme_mod('chartered_accountant_banner_bottom_text' . ($chartered_accountant_i + 1), $chartered_accountant_box_text[$chartered_accountant_i]);
        }

        set_theme_mod( 'chartered_accountant_banner_phone_icon', 'fa-solid fa-phone' );
        set_theme_mod( 'chartered_accountant_banner_phone_text', 'Contact Now' );
        set_theme_mod( 'chartered_accountant_banner_phone_number', '+12 3456789123' );

        // Our Case Section 
        set_theme_mod( 'chartered_accountant_our_case_section_text', 'Our Case' );
        set_theme_mod( 'chartered_accountant_our_case_section_title', 'Recent Case Studies' );
        set_theme_mod('chartered_accountant_discovery_category', 'Case1');

            // Define post category names and post titles
            $chartered_accountant_category_names = array('Case1', 'Case2');
            $chartered_accountant_title_array = array(
                array("Financial Report 2023", "Financial Report 2024", "Financial Report 2025", "Financial Report 2022"),
                array("Financial Report 2022", "Financial Report 2023", "Financial Report 2024", "Financial Report 2025")
            );

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

                for ($chartered_accountant_i = 0; $chartered_accountant_i < 4; $chartered_accountant_i++) {
                    // Create post content
                    $chartered_accountant_title = $chartered_accountant_title_array[$chartered_accountant_index][$chartered_accountant_i];
                    $chartered_accountant_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, when an unknown printer took a galley of type and scrambled it to make a type specimen book.';

                    // Create post post object
                    $chartered_accountant_my_post = array(
                        'post_title'    => wp_strip_all_tags($chartered_accountant_title),
                        'post_content'  => $chartered_accountant_content,
                        'post_status'   => 'publish',
                        'post_type'     => 'post', // Post type set to 'post'
                    );

                    // Insert the post into the database
                    $chartered_accountant_post_id = wp_insert_post($chartered_accountant_my_post);

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

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

                    // Handle the featured image using media_sideload_image
                    $chartered_accountant_image_url = get_template_directory_uri() . '/assets/images/service' . ($chartered_accountant_i + 1) . '.png';
                    $chartered_accountant_image_id = media_sideload_image($chartered_accountant_image_url, $chartered_accountant_post_id, null, 'id');

                    if (is_wp_error($chartered_accountant_image_id)) {
                        error_log('Error downloading image: ' . $chartered_accountant_image_id->get_error_message());
                        continue; // Skip to the next post if image download fails
                    }
                    // Assign featured image to post
                    set_post_thumbnail($chartered_accountant_post_id, $chartered_accountant_image_id);
                }
            } 
            
        //Copyright Text
        set_theme_mod( 'chartered_accountant_footer_text', 'By VWThemes' );  
     
        }
    ?>
  
    <form action="<?php echo esc_url(home_url()); ?>/wp-admin/themes.php?page=chartered_accountant_guide" method="POST" onsubmit="return validate(this);">
        <?php if (!get_option('chartered_accountant_demo_import_completed')) : ?>
        <form method="post">   
            <p class="run-import-text"><?php esc_html_e('Click On The Below Run Importer Button To Import Demo Content Of chartered accountant', 'chartered-accountant'); ?></p>
                <p><?php esc_html_e('Please back up your website if it’s already live with data. This importer will overwrite your existing settings with the new customizer values for chartered accountant', 'chartered-accountant'); ?></p>
                <input class="run-import" type="submit" name="submit" value="<?php esc_attr_e('Run Importer', 'chartered-accountant'); ?>" class="button button-primary button-large">
        </form>
        <?php endif; ?>
        <div id="spinner" style="display:none;">         
            <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/images/spinner.png" alt="" />
        </div>
    </form>
    <script type="text/javascript">
        function validate(form) {
            if (confirm("Do you really want to import the theme demo content?")) {
                // Show the spinner
                document.getElementById('spinner').style.display = 'block';
                // Allow the form to be submitted
                return true;
            } 
            else {
                return false;
            }
        }
    </script>
</div>
