<div class="theme-offer">
	<?php
	$visa_consulting_service_demo_import_completed = get_option( 'visa_consulting_service_demo_import_completed', false );

	if ( $visa_consulting_service_demo_import_completed ) {
		echo '<p class="notice-text">' . esc_html__( 'Your demo import has been completed successfully.', 'visa-consulting-service' ) . '</p>';
		echo '<span><a href="' . esc_url( home_url() ) . '" class="site-btn" target="_blank">' . esc_html__( 'VIEW SITE', 'visa-consulting-service' ) . '</a></span>';
	}

	if ( isset( $_POST['submit'] ) && current_user_can( 'edit_theme_options' ) ) {
		if ( ! function_exists( 'visa_consulting_service_attach_theme_image' ) ) {
			function visa_consulting_service_attach_theme_image( $visa_consulting_service_relative_path, $visa_consulting_service_post_id = 0 ) {
				$visa_consulting_service_source_path = trailingslashit( get_template_directory() ) . ltrim( $visa_consulting_service_relative_path, '/' );
				if ( ! file_exists( $visa_consulting_service_source_path ) ) {
					return 0;
				}

				$visa_consulting_service_filename = wp_basename( $visa_consulting_service_source_path );
				$visa_consulting_service_bits     = wp_upload_bits( $visa_consulting_service_filename, null, file_get_contents( $visa_consulting_service_source_path ) );
				if ( ! empty( $visa_consulting_service_bits['error'] ) ) {
					return 0;
				}

				$visa_consulting_service_filetype   = wp_check_filetype( $visa_consulting_service_filename, null );
				$visa_consulting_service_attachment = array(
					'post_mime_type' => $visa_consulting_service_filetype['type'],
					'post_title'     => sanitize_file_name( pathinfo( $visa_consulting_service_filename, PATHINFO_FILENAME ) ),
					'post_content'   => '',
					'post_status'    => 'inherit',
				);

				$visa_consulting_service_attach_id = wp_insert_attachment( $visa_consulting_service_attachment, $visa_consulting_service_bits['file'], $visa_consulting_service_post_id );
				if ( is_wp_error( $visa_consulting_service_attach_id ) ) {
					return 0;
				}

				require_once ABSPATH . 'wp-admin/includes/image.php';
				$visa_consulting_service_metadata = wp_generate_attachment_metadata( $visa_consulting_service_attach_id, $visa_consulting_service_bits['file'] );
				wp_update_attachment_metadata( $visa_consulting_service_attach_id, $visa_consulting_service_metadata );
				return (int) $visa_consulting_service_attach_id;
			}
		}

		$visa_consulting_service_menu_name     = 'Main Menus';
		$visa_consulting_service_menu_location = 'primary';
		$visa_consulting_service_menu_object   = wp_get_nav_menu_object( $visa_consulting_service_menu_name );
		$visa_consulting_service_menu_id       = $visa_consulting_service_menu_object ? (int) $visa_consulting_service_menu_object->term_id : (int) wp_create_nav_menu( $visa_consulting_service_menu_name );

		$visa_consulting_service_pages = array(
			array(
				'title'   => 'Home',
				'slug'    => 'home',
				'content' => '',
			),
			array(
				'title'   => 'Visa',
				'slug'    => 'visa',
				'content' => 'Our Visa services provide complete assistance for application, documentation, and approval processes, ensuring a smooth and hassle-free journey for students and professionals.',
			),
			array(
				'title'   => 'Countries',
				'slug'    => 'countries',
				'content' => 'Explore top destinations worldwide for study, work, and travel. Get detailed insights into visa requirements, opportunities, and benefits for each country.',
			),
			array(
				'title'   => 'Service',
				'slug'    => 'service',
				'content' => 'We offer a wide range of consulting services including visa guidance, documentation support, interview preparation, and personalized consultation tailored to your needs.',
			),
			array(
				'title'   => 'Blog',
				'slug'    => 'blog',
				'content' => 'Read our latest blogs for expert tips, visa updates, travel advice, and helpful resources to guide you through every step of your journey.',
			),
		);

		$visa_consulting_service_page_ids = array();
		foreach ( $visa_consulting_service_pages as $visa_consulting_service_page_data ) {
			$visa_consulting_service_existing_page = get_page_by_path( $visa_consulting_service_page_data['slug'], OBJECT, 'page' );
			if ( $visa_consulting_service_existing_page ) {
				$visa_consulting_service_page_ids[ $visa_consulting_service_page_data['slug'] ] = (int) $visa_consulting_service_existing_page->ID;
				continue;
			}

			$visa_consulting_service_page_ids[ $visa_consulting_service_page_data['slug'] ] = (int) wp_insert_post(
				array(
					'post_type'    => 'page',
					'post_title'   => $visa_consulting_service_page_data['title'],
					'post_content' => $visa_consulting_service_page_data['content'],
					'post_status'  => 'publish',
					'post_author'  => 1,
					'post_name'    => $visa_consulting_service_page_data['slug'],
				)
			);
		}

			if ( ! empty( $visa_consulting_service_page_ids['home'] ) ) {
			update_option( 'page_on_front', $visa_consulting_service_page_ids['home'] );
			update_option( 'show_on_front', 'page' );
			update_post_meta( $visa_consulting_service_page_ids['home'], '_wp_page_template', 'page-template/home-page.php' );
		}
		if ( ! empty( $visa_consulting_service_page_ids['blog'] ) ) {
			update_option( 'page_for_posts', $visa_consulting_service_page_ids['blog'] );
		}

		$visa_consulting_service_existing_menu_items = wp_get_nav_menu_items( $visa_consulting_service_menu_id );
		$visa_consulting_service_menu_object_ids     = array();
		if ( ! empty( $visa_consulting_service_existing_menu_items ) ) {
			foreach ( $visa_consulting_service_existing_menu_items as $visa_consulting_service_menu_item ) {
				$visa_consulting_service_menu_object_ids[] = (int) $visa_consulting_service_menu_item->object_id;
			}
		}

		foreach ( $visa_consulting_service_pages as $visa_consulting_service_page_data ) {
			$visa_consulting_service_page_id = isset( $visa_consulting_service_page_ids[ $visa_consulting_service_page_data['slug'] ] ) ? (int) $visa_consulting_service_page_ids[ $visa_consulting_service_page_data['slug'] ] : 0;
			if ( ! $visa_consulting_service_page_id || in_array( $visa_consulting_service_page_id, $visa_consulting_service_menu_object_ids, true ) ) {
				continue;
			}

			wp_update_nav_menu_item(
				$visa_consulting_service_menu_id,
				0,
				array(
					'menu-item-title'     => $visa_consulting_service_page_data['title'],
					'menu-item-status'    => 'publish',
					'menu-item-object-id' => $visa_consulting_service_page_id,
					'menu-item-object'    => 'page',
					'menu-item-type'      => 'post_type',
				)
			);
		}

		$visa_consulting_service_locations                           = get_theme_mod( 'nav_menu_locations', array() );
		$visa_consulting_service_locations[ $visa_consulting_service_menu_location ] = $visa_consulting_service_menu_id;
		set_theme_mod( 'nav_menu_locations', $visa_consulting_service_locations );

		$visa_consulting_service_resources_category_slug = 'learning-resources';
		$visa_consulting_service_resources_term          = term_exists( $visa_consulting_service_resources_category_slug, 'category' );
		if ( ! $visa_consulting_service_resources_term ) {
			$visa_consulting_service_resources_term = wp_insert_term( 'Learning Resources', 'category', array( 'slug' => $visa_consulting_service_resources_category_slug ) );
		}
		$visa_consulting_service_resources_term_id = is_wp_error( $visa_consulting_service_resources_term ) ? 0 : (int) $visa_consulting_service_resources_term['term_id'];

		$visa_consulting_service_resources_posts = array(
			array(
				'title'   => 'Mastering Mathematics Basics',
				'slug'    => 'mastering-mathematics-basics',
				'content' => 'Create a practical study routine with realistic goals, short sessions, and regular revision time.',
				'image'   => 'images/demo/resource-1.png',
			),
			array(
				'title'   => 'Effective Study Techniques',
				'slug'    => 'effective-study-techniques',
				'content' => 'Use topic planning, timed practice, and active recall methods to improve exam performance.',
				'image'   => 'images/demo/resource-2.png',
			),
			array(
				'title'   => 'Grammar Made Easy',
				'slug'    => 'grammar-made-easy',
				'content' => 'Break complex concepts into smaller parts and combine notes with problem-solving practice.',
				'image'   => 'images/demo/resource-3.png',
			),
			array(
				'title'   => 'Science Simplified for Students',
				'slug'    => 'science-simplified-for-students',
				'content' => 'Prioritize assignments and reduce distractions using simple planning tools and daily checklists.',
				'image'   => 'images/demo/resource-4.png',
			),
		);

		$visa_consulting_service_banner_image_url = get_template_directory_uri() . '/images/demo/banner-right.png';

		foreach ( $visa_consulting_service_resources_posts as $visa_consulting_service_resource_post ) {
			$visa_consulting_service_existing_post = get_page_by_path( $visa_consulting_service_resource_post['slug'], OBJECT, 'post' );
			$visa_consulting_service_post_id       = $visa_consulting_service_existing_post ? (int) $visa_consulting_service_existing_post->ID : (int) wp_insert_post(
				array(
					'post_type'    => 'post',
					'post_title'   => $visa_consulting_service_resource_post['title'],
					'post_content' => $visa_consulting_service_resource_post['content'],
					'post_status'  => 'publish',
					'post_author'  => 1,
					'post_name'    => $visa_consulting_service_resource_post['slug'],
				)
			);

			if ( $visa_consulting_service_post_id && $visa_consulting_service_resources_term_id ) {
				wp_set_post_categories( $visa_consulting_service_post_id, array( $visa_consulting_service_resources_term_id ), false );
			}

			if ( $visa_consulting_service_post_id && ! empty( $visa_consulting_service_resource_post['image'] ) ) {
				$visa_consulting_service_image_id = visa_consulting_service_attach_theme_image( $visa_consulting_service_resource_post['image'], $visa_consulting_service_post_id );
				if ( $visa_consulting_service_image_id ) {
					set_post_thumbnail( $visa_consulting_service_post_id, $visa_consulting_service_image_id );
				}
			}
		}

		// Banner and section demo settings.
		update_option( 'blogname', 'Visa Consulting' );
		set_theme_mod( 'visa_consulting_service_site_title_enable', true );
		set_theme_mod( 'visa_consulting_service_site_tagline_enable', true );

		set_theme_mod( 'visa_consulting_service_topbar_hide', true );
		set_theme_mod( 'visa_consulting_service_topbar_text', 'Welcome to Immigration and Visa Consulting Company' );
		set_theme_mod( 'visa_consulting_service_facebook_url', '#' );
		set_theme_mod( 'visa_consulting_service_twitter_url', '#' );
		set_theme_mod( 'visa_consulting_service_instagram_url', '#' );
		set_theme_mod( 'visa_consulting_service_banner_hide', true );
		set_theme_mod( 'visa_consulting_service_banner_sub_title', 'Welcome To Visa Consulting WordPress Theme' );
		set_theme_mod( 'visa_consulting_service_banner_title', 'Top Tips for a Successful Visa Interview' );
		set_theme_mod( 'visa_consulting_service_banner_desc_text', 'Achieving permanent residency is a milestone for immigrants. This guide details the steps involved in applying for permanent residency, including eligibility requirements, application process, and the benefits of becoming a permanent resident.' );
		set_theme_mod( 'visa_consulting_service_banner_btn_text1', 'Explore More' );
		set_theme_mod( 'visa_consulting_service_banner_btn_url1', '#' );
		set_theme_mod( 'visa_consulting_service_banner_btn_text2', 'Get In Tech' );
		set_theme_mod( 'visa_consulting_service_banner_btn_url2', '#' );
		set_theme_mod( 'visa_consulting_service_banner_right_image', get_template_directory_uri().'/images/banner-img.png' );

		for ( $visa_consulting_service_i = 1; $visa_consulting_service_i <= 5; $visa_consulting_service_i++ ) {
			set_theme_mod( 'visa_consulting_service_banner_country_image' .$visa_consulting_service_i, get_template_directory_uri().'/images/country' . $visa_consulting_service_i . '.png' );
		}

		set_theme_mod('visa_consulting_service_select_category', 'Visa1');

        // Define post category names and post titles
        $visa_consulting_service_category_names = array('Visa1', 'Visa2');
        $visa_consulting_service_title_array = array(
            array("Student Visa", "Tourist Visa", "Commercial Visa", "Residence Visa"),
            array("Transit Visa", "Medical Visa", "Conference Visa", "Journalist Visa")
        );

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

            for ($visa_consulting_service_i = 0; $visa_consulting_service_i < 4; $visa_consulting_service_i++) {
                // Create post content
                $visa_consulting_service_title = $visa_consulting_service_title_array[$visa_consulting_service_index][$visa_consulting_service_i];
                $visa_consulting_service_content = 'Explore a world without borders with our expert immigration solutions. Turn your global dreams into reality with trusted guidance every step of the way.';

                // Create post post object
                $visa_consulting_service_my_post = array(
                    'post_title'    => wp_strip_all_tags($visa_consulting_service_title),
                    'post_content'  => $visa_consulting_service_content,
                    'post_status'   => 'publish',
                    'post_type'     => 'post', // Post type set to 'post'
                );

                // Insert the post into the database
                $visa_consulting_service_post_id = wp_insert_post($visa_consulting_service_my_post);

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

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

                // Handle the featured image using media_sideload_image
                $visa_consulting_service_image_url = get_template_directory_uri() . '/images/visa' . ($visa_consulting_service_i + 1) . '.png';
                $visa_consulting_service_image_id = media_sideload_image($visa_consulting_service_image_url, $visa_consulting_service_post_id, null, 'id');

                if (is_wp_error($visa_consulting_service_image_id)) {
                    error_log('Error downloading image: ' . $visa_consulting_service_image_id->get_error_message());
                    continue; // Skip to the next post if image download fails
                }
                // Assign featured image to post
                set_post_thumbnail($visa_consulting_service_post_id, $visa_consulting_service_image_id);
            }
        }

		update_option( 'visa_consulting_service_demo_import_completed', true );
		echo '<p class="notice-text">' . esc_html__( 'Your demo import has been completed successfully.', 'visa-consulting-service' ) . '</p>';
		echo '<span><a href="' . esc_url( home_url() ) . '" class="button button-primary site-btn" target="_blank">' . esc_html__( 'VIEW SITE', 'visa-consulting-service' ) . '</a></span>';
	}
	?>

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