(function ($) {
	'use strict';

	jQuery(document).ready(function ($) {

		$('.qcld-feature-accordion-header, .qc-feature-accordion-header').on('click', function () {
			$(this).parent().toggleClass('active').siblings().removeClass('active');
		});



		// open ai settings
		var qcld_check_temperature = $('#qcld_seohelp_temperature');
		if (qcld_check_temperature.length > 0) {
			var slidertemperature = new Slider("#qcld_seohelp_temperature");
			slidertemperature.on("slide", function (sliderValue) {
				document.getElementById("temperatureVal").textContent = sliderValue;
			});
		}

		var qcld_presence_penalty = $('#qcld_seohelp_presence_penalty');
		if (qcld_presence_penalty.length > 0) {
			var sliderpp = new Slider("#qcld_seohelp_presence_penalty");
			sliderpp.on("slide", function (sliderValue) {
				document.getElementById("presence_penaltyVal").textContent = sliderValue;
			});
		}

		var qcld_frequency_penalty = $('#qcld_seohelp_frequency_penalty');
		if (qcld_frequency_penalty.length > 0) {
			var sliderfp = new Slider("#qcld_seohelp_frequency_penalty");
			sliderfp.on("slide", function (sliderValue) {
				document.getElementById("frequency_penaltyVal").textContent = sliderValue;
			});
		}

		// gemini ai settings
		var qcld_gemini_ai_temperature = $('#qcld_gemini_ai_temperature');
		if (qcld_gemini_ai_temperature.length > 0) {
			var slidertemperature = new Slider("#qcld_gemini_ai_temperature");
			slidertemperature.on("slide", function (sliderValue) {
				document.getElementById("qcld_gemini_ai_temperature_val").textContent = sliderValue;
			});
		}

		var qcld_gemini_ai_top_k = $('#qcld_gemini_ai_top_k');
		if (qcld_gemini_ai_top_k.length > 0) {
			var sliderpp = new Slider("#qcld_gemini_ai_top_k");
			sliderpp.on("slide", function (sliderValue) {
				document.getElementById("qcld_gemini_ai_top_k_val").textContent = sliderValue;
			});
		}

		var qcld_gemini_ai_top_p = $('#qcld_gemini_ai_top_p');
		if (qcld_gemini_ai_top_p.length > 0) {
			var sliderfp = new Slider("#qcld_gemini_ai_top_p");
			sliderfp.on("slide", function (sliderValue) {
				document.getElementById("qcld_gemini_ai_top_p_val").textContent = sliderValue;
			});
		}

		var qcld_seohelp_openrouter_temperature = $('#qcld_seohelp_openrouter_temperature');
		if (qcld_seohelp_openrouter_temperature.length > 0) {
			var qcld_seohelp_openrouter_temperature = new Slider("#qcld_seohelp_openrouter_temperature");
			qcld_seohelp_openrouter_temperature.on("slide", function (sliderValue) {
				document.getElementById("qcld_seohelp_openrouter_temperature_val").textContent = sliderValue;
			});
		}

		//code for title generator
		$(document).on('click', '#title_generator', function (event) {
			// $("#title_generator").on("click", function(event){
			//console.log('working')
			var titles = $("#title");
			if (titles.length == '') {
				var title = $(".editor-post-title").children().children().find('textarea').val();
			} else {
				var title = $("#title").val();
			}

			$.post(
				qcld_seohelp_ajaxurl,
				{
					action: 'qcld_seohelp_show_suggestion',
					security: qcld_seohelp_ajax_nonce,
					linkbait_title: title
				},
				function (data) {
					$('#title_Generator').modal('show');
					$('#title_Generator').addClass('show');
					$('#wpwrap').append(data);

				}
			)

		});
		$(document).on('click', '#qcld_keyword_search', function (event) {
			var qcld_keyword_suggestion = $('#qcld_keyword_suggestion_text').val();

			var languageSpecifics = $("#qcld-seo-language").find(":selected").val();
			var languageSpecificsArray = languageSpecifics.split('-');
			var selectedCountry = languageSpecificsArray[0];
			var selectedlanguage = languageSpecificsArray[1];

			$('#qcld_keyword_search').addClass('spinning');
			$('#qcld_keyword_search').prop("disabled", true);

			if (qcld_keyword_suggestion == '') {
				alert('Please Add Keyword Suggestion');
			}


			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcldseohelp_keyword_suggestion_tag',
					'security': qcld_seohelp_ajax_nonce,
					'keyword': qcld_keyword_suggestion,
					'selectedCountry': selectedCountry,
					'selectedlanguage': selectedlanguage,
				},
				dataType: 'json',
				success: function (response) {
					$('#qcld_keyword_search').prop("disabled", false);
					$('#qcld_keyword_search').removeClass('spinning');
					$('#keywords_result').html(response.keywords);
					$('#keywords_resultBackdrop').modal('show');
					$('#keywords_resultBackdrop').addClass('show');
				}
			});

		});
		$(document).on('click', '#seo_help_createtag', function (event) {
			var currentDom = $(this);
			var postid = $('.postid').val();
			var valkeyword = [];
			$('.form-check-input:checkbox:checked').each(function () {
				valkeyword.push($(this).val());
			});

			$.post(
				qcld_seohelp_ajaxurl,
				{
					action: 'qcld_seohelp_add_ontag',
					security: qcld_seohelp_ajax_nonce,
					keywordkeys: valkeyword,
					postid: postid
				},
				function (response) {

					$('#keywords_resultBackdrop').modal('hide');

					var conFirm = confirm(response.keywords);
					if (conFirm == true) {
						window.location.reload();
					}


				}
			);

		});
		// qcld_suggest_title
		$(document).on('click', '#qcld_suggest_title', function (event) {
			event.preventDefault();
			var currentDom = $(this);
			var post_id = currentDom.data('post_id');

			if (post_id == '') {
				alert('Please Add Title Suggestion');
			}

			currentDom.addClass('spinning');
			currentDom.prop("disabled", true);

			$('#qcld_suggest_title_modal .modal-footer').hide();
			$('#qcld_suggest_title_modal').modal('show');
			$('#qcld_suggest_title_modal').addClass('show');
			$('#qcld_suggest_result').html('');
			$('#qcld_suggest_result').html('<div class="qcld_double_bounce_wrap"><div class="qcld_double_bounce_content"><div class="qcld_double_bounce_1"></div><div class="qcld_double_bounce_2"></div></div></div>');

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_suggestion_title',
					'security': qcld_seohelp_ajax_nonce,
					'post_id': post_id
				},
				dataType: 'json',
				success: function (response) {
					currentDom.prop("disabled", false);
					currentDom.removeClass('spinning');
					$('#qcld_suggest_result').html(response.keywords);
					$('#qcld_suggest_title_modal .modal-footer').show();
				}
			});

		});
		// qcld_suggest_title
		$(document).on('click', '.qcld_seohelp_suggest_title', function (event) {
			event.preventDefault();
			var currentDom = $(this);
			var suggest_title = currentDom.text();

			if (suggest_title == '') {
				alert('Please Add Keyword Suggestion');
			}

			$('#qcld_suggest_title_modal').modal('hide');

			$(".editor-post-title__input").text(suggest_title);

		});
		// qcld_suggest_excerpt
		$(document).on('click', '#qcld_suggest_excerpt', function (event) {
			event.preventDefault();
			var currentDom = $(this);
			var post_id = currentDom.data('post_id');

			if (post_id == '') {
				alert('Please Add Excerpt Suggestion');
			}

			currentDom.addClass('spinning');
			currentDom.prop("disabled", true);

			$('#qcld_suggest_excerpt_modal .modal-footer').hide();
			$('#qcld_suggest_excerpt_modal').modal('show');
			$('#qcld_suggest_excerpt_modal').addClass('show');
			$('#qcld_suggest_excerpt_result').html('');
			$('#qcld_suggest_excerpt_result').html('<div class="qcld_double_bounce_wrap"><div class="qcld_double_bounce_content"><div class="qcld_double_bounce_1"></div><div class="qcld_double_bounce_2"></div></div></div>');

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_suggestion_excerpt',
					'security': qcld_seohelp_ajax_nonce,
					'post_id': post_id
				},
				dataType: 'json',
				success: function (response) {
					currentDom.prop("disabled", false);
					currentDom.removeClass('spinning');
					$('#qcld_suggest_excerpt_result').html(response.keywords);
					$('#qcld_suggest_excerpt_modal .modal-footer').show();
				}
			});

		});
		// qcld_suggest_excerpt
		$(document).on('click', '.qcld_seohelp_suggest_excerpt', function (event) {
			event.preventDefault();
			var currentDom = $(this);
			var suggest_excerpt = currentDom.text();
			var post_id = currentDom.data('post_id');

			if (suggest_excerpt == '') {
				alert('Excerpt data not found');
			}

			$('#qcld_suggest_excerpt_modal').modal('hide');

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_suggestion_excerpt_save',
					'security': qcld_seohelp_ajax_nonce,
					'suggest_excerpt': suggest_excerpt,
					'post_id': post_id
				},
				dataType: 'json',
				success: function (response) {

					alert(response.results);

				}
			});

		});
		// qcld_grammar_checker_post
		$(document).on('click', '#qcld_translate_test', function (event) {
			event.preventDefault();
			var currentDom = $(this);
			var post_id = currentDom.data('post_id');

			if (post_id == '') {
				alert('Please Add Excerpt Suggestion');
			}

			currentDom.addClass('spinning');
			currentDom.prop("disabled", true);

			$('#qcld_translate_test_result_data').hide();
			$('#qcld_translate_test_modal .modal-footer').hide();
			$('#qcld_translate_test_modal').modal('show');
			$('#qcld_translate_test_modal').addClass('show');
			$('#qcld_translate_test_result').html('');
			$('#qcld_translate_test_result').html('<div class="qcld_double_bounce_wrap"><div class="qcld_double_bounce_content"><div class="qcld_double_bounce_1"></div><div class="qcld_double_bounce_2"></div></div></div>');


			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_translate_post',
					'security': qcld_seohelp_ajax_nonce,
					'post_id': post_id
				},
				dataType: 'json',
				success: function (response) {
					currentDom.prop("disabled", false);
					currentDom.removeClass('spinning');
					$('#qcld_translate_test_result').html('');
					$('#qcld_translate_test_result').hide();
					$('#qcld_translate_test_result_data').show();
					var editor = tinyMCE.get('qcld_translate_test_result_msg');
					var basicEditor = true;
					if ($('#wp-qcld_translate_test_result_msg-wrap').hasClass('tmce-active') && editor) {
						basicEditor = false;
					}
					if (basicEditor) {
						$('#qcld_translate_test_result_msg').val(response.keywords);
					} else {
						editor.setContent(response.keywords);
					}
					$('#qcld_translate_test_modal .modal-footer').show();
				}
			});

		});
		// qcld_grammar_checker_post
		$(document).on('click', '#qcld_grammar_checker_post', function (event) {
			event.preventDefault();
			var currentDom = $(this);
			var post_id = currentDom.data('post_id');

			if (post_id == '') {
				alert('Please Add Excerpt Suggestion');
			}

			currentDom.addClass('spinning');
			currentDom.prop("disabled", true);

			$('#qcld_grammar_checker_modal .modal-footer').hide();
			$('#qcld_grammar_checker_modal').modal('show');
			$('#qcld_grammar_checker_modal').addClass('show');
			$('#qcld_grammar_checker_result').html('');
			$('#qcld_grammar_checker_result').html('<div class="qcld_double_bounce_wrap"><div class="qcld_double_bounce_content"><div class="qcld_double_bounce_1"></div><div class="qcld_double_bounce_2"></div></div></div>');

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_grammer_checker',
					'security': qcld_seohelp_ajax_nonce,
					'post_id': post_id
				},
				dataType: 'json',
				success: function (response) {
					currentDom.prop("disabled", false);
					currentDom.removeClass('spinning');
					$('#qcld_grammar_checker_result').html(response.keywords);
					$('#qcld_grammar_checker_modal .modal-footer').show();
				}
			});

		});
		//code for seo tips
		$(".seo_tips_rand").on("click", function (event) {
			var seo_tips_id = this.id;
			$.post( // Ajax request for Content Writting Tips//
				qcld_seohelp_ajaxurl,
				{
					action: 'qcld_seohelp_seo_tips',
					security: qcld_seohelp_ajax_nonce,
					linkbait_seo_id: seo_tips_id
				},
				function (data) {
					$('#wpwrap').append(data);
				}
			)
		});

		// Closing Modal //
		$(document).on('click', '.modal-content .close', function () {
			$(this).parent().parent().remove();
		});

		$(document).on('click', '.modal-content #linkbait_add', function (e) { //Adding data //
			e.preventDefault();
			var currentDom = $(this);
			var $selected_title = $('input[name="linkbait_radio"]:checked').val();
			if ($selected_title) {
				$("#title-prompt-text").html('');
				var titles = $("#title");
				var props = $("#title");
				if (titles.length == '') {

					var $temp = $("<input>");
					currentDom.append($temp);
					$temp.val($selected_title).select();
					document.execCommand("copy");
					$temp.remove();

					//navigator.clipboard.writeText($selected_title);
					$('<div class="copy-notification">Copied Title</div>').prependTo('.qcld_seohelp_copy_msg').delay(2000).fadeOut(600, function () {
						$('.copy-notification').remove();
					});


				} else {

					$("#title").val($selected_title);


					var $temp = $("<input>");
					currentDom.append($temp);
					$temp.val($selected_title).select();
					document.execCommand("copy");
					$temp.remove();

					//navigator.clipboard.writeText($selected_title);
					$('<div class="copy-notification">Copied Title</div>').prependTo('.qcld_seohelp_copy_msg').delay(2000).fadeOut(600, function () {
						$('.copy-notification').remove();
					});

					$('<div class="copy-notification">Copied Title</div>').prependTo('.qcld_seohelp_copy_msg').delay(2000).fadeOut(600, function () {
						$('.copy-notification').remove();
					});

				}


			} else {

				alert('Could not find any Selected Title!!');

			}
		});


		$(document).on('click', '.modal-content #linkbait_singular', function () { //Code for Singular //
			$("#linkbait_skip").val("");
			$("#linkbait_skip2").val("");
		});

		$(document).on('click', '.modal-content #linkbait_plural', function () { //Code for Plural
			$("#linkbait_skip").val("");
			$("#linkbait_skip2").val("");
		});

		$(document).on('click', '.modal-content #linkbait_generate', function () { //Linkbait generate Operation //

			var filter = '';
			if (document.getElementById('linkbait_singular').checked) {
				filter = document.getElementById('linkbait_singular').value;
			}
			if (document.getElementById('linkbait_plural').checked) {
				filter = document.getElementById('linkbait_plural').value;
			}
			if (document.getElementById('linkbait_google').checked) {
				filter = document.getElementById('linkbait_google').value;
			}
			if (document.getElementById('linkbait_openai').checked) {
				filter = document.getElementById('linkbait_openai').value;
			}


			var $skip = $("#linkbait_skip").val();
			var $skip2 = $("#linkbait_skip2").val();
			var $subject = $("#linkbait_subject").val();

			if ($subject) {
				$('#linkbait_loading').show();
				$.post( //Making Ajax request for data //
					qcld_seohelp_ajaxurl,
					{
						action: 'qcld_seohelp_generate_suggestion',
						security: qcld_seohelp_ajax_nonce,
						linkbait_title: $subject,
						linkbait_skip: $skip,
						linkbait_skip2: $skip2,
						linkbait_filter: filter
					},
					function (data) {
						$('#linkbait_ajax_data').html(data).fadeIn('slow');
						$('#linkbait_loading').hide();
					}
				)

			} else {
				alert('Please put subject.');
				$("#linkbait_subject").focus();
			}
		});

		$(document).on('click', '#content_generator', function () {

			//console.log('working');
			$('#content_Generator_modal').modal({ backdrop: 'static', keyboard: false }, 'show');
			jQuery('#content_Generator_modal').modal('show')

		});

		$(document).on('click', '#qcld_article_keyword_suggestion', function () {

			var qcld_keyword_suggestion = $('#qcld_article_keyword_suggestion_mf').val();
			var qcld_keyword_number = $('#qcld_keyword_number').val();
			var qcld_article_language = $('#qcld_article_language').val();
			var qcld_article_number_of_heading = $('#qcld_article_number_of_heading').val();
			var qcld_article_heading_tag = $('#qcld_article_heading_tag').val();
			var qcld_article_heading_style = $('#qcld_article_heading_style').val();
			var qcld_article_heading_tone = $('#qcld_article_heading_tone').val();
			var qcld_article_heading_img = $('#qcld_article_heading_img').val();
			var qcld_article_heading_img = $("input[name=qcld_article_heading_img]:checked").val();
			var qcld_article_heading_tagline = $("input[name=qcld_article_heading_tagline]:checked").val();
			var qcld_article_heading_intro = $("input[name=qcld_article_heading_intro]:checked").val();
			var qcld_article_heading_conclusion = $("input[name=qcld_article_heading_conclusion]:checked").val();
			var qcld_article_label_anchor_text = $('#qcld_article_label_anchor_text').val();
			var qcld_article_target_url = $('#qcld_article_target_url').val();
			var qcld_article_target_label_cta = $('#qcld_article_target_label_cta').val();
			var qcld_article_cta_pos = $('#qcld_article_cta_pos').val();
			var qcld_article_label_keywords = $('#qcld_article_label_keywords').val();
			var qcld_article_label_word_to_avoid = $('#qcld_article_label_word_to_avoid').val();
			var qcld_article_label_keywords_bold = $("input[name=qcld_article_label_keywords_bold]:checked").val();
			var qcld_article_heading_faq = $("input[name=qcld_article_heading_faq]:checked").val();
			var qcld_article_img_size = $('#qcld_article_img_size').val();
			var qcld_article_humanize_text = '';

			var qcld_article_Steps = [];
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('process');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}

			if (qcld_article_heading_img) {
				qcld_article_Steps.push('image');
			}

			if (qcld_keyword_suggestion === '') {
				alert('Please enter title');
			}
			else if (!qcld_article_Steps.length) {
				alert('Please choose least one field');
			}
			else {

				$('#qcld_article_keyword_suggestion').addClass('spinning');
				$('#qcld_article_keyword_suggestion').prop("disabled", true);
				$('#linkbait_article_keyword_data').hide();
				$('.qcld_seo-playground-buttons').hide();

				var data = {
					'action': 'qcldseohelp_keyword_suggestion_content',
					'keyword': qcld_keyword_suggestion,
					'keyword_number': qcld_keyword_number,
					'qcld_article_language': qcld_article_language,
					'qcld_article_number_of_heading': qcld_article_number_of_heading,
					'qcld_article_heading_tag': qcld_article_heading_tag,
					'qcld_article_heading_style': qcld_article_heading_style,
					'qcld_article_heading_tone': qcld_article_heading_tone,
					'qcld_article_heading_img': qcld_article_heading_img,
					'qcld_article_heading_tagline': qcld_article_heading_tagline,
					'qcld_article_heading_intro': qcld_article_heading_intro,
					'qcld_article_heading_conclusion': qcld_article_heading_conclusion,
					'qcld_article_label_anchor_text': qcld_article_label_anchor_text,
					'qcld_article_target_url': qcld_article_target_url,
					'qcld_article_target_label_cta': qcld_article_target_label_cta,
					'qcld_article_cta_pos': qcld_article_cta_pos,
					'qcld_article_label_keywords': qcld_article_label_keywords,
					'qcld_article_label_word_to_avoid': qcld_article_label_word_to_avoid,
					'qcld_article_label_keywords_bold': qcld_article_label_keywords_bold,
					'qcld_article_heading_faq': qcld_article_heading_faq,
					'qcld_article_img_size': qcld_article_img_size,
					'qcld_article_humanize_text': qcld_article_humanize_text,
					'step': '',
					'content': '',
					'security': qcld_seohelp_ajax_nonce
				};

				var data_html = '<div class="qcld_seohelp_ai_generating_wrap">' +
					'<div class="qcld_seohelp_ai_generating_overlay"></div>' +
					'<div class="qcld_seohelp_ai_generating_inner">' +
					'<div class="qcld_seohelp_ai-generating-title">Generating content..</div>' +
					'<div class="qcld_seohelp_ai-generating-process">' +
					'</div>' +
					'<div class="qcld_seohelp_ai-timer">00:00</div>' +
					'</div></div>';

				$('.qcld_seohelp_ai_generating_wrap').remove();
				$('body').append(data_html);

				// console.log(qcld_article_Steps)


				if (window['qcldTimer'] !== undefined) {
					clearTimeout(window['qcldTimer']);
				}

				var ai_timer = $('.qcld_seohelp_ai-timer'), seconds = 0, minutes = 0, t;
				function qcld_add() {
					seconds++;
					if (seconds >= 60) {
						seconds = 0;
						minutes++;
					}
					if (minutes >= 60) {
						minutes = 0;
						hours++;
					}
					var htmlTimer = (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
					ai_timer.html(htmlTimer);
					qcld_timer();

				}

				function qcld_timer() {
					window['qcldTimer'] = setTimeout(qcld_add, 1000);
				}
				qcld_timer();

				QcldStepWorking(data, 0, qcld_article_Steps);

			}



		});

		function QcldStepWorking(data, step, steps) {
			var qcld_next_step = step + 1;
			var qcld_step = steps[step];
			data.step = qcld_step;


			if (data.step == 'process') {
				qcld_generatorProcess('process', 'Processing Content');
			}
			if (data.step == 'content') {
				qcld_generatorProcess('content', 'Generating Content');
			}
			if (data.step == 'heading') {
				qcld_generatorProcess('heading', 'Generating Heading');
			}
			if (data.step == 'intro') {
				qcld_generatorProcess('intro', 'Generating Introduction');
			}
			if (data.step == 'faq') {
				qcld_generatorProcess('faq', 'Generating Q&A');
			}
			if (data.step == 'conclusion') {
				qcld_generatorProcess('conclusion', 'Generating Conclusion');
			}
			if (data.step == 'tagline') {
				qcld_generatorProcess('tagline', 'Generating Tagline');
			}
			if (data.step == 'addition') {
				qcld_generatorProcess('addition', 'Applying Changes');
			}
			if (data.step === 'image') {
				qcld_generatorProcess('image', 'Generating Image');
			}
			if (qcld_step == '') {
				qcld_generatorProcess('finished', 'Finished');
				$('.qcld_seohelp_ai_generating_wrap').remove();
				return false;
			}

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: data,
				dataType: 'json',
				success: function (response) {

					if (response.status == 'success') {
						var response_result = '';
						$('.qcld_seohelp_ai-generating-process').find('.qcld_seohelp_ai-generating-process-' + response.oldStep).addClass('finished');
						$('.qcld_seohelp_ai-generating-process').find('.qcld_seohelp_ai-generating-process-' + response.oldStep + ' span').html('Done');
						if (response.keywords) {
							response_result = response.keywords.replaceAll('\\', '');
							data.content = response.keywords;
						}
						//console.log(data.content);
						if (qcld_next_step === steps.length) {
							qcld_generatorProcess('finished', 'Finished');
							$('.qcld_seohelp_ai_generating_wrap').remove();
							$('#qcld_article_keyword_suggestion').prop("disabled", false);
							$('#qcld_article_keyword_suggestion').removeClass('spinning');
							$('.qcld_seo-playground-buttons').show();
						} else {
							QcldStepWorking(data, qcld_next_step, steps);
						}
						if (response.keywords) {
							$('#linkbait_article_keyword_data').show();
							$('.qcld_copied-content-wrap').remove();
							$('.qcld_content_result_wrap').remove();
							$('#linkbait_article_keyword_data').before('<div class="qcld_copied-content-wrap"><div class="qcld_copied-content_text btn d-none link-success">Copied</div><a class="btn btn-sm btn-secondary qcld-copied-content_text"><span class="dashicons dashicons-admin-page"></span></a></div>');
							$('#linkbait_article_keyword_data').append('<div class="qcld_content_result_wrap"><div class="qcld_rewrite_result_count">' + response.keywords.length + '</div></div>');

							$('#qcld_content_result_msg').focus();
							$('#qcld_content_result_msg').focusout();
							var editor = tinyMCE.get('qcld_content_result_msg');
							var basicEditor = true;
							if ($('#wp-qcld_content_result_msg-wrap').hasClass('tmce-active') && editor) {
								basicEditor = false;
							}
							if (basicEditor) {
								$('#qcld_content_result_msg').val(response_result);
							} else {
								editor.setContent(response_result);
							}
						}

					}
				},
				error: function ($xhr, textStatus, errorThrown) {

					$('#qcld_article_keyword_suggestion').prop("disabled", false);
					$('#qcld_article_keyword_suggestion').removeClass('spinning');
					qcld_generatorProcess('finished', $xhr.responseText);
					$('.qcld_seohelp_ai_generating_wrap').remove();
					alert($xhr.responseText);
				}

			});

		}
		function qcld_generatorProcess(step, message) {
			var wpaicg_generator_process = $('.qcld_seohelp_ai-generating-process');
			if (step === 'finished') {
				wpaicg_generator_process.append('<div class="qcld_seohelp_ai-generating-process-' + step + '"><p>' + message + '</p></div>');
			} else {
				wpaicg_generator_process.append('<div class="qcld_seohelp_ai-generating-process-' + step + '"><p>' + message + '</p><span>In Progress..</span></div>');
			}
		}

		$(document).on('click', '.qcld_article_playground_save', function (e) {
			// $('.qcld_seo-playground-save').click(function (){
			var qcld_seohelp_draft_btn = $(this);
			var title = $('#qcld_article_keyword_suggestion_mf').val();
			var content = $('#qcld_content_result_msg').val();

			if (title === '') {
				alert('Please enter title');
			} else if (content === '') {
				alert('Please wait content generated');
			} else {
				$.ajax({
					url: qcld_seohelp_ajaxurl,
					data: { title: title, content: content, action: 'qcld_seohelp_save_draft_post_extra', security: qcld_seohelp_ajax_nonce },
					dataType: 'json',
					type: 'POST',
					beforeSend: function () {
						qcld_seohelp_draft_btn.attr('disabled', 'disabled');
						qcld_seohelp_draft_btn.append('<span class="spinner"></span>');
						qcld_seohelp_draft_btn.find('.spinner').css('visibility', 'unset');
					},
					success: function (res) {
						qcld_seohelp_draft_btn.removeAttr('disabled');
						qcld_seohelp_draft_btn.find('.spinner').remove();
						if (res.status === 'success') {
							//window.location.href;
							window.location.replace(res.post_link);
						} else {
							alert(res.msg);
						}
					},
					error: function () {
						qcld_seohelp_draft_btn.removeAttr('disabled');
						qcld_seohelp_draft_btn.find('.spinner').remove();
						alert('Something went wrong');
					}
				});
			}
		});
		$(document).on('click', '.qcld_article_playground_clear', function (e) {


			$('#qcld_article_keyword_suggestion_mf').val('');
			$('#qcld_content_result_msg').val('');
			$('.qcld_seo-playground-buttons').hide();
			$('.qcld_rewrite_result_count').hide();

		});


		$(document).on('click', '.qcld-copied-content_text', function (event) {
			var currentDom = $(this);
			var copy_con = currentDom.parent().parent().parent().find('#qcld_content_result_msg').val();
			var copy_text = (copy_con !== '') ? copy_con : currentDom.parent().parent().parent().find('#qcld_content_result_msg').text();

			var editor = tinyMCE.get('qcld_content_result_msg');
			var basicEditor = true;
			if ($('#wp-qcld_content_result_msg-wrap').hasClass('tmce-active') && editor) {
				basicEditor = false;
			}

			if (!basicEditor) {
				var copy_text = editor.getContent();
			}

			currentDom.addClass("qcld_copied");

			currentDom.parent().find(".qcld_copied-content_text").removeClass("d-none");
			setTimeout(() => {
				currentDom.parent().find(".qcld_copied-content_text").addClass("d-none");
			}, 1500);
			//console.log( copy_con );
			//console.log( copy_text );

			var $temp = $("<input>");
			currentDom.append($temp);
			$temp.val(copy_text).select();
			document.execCommand("copy");
			$temp.remove();

		});


		jQuery(document).ready(function ($) {
			$("#tabs").tabs();
		});

		// post & page link quick search
		$(document).on('click', '.qcld_seohelp_linkcheck', function () {

			var $this = $(this);
			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_broken_link_checking_by_ajax',
					'security': qcld_seohelp_ajax_nonce,

				},
				dataType: 'json',
				beforeSend: function () {
					$this.text('Checking...');
					$this.addClass('spinning');
					$('.qcld_seohelp_link_content').html('');
				},
				complete: function () {

				},
				success: function (response) {

					$this.removeClass('spinning');
					$this.text('Start Checking');
					$('.qcld_seohelp_link_content').html(response.html);


				},
				error: function () { }
			});

		});


		// sld link quick search
		$(document).on('click', '.qcld_seohelp_simple_linkcheck', function () {

			var $this = $(this);
			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_broken_sld_link_checking_by_ajax',
					'security': qcld_seohelp_ajax_nonce,
				},
				dataType: 'json',
				beforeSend: function () {
					$this.text('Checking...');
					$this.addClass('spinning');
					$('.qcld_seohelp_sld_link_content').html('');
				},
				complete: function () {

				},
				success: function (response) {

					/*$this.removeClass('spinning');
					$this.text('Start Link Checking');
					$('.qcld_seohelp_sld_link_content').html(response.html);*/

					setTimeout(
						function () {
							$this.removeClass('spinning');
							$this.text('Start Link Checking');
							$('.qcld_seohelp_sld_link_content').html(response.html);
						}, 5000);

				},
				error: function () { }
			});

		});
		$(document).on('click', '#qcld_keyword_suggestion_btn', function () {
			var qcld_keyword_suggestion = $('#qcld_keyword_suggestion').val();

			var languageSpecifics = $("#qcld-seo-language").find(":selected").val();
			var languageSpecificsArray = languageSpecifics.split('-');
			var selectedCountry = languageSpecificsArray[0];
			var selectedlanguage = languageSpecificsArray[1];


			$('#qcld_keyword_suggestion_btn').addClass('spinning');
			$('#qcld_keyword_suggestion_btn').prop("disabled", true);

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcldseohelp_keyword_suggestion',
					'security': qcld_seohelp_ajax_nonce,
					'keyword': qcld_keyword_suggestion,
					'selectedCountry': selectedCountry,
					'selectedlanguage': selectedlanguage,
				},
				dataType: 'json',
				success: function (response) {
					$('#qcld_keyword_suggestion_btn').prop("disabled", false);
					$('#qcld_keyword_suggestion_btn').removeClass('spinning');
					$('#linkbait_keyword_data').empty();
					var keywordobject = JSON.parse(JSON.stringify(response.keywords));
					var keywordkeys = Object.keys(keywordobject);
					for (var i = 0; i < keywordkeys.length; i++) {
						//var fileds = '<div class="form-check form-check-inline"><input type="checkbox" class="form-check-input keywordsuggetions" name="keywordsuggetions_'+i+'" value="' + keywordobject[keywordkeys[i]] +'"/> <label class="form-check-label">' + keywordobject[keywordkeys[i]] +'</label></div>';
						var fileds = '<p>' + keywordobject[keywordkeys[i]] + '</p> ';
						$('#linkbait_keyword_data').append(fileds);
					}
					//$('#linkbait_keyword_data').append('</br><div id="qcld_custom_keyword_input" class="mt-2"></div></br><button id="qcld_outline_suggestion" class="btn btn-info">Create outline</button>');
				}
			});
		});

		$(document).on('click', '#qcld_outline_suggestion', function () {
			var suggesstions = [];
			$('.keywordsuggetions:checkbox:checked').each(function () {
				suggesstions.push($(this).val())
			});


			$('#qcld_outline_suggestion').addClass('spinning');
			$('#qcld_outline_suggestion').prop("disabled", true);


			$.post(
				qcld_seohelp_ajaxurl,
				{
					action: 'qcld_seohelp_outline_data',
					security: qcld_seohelp_ajax_nonce,
					suggesstions: suggesstions
				},
				function (data) {
					$('#qcld_outline_suggestion').prop("disabled", false);
					$('#qcld_outline_suggestion').removeClass('spinning');
					$('#linkbait_outline_data').empty();
					$('#linkbait_keyword_data .words-select').html('');
					if (data.status == 'success') {
						$('#linkbait_outline_data').append(data.results);
					}

				}
			)
		});
		$('body').on('click', '#qcld_seohelp_openai_check_connection', function () {
			var api_key = $('#api_key').val();
			var check_btn = $(this);
			var status_span = $('#qcld_openai_conn_status');

			if (!api_key) {
				status_span.html('<span style="color: red;">Please enter an API key first.</span>');
				return;
			}

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_check_openai_connection',
					'security': qcld_seohelp_ajax_nonce,
					'api_key': api_key
				},
				dataType: 'json',
				beforeSend: function () {
					check_btn.prop('disabled', true).text('Connecting...');
					status_span.html('<span style="color: #666;">Verifying connection...</span>');
				},
				complete: function () {
					check_btn.prop('disabled', false).text('Check Connection');
				},
				success: function (response) {
					//console.log( response );
					if (response.status === 'success') {
						status_span.html('<span style="color: green; font-weight: bold;">' + response.message + '</span>').show();
						
						setTimeout(function() {
							status_span.fadeOut('slow', function() { $(this).empty().show(); });
						}, 5000);

						// Dynamically update the dropdown options
						var engines_select = $('#opeai_engines');
						if (engines_select.length > 0 && response.models) {
							// Clear current options
							engines_select.empty();

							// Populate options
							$.each(response.models, function (index, model_id) {
								var is_selected = (model_id === response.selected) ? 'selected' : '';
								engines_select.append('<option value="' + model_id + '" ' + is_selected + '>' + model_id + '</option>');
							});

							// Trigger change to update UI elements if any
							engines_select.trigger('change');
						}
					} else {
						status_span.html('<span style="color: red;">' + response.message + '</span>');
					}
				},
				error: function () {
					status_span.html('<span style="color: red;">Ajax request failed. Please try again.</span>');
				}
			});
		});

		$('body').on('click', '#qcld_seohelp_gemini_check_connection', function () {
			var api_key = $('#qcld_gemini_api_key').val();
			var check_btn = $(this);
			var status_span = $('#qcld_gemini_conn_status');

			if (!api_key) {
				status_span.html('<span style="color: red;">Please enter a Gemini API key first.</span>');
				return;
			}

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_check_gemini_connection',
					'security': qcld_seohelp_ajax_nonce,
					'api_key': api_key
				},
				dataType: 'json',
				beforeSend: function () {
					check_btn.prop('disabled', true).text('Connecting...');
					status_span.html('<span style="color: #666;">Verifying connection...</span>');
				},
				complete: function () {
					check_btn.prop('disabled', false).text('Check Connection');
				},
				success: function (response) {
					if (response.status === 'success') {
						status_span.html('<span style="color: green; font-weight: bold;">' + response.message + '</span>').show();

						setTimeout(function() {
							status_span.fadeOut('slow', function() { $(this).empty().show(); });
						}, 5000);

						// Dynamically update the dropdown options
						var engines_select = $('#qcld_gemini_model');
						if (engines_select.length > 0 && response.models) {
							// Clear current options
							engines_select.empty();

							// Populate options
							$.each(response.models, function (index, model) {
								var is_selected = (model.id === response.selected) ? 'selected' : '';
								engines_select.append('<option value="' + model.id + '" ' + is_selected + '>' + model.displayName + '</option>');
							});

							engines_select.trigger('change');
						}
					} else {
						status_span.html('<span style="color: red;">' + response.message + '</span>');
					}
				},
				error: function () {
					status_span.html('<span style="color: red;">Ajax request failed. Please try again.</span>');
				}
			});
		});

		$('body').on('click', '.openai_save_settings', function () {
			var api_key = $('#api_key').val();
			var opeai_engines = $('#opeai_engines').val();
			var manual_models = $('#qcld_seohelp_openai_manual_models').val();
			var temperature = $('#qcld_seohelp_temperature').val();
			var presence_penalty = $('#qcld_seohelp_presence_penalty').val();
			var frequency_penalty = $('#qcld_seohelp_frequency_penalty').val();
			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_open_save_settings',
					'security': qcld_seohelp_ajax_nonce,
					'api_key': api_key,
					'opeai_engines': opeai_engines,
					'manual_models': manual_models,
					'temperature': temperature,
					'presence_penalty': presence_penalty,
					'frequency_penalty': frequency_penalty
				},
				dataType: 'json',
				success: function (response) {
					//console.log(response)

					if (response.status == 'success') {
						alert(response.results);
						location.reload();
					}
				}
			});
		});
		$('body').on('click', '.qcld_seohelp_gemini_save_settings', function () {
			var qcld_gemini_api_key = $('#qcld_gemini_api_key').val();
			var qcld_gemini_model = $('#qcld_gemini_model').val();
			var manual_models = $('#qcld_seohelp_gemini_manual_models').val();
			var qcld_gemini_api_version = $('#qcld_gemini_api_version').val();
			var qcld_gemini_ai_temperature = $('#qcld_gemini_ai_temperature').val();
			var qcld_gemini_ai_top_p = $('#qcld_gemini_ai_top_p').val();
			var qcld_gemini_ai_top_k = $('#qcld_gemini_ai_top_k').val();
			var qcld_ai_settings_open_ai = $("input[name=qcld_ai_settings_open_ai]:checked").val();
			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_gemini_save_settings',
					'security': qcld_seohelp_ajax_nonce,
					'qcld_gemini_api_key': qcld_gemini_api_key,
					'qcld_gemini_model': qcld_gemini_model,
					'manual_models': manual_models,
					'qcld_gemini_api_version': qcld_gemini_api_version,
					'qcld_gemini_ai_temperature': qcld_gemini_ai_temperature,
					'qcld_gemini_ai_top_p': qcld_gemini_ai_top_p,
					'qcld_gemini_ai_top_k': qcld_gemini_ai_top_k,
					'qcld_ai_settings_open_ai': qcld_ai_settings_open_ai
				},
				dataType: 'json',
				success: function (response) {
					//console.log(response)

					if (response.status == 'success') {
						alert(response.results);
						location.reload();
					}
				}
			});
		});

		$('body').on('click', '.qcld_seohelp_openrouter_save_settings', function () {
			var qcld_seohelp_openrouter_api_key = $('#qcld_seohelp_openrouter_api_key').val();
			var qcld_seohelp_openrouter_model = $('#qcld_seohelp_openrouter_model').val();
			var manual_models = $('#qcld_seohelp_openrouter_manual_models').val();
			var qcld_seohelp_openrouter_temperature = $('#qcld_seohelp_openrouter_temperature').val();
			var qcld_ai_settings_open_ai = $("input[name=qcld_ai_settings_open_ai]:checked").val();
			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_openrouter_save_settings',
					'security': qcld_seohelp_ajax_nonce,
					'qcld_seohelp_openrouter_api_key': qcld_seohelp_openrouter_api_key,
					'qcld_seohelp_openrouter_model': qcld_seohelp_openrouter_model,
					'manual_models': manual_models,
					'qcld_seohelp_openrouter_temperature': qcld_seohelp_openrouter_temperature,
					'qcld_ai_settings_open_ai': qcld_ai_settings_open_ai
				},
				dataType: 'json',
				success: function (response) {
					if (response.status == 'success') {
						alert(response.results);
						location.reload();
					} else {
						alert(response.results || 'An error occurred while saving.');
					}
				}
			});
		});

		$('body').on('click', '#qcld_seohelp_openrouter_check_connection', function (e) {
			e.preventDefault();
			var api_key = $('#qcld_seohelp_openrouter_api_key').val().trim();
			var status_span = $('#qcld_openrouter_conn_status');
			var modelSelect = $('#qcld_seohelp_openrouter_model');
			if (!api_key) {
				status_span.html('<span style="color: red;">Please enter an API Key first.</span>');
				return;
			}
			status_span.html('<span style="color: blue;">Testing connection...</span>');
			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: {
					'action': 'qcld_seohelp_check_openrouter_connection',
					'security': qcld_seohelp_ajax_nonce,
					'api_key': api_key
				},
				dataType: 'json',
				success: function (response) {
					if (response.success) {
						status_span.html('<span style="color: green;">' + response.data.message + '</span>');
						if (response.data.models && Object.keys(response.data.models).length > 0) {
							var current_val = modelSelect.val();
							modelSelect.empty();
							$.each(response.data.models, function (val, label) {
								modelSelect.append($('<option></option>').attr('value', val).text(label));
							});
							if (current_val && response.data.models[current_val]) {
								modelSelect.val(current_val);
							}
						}
					} else {
						status_span.html('<span style="color: red;">' + (response.data || 'Connection failed.') + '</span>');
					}
				},
				error: function () {
					status_span.html('<span style="color: red;">Ajax request failed.</span>');
				}
			});
		});

		$(document).on('click', '.qcld_seohelp_pro_feature', function () {
			$('.qcld_seohelp_pro_feature_content').toggleClass("qcld_seohelp_pro_feature_active");
		});



		$(document).on('click', '.generate_image', function (e) {
			e.preventDefault();
			var qcld_seohelp_button = $(this);
			var qcld_seohelp_prompt = $('#prompt').val();
			var qcld_seohelp_artist = $('#artist').val();
			var qcld_seohelp_art_style = $('#art_style').val();
			var qcld_seohelp_photography_style = $('#photography_style').val();
			var qcld_seohelp_lighting = $('#lighting').val();
			var qcld_seohelp_subject = $('#subject').val();
			var qcld_seohelp_camera_settings = $('#camera_settings').val();
			var qcld_seohelp_composition = $('#composition').val();
			var qcld_seohelp_resolution = $('#resolution').val();
			var qcld_seohelp_color = $('#color').val();
			var qcld_seohelp_special_effects = $('#special_effects').val();
			var qcld_seohelp_img_size = $('#img_size').val();
			var qcld_seohelp_num_images = $('#num_images').val();
			var num_images = $('#num_images');


			var qcld_article_Steps = [];
			if (qcld_seohelp_prompt) {
				qcld_article_Steps.push('process');
			}
			if (qcld_seohelp_num_images) {
				for (var i = 0; i < qcld_seohelp_num_images; i++) {
					qcld_article_Steps.push('image');
				}
			}

			//console.log(qcld_article_Steps)

			if (qcld_seohelp_prompt === '') {
				alert('Please enter Prompt title');
			}
			else if (!qcld_article_Steps.length) {
				alert('Please choose least one field');
			} else {

				var qcld_seohelp_post_status = $('.qcld_seo-post-status:checked').val();
				var qcld_seohelp_schedule = $('.qcld_seo-schedule-post').val();

				var data = {
					qcld_seohelp_prompt: qcld_seohelp_prompt,
					artist: qcld_seohelp_artist,
					art_style: qcld_seohelp_art_style,
					photography_style: qcld_seohelp_photography_style,
					lighting: qcld_seohelp_lighting,
					subject: qcld_seohelp_subject,
					camera_settings: qcld_seohelp_camera_settings,
					composition: qcld_seohelp_composition,
					resolution: qcld_seohelp_resolution,
					color: qcld_seohelp_color,
					special_effects: qcld_seohelp_special_effects,
					qcld_seohelp_img_size: qcld_seohelp_img_size,
					qcld_seohelp_num_images: qcld_seohelp_num_images,
					security: qcld_seohelp_ajax_nonce,
					action: 'qcld_seohelp_image_generate'
				};

				var data_html = '<div class="qcld_seohelp_ai_generating_wrap">' +
					'<div class="qcld_seohelp_ai_generating_overlay"></div>' +
					'<div class="qcld_seohelp_ai_generating_inner">' +
					'<div class="qcld_seohelp_ai-generating-title">Generating content..</div>' +
					'<div class="qcld_seohelp_ai-generating-process">' +
					'</div>' +
					'<div class="qcld_seohelp_ai-timer">00:00</div>' +
					'</div></div>';

				$('.qcld_seohelp_ai_generating_wrap').remove();
				$('body').append(data_html);

				if (window['qcld_image_timer'] !== undefined) {
					clearTimeout(window['qcld_image_timer']);
				}

				var ai_timer = $('.qcld_seohelp_ai-timer'), seconds = 0, minutes = 0, t;
				function qcld_image_add() {
					seconds++;
					if (seconds >= 60) {
						seconds = 0;
						minutes++;
					}
					if (minutes >= 60) {
						minutes = 0;
						hours++;
					}
					var htmlTimer = (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
					ai_timer.html(htmlTimer);
					qcld_image_timer();

				}

				function qcld_image_timer() {
					window['qcld_image_timer'] = setTimeout(qcld_image_add, 1000);
				}
				qcld_image_timer();

				qcld_image_StepWorking(data, 0, qcld_article_Steps, qcld_seohelp_button);

			}
		});

		function qcld_image_StepWorking(data, step, steps, qcld_seohelp_button) {
			var qcld_next_step = step + 1;
			var qcld_step = steps[step];
			data.step = qcld_step;

			if (data.step == 'process') {
				qcld_image_generatorProcess('process', 'Processing Content');
			}
			if (data.step === 'image') {
				qcld_image_generatorProcess('image', 'Generating Image');
			}
			if (qcld_step == '') {
				qcld_image_generatorProcess('finished', 'Finished');
				$('.qcld_seohelp_ai_generating_wrap').remove();
				return false;
			}



			$.ajax({
				url: qcld_seohelp_ajaxurl,
				data: data,
				type: 'POST',
				dataType: 'JSON',
				beforeSend: function () {
					qcld_seohelp_button.attr('disabled', 'disabled');
					qcld_seohelp_button.find('.spinner').remove();
					qcld_seohelp_button.append('<span class="spinner"></span>');
					qcld_seohelp_button.find('.spinner').css('visibility', 'unset');
				},
				success: function (res) {
					console.log( res )
					if (res.status == 'success') {

						$('.qcld_seohelp_ai-generating-process').find('.qcld_seohelp_ai-generating-process-' + res.oldStep).addClass('finished');
						$('.qcld_seohelp_ai-generating-process').find('.qcld_seohelp_ai-generating-process-' + res.oldStep + ' span').html('Done');
						if (res.html) {
							$('.qcld_image_grid').append(res.html);
						}
						if (qcld_next_step === steps.length) {
							qcld_image_generatorProcess('finished', 'Finished');
							$('.qcld_seohelp_ai_generating_wrap').remove();

							qcld_seohelp_button.removeAttr('disabled');
							qcld_seohelp_button.find('.spinner').remove();
						} else {
							qcld_image_StepWorking(data, qcld_next_step, steps, qcld_seohelp_button);
						}
					} else {
						alert('Something went wrong');
					}
				},
				error: function () {
					qcld_seohelp_button.removeAttr('disabled');
					qcld_seohelp_button.find('.spinner').remove();
					alert('Something went wrong');
					$('.qcld_seohelp_ai_generating_wrap').remove();
				}
			});

		}
		function qcld_image_generatorProcess(step, message) {
			var wpaicg_generator_process = $('.qcld_seohelp_ai-generating-process');
			if (step === 'finished') {
				wpaicg_generator_process.append('<div class="qcld_seohelp_ai-generating-process-' + step + '"><p>' + message + '</p></div>');
			} else {
				wpaicg_generator_process.append('<div class="qcld_seohelp_ai-generating-process-' + step + '"><p>' + message + '</p><span>In Progress..</span></div>');
			}
		}

		$(document).on('click', '.generate_image_download', function (e) {
			e.preventDefault();

			var url = $(this).find("img").attr('src');

			var qcld_seohelp_button = $(this);
			var image_url = $(this).find('img').attr('src');
			var image_page_url = $(this).find('img').attr('src');
			var image_prompt = $(this).find('img').attr('alt');
			var photographer = $(this).find('img').data('photographer');

			var q = '';
			var data = {
				qcld_seohelp_openai_images_upload: "1",
				image_url: image_url,
				image_src_page: image_page_url,
				image_user: photographer,
				q: q,
				image_prompt: image_prompt,
				wpnonce: openai_images_security_nonce,
				security: qcld_seohelp_ajax_nonce,
				action: 'qcld_seohelp_image_generate_url_functions'
			};

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				data: data,
				type: 'POST',
				dataType: 'JSON',
				beforeSend: function () {
					qcld_seohelp_button.removeAttr('disabled').removeClass("uploading").find('.qcld_seohelp_msg').remove();
					qcld_seohelp_button.attr('disabled', 'disabled');
					//qcld_seohelp_button.append('<span class="spinner"></span>');
					//qcld_seohelp_button.find('.spinner').css('visibility','unset');
					qcld_seohelp_button.addClass('uploading').find('.qcld_seohelp_download img').replaceWith('<img src="' + qcld_seohelp_img_url + '/loading.svg" >');
				},
				success: function (res) {

					//qcld_seohelp_button.find('.spinner').remove();
					qcld_seohelp_button.find('.qcld_seohelp_download img').replaceWith('<img src="' + qcld_seohelp_img_url + '/download.svg" ><p class="qcld_seohelp_msg">' + res.html + '<p>');

					//setTimeout(function(){ 
					//qcld_seohelp_button.removeAttr('disabled').removeClass("uploading").find('.qcld_seohelp_msg').remove();
					//}, 3500);

				},
				error: function () {
					qcld_seohelp_button.removeAttr('disabled');
					//qcld_seohelp_button.find('.spinner').remove();
					//alert('Something went wrong');
				}
			})



		});


		$(document).on('click', '.generate_image_text', function (e) {

			var randomIndex = Math.floor(Math.random() * 41);
			document.getElementById("prompt").value = ["A cat playing checkers", "Several cute monsters with ice cream cone bodies", "A forest with a small cabin nestled among the trees", "A cityscape, with tall skyscrapers and a street scene", "A majestic elephant in the African savannah", "A robot with a human-like expression on its face", "An old fashioned steam train", "A musician with a bold graphic style", "A bowl of fruit on a table with a window in the background", "A detailed fantasy castle with turrets and towers", "A dragon breathing fire in a medieval-inspired setting", "A young girl with flowers in her hair", "A science fiction scene with spaceships and planets", "A sunset over a seascape with waves crashing on the shore", "A male figure sitting on a park bench lost in thought", "A group of superheroes in action", "A horse galloping in a field", "A city skyline", "A mermaid sitting on a rock in a fantasy underwater setting", "A still life with a vase of flowers and a bowl of lemons", "A jungle scene with monkeys swinging from vines", "A robot dog with a playful expression", "A group of birds flying over a cityscape", "A fantasy landscape with a floating castle and a dragon", "A sunset over a desert landscape with a camel in the foreground", "A car with a lot of details", "A group of robots playing instruments in a band", "A man sitting in a chair reading a book", "A fantasy dragon with golden scales and red eyes", "A still life with a bowl of fruit on a tablecloth", "A zoo scene with different animals in their enclosures", "A robot with a cat-like design", "A group of birds flying over a forest", "A fantasy landscape with a floating island and a castle", "A sunset over a mountain landscape with a cabin in the foreground", "An airplane with a lot of details", "A group of robots dancing in a club", "A woman sitting on a bench with a book", "A fantasy dragon with blue scales and yellow eyes", "A still life with a vase of flowers on a windowsill", "A robot with a dog-like design"][randomIndex];

		});


		$('.generate_image_text').trigger('click');


		$(document).on('click', '.qcld_form_tab_next', function (event) {
			var cur = $(this).closest("#qcld_tab_area").find(".qcld_form_tab_pan.qcld_tab_active");
			var input_val = $(this).closest("#qcld_tab_area").find(".qcld_form_tab_pan.qcld_tab_active input[type=text]");

			if (jQuery(cur).next().length > 0) {
				$(".qcld_form_tab_prev").removeClass("qcld_form_tab_hide");
				jQuery(".qcld_form_tab_pan").removeClass("qcld_tab_active");
				jQuery(cur).next().addClass("qcld_tab_active");
			}
			if (jQuery(cur).next().next().length == 0) {
				$(".qcld_form_tab_next").addClass("qcld_form_tab_hide");
				$(".qcld_form_submit").removeClass("qcld_form_tab_hide");
			}
		});

		$(document).on('click', '.qcld_form_tab_prev', function (event) {
			var cur = $(this).closest("#qcld_tab_area").find(".qcld_form_tab_pan.qcld_tab_active");
			if (jQuery(cur).prev().length > 0) {
				$(".qcld_form_submit").addClass("qcld_form_tab_hide");
				$(".qcld_form_tab_next").removeClass("qcld_form_tab_hide");
				jQuery(".qcld_form_tab_pan").removeClass("qcld_tab_active");
				jQuery(cur).prev().addClass("qcld_tab_active");
			}
			if (jQuery(cur).prev().prev().length == 0) {
				$(".qcld_form_tab_prev").addClass("qcld_form_tab_hide");
			}
		});

		//$(document).on('click','.qcld_generate_content', function( event ) {
		$(document).on('keyup', '#qcld_seohelp_api_key', function (event) {
			var value = $(this).val();
			$('#qcld_valid_api_key').val('');
			$('#qcld_valid_api_key').val(value);
		});

		$(document).on('click', '.qcld_form_submit', function (event) {
			//event.preventDefault();
			var currentDom = $(this);
			var api_key = $("#qcld_valid_api_key").val() ? $("#qcld_valid_api_key").val() : $("#qcld_seohelp_api_key").val();
			var title = $("#qcld_article_keyword").val();
			var topic = $(".qcld_form_tab_topic_wrap input[type=radio][name=qcld_seohelp_topic]:checked").val();

			currentDom.addClass('qcld_ai_active');
			currentDom.attr('disabled', 'disabled');
			currentDom.append('<span class="spinner"></span>');
			currentDom.find('.spinner').css('visibility', 'unset');
			$(".qcld_form_tab_prev").attr('disabled', 'disabled');
			$(".qcld_form_tab_topic_wrap input[type=radio]").attr('disabled', 'disabled');



			var qcld_article_Steps = [];
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('process');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}
			if (qcld_keyword_suggestion) {
				qcld_article_Steps.push('content');
			}

			var data = {
				'action': 'qcld_seohelp_content_generate',
				'api_key': api_key,
				'title': title,
				'topic': topic,
				'step': '',
				'content': '',
				'security': qcld_seohelp_ajax_nonce
			};

			var data_html = '<div class="qcld_seohelp_ai_generating_wrap">' +
				'<div class="qcld_seohelp_ai_generating_overlay"></div>' +
				'<div class="qcld_seohelp_ai_generating_inner">' +
				'<div class="qcld_seohelp_ai-generating-title">Generating content..</div>' +
				'<div class="qcld_seohelp_ai-generating-process">' +
				'</div>' +
				'<div class="qcld_seohelp_ai-timer">00:00</div>' +
				'</div></div>';

			$('.qcld_seohelp_ai_generating_wrap').remove();
			$('body').append(data_html);

			if (window['qcld_turbo_timer'] !== undefined) {
				clearTimeout(window['qcld_turbo_timer']);
			}

			var ai_timer = $('.qcld_seohelp_ai-timer'), seconds = 0, minutes = 0, t;
			function qcld_turbo_add() {
				seconds++;
				if (seconds >= 60) {
					seconds = 0;
					minutes++;
				}
				if (minutes >= 60) {
					minutes = 0;
					hours++;
				}
				var htmlTimer = (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
				ai_timer.html(htmlTimer);
				qcld_turbo_timer();

			}

			function qcld_turbo_timer() {
				window['qcld_turbo_timer'] = setTimeout(qcld_turbo_add, 1000);
			}
			qcld_turbo_timer();

			QcldTurboStepWorking(data, 0, qcld_article_Steps);

			/*$.post(
		qcld_seohelp_ajaxurl,
		{
		  action    : 'qcld_seohelp_content_generate',
		  security  : qcld_seohelp_ajax_nonce,
		  api_key   : api_key,
		  title     : title,
		  topic     : topic
		},
		function(data){
			  
			  currentDom.removeClass( 'qcld_ai_active' );
			  currentDom.removeAttr('disabled');
			  currentDom.find('.spinner').remove();

			  $(".qcld_form_tab_prev").removeAttr('disabled');
			  $(".qcld_form_tab_topic_wrap input[type=radio]").removeAttr('disabled');


			  if ( data.status == 'success' ) {
				// do something

				$('.qcld_seohelp_ai_single_contents .qcld_seohelp_msg').remove();
				$('.qcld_seohelp_ai_single_contents').append( data.html );
				return false;

			  } else if (data.status == "pending") {
				// do something
				$('.qcld_seohelp_ai_single_contents .qcld_seohelp_msg').remove();
				$('.qcld_seohelp_ai_single_contents').append( data.html );

				setTimeout(() => {
				  $('.qcld_form_submit').trigger('click');

				}, 1000);

				return false;

			  }


		});*/


		});

		function QcldTurboStepWorking(data, step, steps) {
			var qcld_next_step = step + 1;
			var qcld_step = steps[step];
			data.step = qcld_step;


			if (data.step == 'process') {
				qcld_turbo_generatorProcess('process', 'Processing Content');
			}
			if (data.step == 'content') {
				qcld_turbo_generatorProcess('content', 'Generating Content');
			}

			$('.qcld_form_submit').removeClass('qcld_ai_active');
			$('.qcld_form_submit').removeAttr('disabled');
			$('.qcld_form_submit').find('.spinner').remove();

			$(".qcld_form_tab_prev").removeAttr('disabled');
			$(".qcld_form_tab_topic_wrap input[type=radio]").removeAttr('disabled');

			$.ajax({
				url: qcld_seohelp_ajaxurl,
				method: 'POST',
				data: data,
				dataType: 'json',
				success: function (response) {


					$('.qcld_seohelp_ai-generating-process').find('.qcld_seohelp_ai-generating-process-' + response.oldStep).addClass('finished');
					$('.qcld_seohelp_ai-generating-process').find('.qcld_seohelp_ai-generating-process-' + response.oldStep + ' span').html('Done');

					if (response.status == 'success') {
						qcld_turbo_generatorProcess('finished', 'Finished');
						$('.qcld_seohelp_ai_generating_wrap').remove();
						$('.qcld_seohelp_ai_single_contents .qcld_seohelp_msg').remove();
						$('.qcld_seohelp_ai_single_contents').append(response.html);
						$('.qcld_seohelp_ai_single_contents .qcld_seohelp_msg').show();

					} else {
						$('.qcld_seohelp_ai_single_contents .qcld_seohelp_msg').remove();
						$('.qcld_seohelp_ai_single_contents').append(response.html);
						$('.qcld_seohelp_ai_single_contents .qcld_seohelp_msg').hide();
						QcldTurboStepWorking(data, qcld_next_step, steps);
					}

				},
				error: function ($xhr, textStatus, errorThrown) {

					$('#qcld_article_keyword_suggestion').prop("disabled", false);
					$('#qcld_article_keyword_suggestion').removeClass('spinning');
					qcld_turbo_generatorProcess('finished', $xhr.responseText);
					$('.qcld_seohelp_ai_generating_wrap').remove();
					alert($xhr.responseText);
				}

			});

		}
		function qcld_turbo_generatorProcess(step, message) {
			var wpaicg_generator_process = $('.qcld_seohelp_ai-generating-process');
			if (step === 'finished') {
				wpaicg_generator_process.append('<div class="qcld_seohelp_ai-generating-process-' + step + '"><p>' + message + '</p></div>');
			} else {
				wpaicg_generator_process.append('<div class="qcld_seohelp_ai-generating-process-' + step + '"><p>' + message + '</p><span>In Progress..</span></div>');
			}
		}



		if ($('input[type=radio][name="qcld_ai_settings_open_ai"]:checked').val() == 'gemini') {
			$(".qcld_ai_settings_wrap.qcld_open_palm_active").css({ 'display': 'block' });
			$(".qcld_ai_settings_wrap.qcld_open_ai_active").css({ 'display': 'none' });
			$(".qcld_ai_settings_wrap.qcld_openrouter_active").css({ 'display': 'none' });
		} else if ($('input[type=radio][name="qcld_ai_settings_open_ai"]:checked').val() == 'openrouter') {
			$(".qcld_ai_settings_wrap.qcld_open_palm_active").css({ 'display': 'none' });
			$(".qcld_ai_settings_wrap.qcld_open_ai_active").css({ 'display': 'none' });
			$(".qcld_ai_settings_wrap.qcld_openrouter_active").css({ 'display': 'block' });
		} else {
			$(".qcld_ai_settings_wrap.qcld_open_palm_active").css({ 'display': 'none' });
			$(".qcld_ai_settings_wrap.qcld_open_ai_active").css({ 'display': 'block' });
			$(".qcld_ai_settings_wrap.qcld_openrouter_active").css({ 'display': 'none' });
		}
		
		$('input[type=radio][name="qcld_ai_settings_open_ai"]').change(function () {
			if (this.value == 'openai') {
				$(".qcld_ai_settings_wrap.qcld_open_ai_active").css({ 'display': 'block' });
				$(".qcld_ai_settings_wrap.qcld_open_palm_active").css({ 'display': 'none' });
				$(".qcld_ai_settings_wrap.qcld_openrouter_active").css({ 'display': 'none' });
			} else if (this.value == 'openrouter') {
				$(".qcld_ai_settings_wrap.qcld_open_palm_active").css({ 'display': 'none' });
				$(".qcld_ai_settings_wrap.qcld_open_ai_active").css({ 'display': 'none' });
				$(".qcld_ai_settings_wrap.qcld_openrouter_active").css({ 'display': 'block' });
			} else {
				$(".qcld_ai_settings_wrap.qcld_open_palm_active").css({ 'display': 'block' });
				$(".qcld_ai_settings_wrap.qcld_open_ai_active").css({ 'display': 'none' });
				$(".qcld_ai_settings_wrap.qcld_openrouter_active").css({ 'display': 'none' });
			}
		});


		$(document).on('click', '.qcld_subtitle_lang_check', function (e) {
			e.preventDefault();
			var btn = $(this);
			var paramLang = btn.data('value');
			$('#qcld_youtube_language').val(paramLang);
			$('.qcld_subtitle_lang_check').removeClass('qcld_subtitle_lang_active');
			btn.addClass('qcld_subtitle_lang_active');
			$('#qcld_youtube_subtitle_generate').trigger('click');

		});


		$(document).on('click', '#qcld_youtube_subtitle_generate', function (e) {
			var btn = $(this);
			var video_id = $('#qcld_youtube_video_id').val();
			var language = $('#qcld_youtube_language').val();
			if (video_id !== '') {

				$('.qcld_youtube_subtitle_data').hide();
				$('.qcld_seo-playground-buttons').hide();

				$.ajax({
					url: qcld_seohelp_ajaxurl,
					data: { video_id: video_id, language: language, action: 'qcld_youtube_subtitle_generate_by_ajax', security: qcld_seohelp_ajax_nonce },
					dataType: 'json',
					type: 'POST',
					beforeSend: function () {
						btn.attr('disabled', 'disabled');
						//btn.find('.spinner').show();
						btn.append('<span class="spinner"></span>');
						btn.find('.spinner').css('visibility', 'unset');
					},
					success: function (res) {
						btn.removeAttr('disabled');
						btn.find('.spinner').hide();
						//console.log( res)
						if (res.language) {
							$('.qcld_subtitle_language_section').html('');
							$('.qcld_subtitle_language_section').append(res.language);
							$('.qcld_subtitle_language_section').show();
						}

						if (res.status === 'success') {

							$('.qcld_youtube_subtitle_data').show();
							$('.qcld_seo-playground-buttons').show();

							//console.log(res.data);
							// var editor = tinyMCE.get('qcld_youtube_subtitle_result');
							// var basicEditor = true;
							// if ( $('#wp-qcld_youtube_subtitle_result-wrap').hasClass('tmce-active') && editor ) {
							//     basicEditor = false;
							// }

							var qcld_subtitle_Text = res.data;
							qcld_subtitle_Text = qcld_subtitle_Text.replace(/<\/?p(>|$)/g, "");

							$('#qcld_youtube_subtitle_result').val(qcld_subtitle_Text);
							$('.qcld_youtube_subtitle_count').remove();
							var words = $.trim(qcld_subtitle_Text).split(" ");
							$('.qcld_youtube_subtitle_count_wrap').html('<span class="qcld_youtube_subtitle_count">' + words.length + '</span>');

						} else {
							alert(res.msg)
						}

					},
					error: function () {
						btn.removeAttr('disabled');
						btn.find('.spinner').remove();
						$('.qcld_youtube_subtitle_data').hide();
						alert('Something went wrong');
					}
				});

			}
		});


		$(document).on('click', '.qcld_youtube_subtitle_rewrite_copied_icon', function (event) {
			var currentDom = $(this);
			var copy_con = $('#qcld_youtube_subtitle_result').val();

			var editor = tinyMCE.get('qcld_youtube_subtitle_rewrite_result_msg');
			if ($('#wp-qcld_youtube_subtitle_rewrite_result_msg-wrap').hasClass('tmce-active') && editor) {
				var copy_con = editor.getContent();
			}

			$(this).addClass("qcld_copied");

			$(".qcld_youtube_subtitle_rewrite_copied_content").removeClass("d-none");
			setTimeout(() => {
				$(".qcld_youtube_subtitle_rewrite_copied_content").addClass("d-none");
			}, 1500);

			var $temp = $("<input>");
			currentDom.append($temp);
			$temp.val(copy_con).select();
			document.execCommand("copy");
			$temp.remove();

		});

		$(document).on('click', '.qcld_youtube_article_playground_clear', function (e) {


			var editor = tinyMCE.get('qcld_youtube_subtitle_rewrite_result_msg');
			var basicEditor = true;
			if ($('#wp-qcld_youtube_subtitle_rewrite_result_msg-wrap').hasClass('tmce-active') && editor) {
				basicEditor = false;
			}


			if (basicEditor) {

				$('#qcld_youtube_subtitle_rewrite_result_msg').val('');

			} else {

				editor.setContent('');

			}

			$('.qcld_youtube-playground-buttons').hide();
			$('.qcld_youtube_subtitle_rewrite_data').hide();

		});




		$(document).on('keyup focusout', '#qcld_youtube_subtitle_result', function (event) {
			var currentDom = $(this);
			$('.qcld_youtube_subtitle_count').remove();
			var words = $.trim(currentDom.val()).split(" ");
			$('.qcld_youtube_subtitle_count_wrap').html('<span class="qcld_youtube_subtitle_count">' + words.length + '</span>');
		});

		$(document).on('click', '.qcld_seohelp_pro_feature_btn', function (e) {

			$(".qcld_youtube_pro_feature_wrap").toggleClass("qcld_youtube_active");

		});






	});

	$(document).ready(function () {
		$(".modal a").not(".dropdown-toggle").on("click", function () {
			$(".modal").modal("hide");
		});

	});

	$(document).on('click', '.qcld_subtitle-copied-content', function (event) {
		var currentDom = $(this);
		var copy_con = $('#qcld_youtube_subtitle_result').val();

		$(this).addClass("qcld_copied");

		$(".qcld_subtitle_copied-content").removeClass("d-none");
		setTimeout(() => {
			$(".qcld_subtitle_copied-content").addClass("d-none");
		}, 1500);

		var $temp = $("<input>");
		currentDom.append($temp);
		$temp.val(copy_con).select();
		document.execCommand("copy");
		$temp.remove();

	});


	//Make the DIV element draggagle:
	var $wrapper = $("#qcld_seohelp_content_wrap");

	if ($wrapper.length > 0) {
		qcld_dragElement($wrapper);
	}

	function qcld_dragElement($elmnt) {
		var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
		var $handle = $("#content_generator");

		// Choose the handle if it exists, otherwise use the main element
		if ($handle.length > 0) {
			$handle.on("mousedown", qcld_dragMouseDown);
		} else {
			$elmnt.on("mousedown", qcld_dragMouseDown);
		}

		function qcld_dragMouseDown(e) {
			e.preventDefault();
			pos3 = e.clientX;
			pos4 = e.clientY;

			$(document).on("mouseup", qcld_closeDragElement);
			$(document).on("mousemove", qcld_elementDrag);
		}

		function qcld_elementDrag(e) {
			e.preventDefault();
			pos1 = pos3 - e.clientX;
			pos2 = pos4 - e.clientY;
			pos3 = e.clientX;
			pos4 = e.clientY;

			$elmnt.css({
				top: ($elmnt.position().top - pos2) + "px",
				left: ($elmnt.position().left - pos1) + "px"
			});
		}

		function qcld_closeDragElement() {
			$(document).off("mousemove", qcld_elementDrag);
			$(document).off("mouseup", qcld_closeDragElement);
		}

	}


	//for downloading gc-client
	$(document).on('click', '#qcld_seohelp_gc_download', function (e) {
		e.preventDefault();

		$.ajax(
			{
				type: 'POST',
				url: qcld_seohelp_ajaxurl,
				data: {
					'action': 'qcld_wp_seo_gc_client_download',
					'security': qcld_seohelp_ajax_nonce,
				},
				beforeSend: function () {
					$('.qcld_seohelp_download_statuses').append('<p>Downloading...</p>');
				},
				success: function (data) {
					$('.qcld_seohelp_download_statuses').append('<p>' + data.content + '</p>');
					if (data.status == 'success') {

						window.location.reload();

					}

				}
			});

	});



	jQuery(document).ready(function ($) {
		$('#llms-settings-form').on('submit', function (e) {
			e.preventDefault();
			const $btn = $('#llms-generate-btn');
			const $log = $('#llms-log');

			$btn.prop('disabled', true).text('Generating Markdown...');
			$log.prepend('<div>Starting generation...</div>');

			$.post(ajaxurl, {
				action: 'qcld_seohelp_llms_generate_txt',
				data: $(this).serialize(),
				_ajax_nonce: qcld_seohelp_ajax_nonce
			}, function (res) {
				if (res.success) {
					$log.prepend('<div style="color:green;">Success: ' + res.data.file_size + ' bytes written.</div>');
					alert('llms.txt has been generated successfully!');
				} else {
					$log.prepend('<div style="color:red;">Error: ' + res.data + '</div>');
				}
				$btn.prop('disabled', false).text('Save & Generate llms.txt');
			});
		});

		$('#llms-reset-btn').on('click', function () {
			if (confirm('Reset all LLM settings?')) {
				$.post(ajaxurl, { action: 'qcld_seohelp_llms_reset_pro',_ajax_nonce: qcld_seohelp_ajax_nonce }, function () { location.reload(); });
			}
		});

		$(document).on('click', '.qcld_start_crawler', function(e) {
			e.preventDefault();
			var $btn = $(this);
			var id = $btn.data('id');
			var nonce = $btn.data('nonce');
			var originalText = $btn.html();
			
			// Show nice loading effect
			$btn.html('<span class="spinner is-active" style="float:none; margin:0 5px;"></span> Crawling...');
			$btn.css('pointer-events', 'none');

			$.ajax({
				url: ajaxurl,
				type: 'POST',
				data: {
					action: 'qcld_seohelp_start_crawler',
					id: id,
					nonce: nonce
				},
				success: function(response) {
					if (response.success) {
						$btn.html('<span style="color:green;">&#10003; Done! Reloading...</span>');
						setTimeout(function() {
							location.reload();
						}, 1000);
					} else {
						alert('Error: ' + response.data.message);
						$btn.html(originalText);
						$btn.css('pointer-events', 'auto');
					}
				},
				error: function() {
					alert('Something went wrong. Please try again.');
					$btn.html(originalText);
					$btn.css('pointer-events', 'auto');
				}
			});
		});
	});



})(jQuery);


