/**
 * Admin Enqueue Script
 *
 * @package wpsyncsheets-wpforms
 */

(function ($) {
	"use strict";

	$(document).ready(
		function () {
			"use strict";

			$(".wpsswp-element .custom-swap").each(
				function () {
					var firstLabel = $(this).children().first();
					var lastLabel = $(this).children().last()[0];
					firstLabel.before(lastLabel);
				}
			);
			$("#wpforms-save").on(
				"click",
				function (e) {
					if ($('#wpforms-panel-field-wpsyncsheets-wpforms-enable').is(':checked')) {
						var spreadsheetID = $('#wpforms-panel-field-wpsyncsheets-wpforms-spreadsheet_id option:selected').val();
						var sheetName = $('#wpforms-panel-field-wpsyncsheets-wpforms-sheetname').val();
						var sheetSelection = $(
							'input[name="spreadsheetselection"]:checked'
						).val();

						var checkedCheckboxes = $('.sortable-headers-list .headers_chk:checked');

						if (String(sheetSelection) === "existing" && (String(spreadsheetID) === "" || parseInt(spreadsheetID) === 0)) {
							alert("Please select spreadsheet.");
							return false;
						} else if (String(sheetSelection) === "new") {
							var newspreadsheetname = $('#wpforms-panel-field-wpsyncsheets-wpforms-newspreadsheetname').val();
							if (sheetName == '' || newspreadsheetname == '') {
								alert("Spreadsheet Name and Sheet Name can not be blank.");
								return false;
							}
						} else if (sheetName == '') {
							alert("Please enter sheet name.");
							return false;
						} else if (checkedCheckboxes.length === 0) {
						    alert('Please select at least one sheet header to continue.');
						    return false;
						}
					}
				}
			);
			function toggleWpsswpClass() {
				var activeSection = getParameterByName('section');
				var panelSidebarEl = $('.wpforms-panel-sidebar-section-wpsyncsheets-wpforms').closest('.wpforms-panel-sidebar');
				var panelActiveSidebar = panelSidebarEl.find('.wpforms-panel-sidebar-section.active');
				var panelActiveSidebarsection = panelActiveSidebar.data('section');

				if (panelActiveSidebarsection === 'wpsyncsheets-wpforms') {
					$('#wpforms-panel-providers .wpforms-panel-content').addClass('wpforms-panel-content-wpsyncsheets-wpforms-active');
				} else {
					$('#wpforms-panel-providers .wpforms-panel-content').removeClass('wpforms-panel-content-wpsyncsheets-wpforms-active');
				}
			}

			// Run on page load.
			toggleWpsswpClass();

			// Run when any sidebar section is clicked.
			$('#wpforms-panel-providers .wpforms-panel-sidebar-section').on('click', function () {
				// Delay to let the 'active' class switch first.
				setTimeout(toggleWpsswpClass, 30);
			});
		}
	);


	$(document).ready(
		function () {
			"use strict";
			// Select all headers.
			$("#selectall").on(
				"click",
				function () {
					$(".headers_chk").prop('checked', true);
				}
			);
			// Select all headers.
			$("#selectnone").on(
				"click",
				function () {
					$(".headers_chk").prop('checked', false);
				}
			);

			if ($('#wpforms-panel-field-wpsyncsheets-wpforms-enable').prop("checked") === true) {
				$(".generalSetting-section:not(.enable-generalSetting-section)").fadeIn(500);
			} else if ($('#wpforms-panel-field-wpsyncsheets-wpforms-enable').prop("checked") === false) {
				$(".generalSetting-section:not(.enable-generalSetting-section)").fadeOut(500);
			}

			$('#wpforms-panel-field-wpsyncsheets-wpforms-enable').on(
				"click",
				function () {
					if ($(this).prop("checked") === true) {
						$(".generalSetting-section:not(.enable-generalSetting-section)").fadeIn(500);
					} else if ($(this).prop("checked") === false) {
						$(".generalSetting-section:not(.enable-generalSetting-section)").fadeOut(500);
					}
				}
			);

		}
	);

	$(document).ready(
		function () {
			if ($('#wpforms-panel-field-wpsyncsheets-wpforms-enable').prop("checked") == true) {
				$("#wpforms-panel-field-wpsyncsheets-wpforms-enable-wrap").siblings().fadeIn(500);
				var newrequest = $('#wpforms-panel-field-wpsyncsheets-wpforms-spreadsheet_id').val();
				if (newrequest == 'new') {
					$("#wpforms-panel-field-wpsyncsheets-wpforms-newspreadsheetname-wrap").show();
				} else {
					$("#wpforms-panel-field-wpsyncsheets-wpforms-newspreadsheetname-wrap").hide();
				}
			} else if ($('#wpforms-panel-field-wpsyncsheets-wpforms-enable').prop("checked") == false) {
				$("#wpforms-panel-field-wpsyncsheets-wpforms-enable-wrap").siblings().fadeOut(500);
			}
		}
	);
	// Check for existing sheets.
	$(document).ready(
		function () {
			$('#synctr').hide();
			$('#wpsslwp-headers-notice').hide();
			var prevsheetid = $('#woocommerce_spreadsheet').val();
			if (prevsheetid != '') {
				$('#synctr').show();
				$('#wpsslwp-headers-notice').show();
			}
		}
	);
	$(document).ready(
		function () {
			$("#authlink").on(
				"click",
				function (e) {
					$('#authbtn').hide();
					document.getElementById("authtext").style.display = "inline-block";
				}
			);
			$("#revoke").on(
				"click",
				function (e) {
					document.getElementById("authtext").style.display = "none";
					document.getElementById("client_token").style.display = "none";
				}
			);
			$("#reset_settings").on(
				"click",
				function (e) {
					e.preventDefault();
					var wpnonce = jQuery('#wpsslwp_api_settings').val();
					jQuery.ajax(
						{
							url: admin_ajax_object.ajaxurl,
							type: 'post',
							data: "action=wpsslwp_reset_settings&_wpnonce=" + wpnonce,
							beforeSend: function () {
								if (confirm("Are you sure you want to reset settings?")) {
								} else {
									return false;
								}
							},
							success: function (response) {
								response = String(response).trim().replace(/^"(.*)"$/, '$1').toLowerCase();
								if (String(response) === 'successful') {
									location.reload();
								} else {
									alert(response);
								}
							},
							error: function (s) {
								alert('Error');
							}
						}
					);
				}
			);

			$("#clear_spreadsheet").on(
				"click",
				function (e) {
					e.preventDefault();
					var formId = jQuery(this).attr('data-form-id');
					var wpnonce = jQuery('#wpsslwp_general_settings').val();
					jQuery.ajax(
						{
							url: admin_ajax_object.ajaxurl,
							type: 'post',
							data: "action=wpsslwp_clear_sheet&form_id=" + formId + "&_wpnonce=" + wpnonce,
							beforeSend: function () {
								if (confirm("Are you sure?")) {
									jQuery('#clearloader').attr('src', jQuery('#syncloader').attr('src'));
									jQuery('#clearloader').show();
									jQuery('#clear_spreadsheet').hide();
								} else {
									return false;
								}
							},
							success: function (response) {
								if (response === 'successful') {
									alert('Spreadsheet Cleared successfully');
								} else if (response === 'save_setting') {
									alert('Please save settings first and try again latter.');
								} else {
									alert(response);
								}
								jQuery('#clearloader').hide();
								jQuery('#clear_spreadsheet').show();
							},
							error: function (s) {
								alert('Error');
								jQuery('#clearloader').hide();
								jQuery('#clear_spreadsheet').show();
							}
						}
					);
				}
			);
		}
	);

	$(document).ready(
		function () {
			$("#wpsyncsheets-wpforms-provider .custom-swap.span-checkbox-switch input[type='checkbox']").each(
				function () {
					var labelId = jQuery(this).attr('id');
					var labelEl = jQuery('label[for="' + labelId + '"]');
					jQuery(this).appendTo(labelEl);
					jQuery("<span class='checkbox-switch'></span>").appendTo(labelEl);
					labelEl.removeClass('inline');
				}
			);
			// Select all headers.
			jQuery("#selectall").on(
				'click',
				function () {
					jQuery(".wpsslwp_headers input[type='checkbox']").prop('checked', true);
				}
			);
			// Select all headers.
			jQuery("#selectnone").on(
				'click',
				function () {
					jQuery(".wpsslwp_headers input[type='checkbox']").prop('checked', false);
				}
			);
		}
	);
})(jQuery);

jQuery(document).ready(
	function ($) {
		$(".wpsslwp-nav-dashboard").addClass("active");
		if ($('.wpsslwp-nav-googleapi').length > 0) {
			var client_token = "";
			var client_id = $("#client_id").val();
			if ($("#client_token").length > 0) {
				client_token = $("#client_token").val();
			}
			var tokenError = $("#token-error").val();
			var tab = getParameterByName('tab');

			if (client_id == "" || tab === 'wpsslwp-nav-dashboard') {
				$(".wpsslwp-nav-dashboard").addClass("active");
			} else if (client_token == "" || 1 === parseInt(tokenError) || tab === 'wpsslwp-nav-googleapi') {
				wpsslwpNavTab(event, "wpsslwp-nav-googleapi");
				$(".wpsslwp-nav-googleapi").addClass("active");
			} else {
				wpsslwpNavTab(event, "wpsslwp-nav-dashboard");
				$(".wpsslwp-nav-dashboard").addClass("active");
			}
		}
		if ($("#error-message").length > 0) {
			var message = $("#error-message").val();
			$(".alert-messages .container").append(
				'<div class="alert alert-danger fade in alert-dismissible" role="alert"><svg width="32" height="32" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"><defs><style>.cls-1{fill:none;stroke:#721c24;stroke-linecap:round;stroke-linejoin:round;stroke-width:20px;}</style></defs><g data-name="Layer 2" id="Layer_2"><g data-name="E410, Error, Media, media player, multimedia" id="E410_Error_Media_media_player_multimedia"><circle class="cls-1" cx="256" cy="256" r="246"/><line class="cls-1" x1="371.47" x2="140.53" y1="140.53" y2="371.47"/><line class="cls-1" x1="371.47" x2="140.53" y1="371.47" y2="140.53"/></g></g></svg>' +
				message +
				'<a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a></div>'
			);
		}
		if ($("#success-message").length > 0) {
			var message = $("#success-message").val();
			$(".alert-messages .container").append(
				'<div class="alert alert-success fade in alert-dismissible" role="alert"><svg fill="#000000" height="32" width="32" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  viewBox="0 0 52 52" xml:space="preserve"><g><path d="M26,0C11.664,0,0,11.663,0,26s11.664,26,26,26s26-11.663,26-26S40.336,0,26,0z M26,50C12.767,50,2,39.233,2,26S12.767,2,26,2s24,10.767,24,24S39.233,50,26,50z"/><path d="M38.252,15.336l-15.369,17.29l-9.259-7.407c-0.43-0.345-1.061-0.274-1.405,0.156c-0.345,0.432-0.275,1.061,0.156,1.406l10,8C22.559,34.928,22.78,35,23,35c0.276,0,0.551-0.114,0.748-0.336l16-18c0.367-0.412,0.33-1.045-0.083-1.411C39.251,14.885,38.62,14.922,38.252,15.336z"/></g></svg>' +
				message +
				'<a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a></div>'
			);
		}
		if ($("#info-message").length > 0) {
			var message = $("#info-message").val();
			$(".alert-messages .container").append(
				'<div class="alert alert-info fade in alert-dismissible"><svg fill="#000000" width="32" height="32" viewBox="0 0 1920 1920" xmlns="http://www.w3.org/2000/svg"><path d="M960 0c530.193 0 960 429.807 960 960s-429.807 960-960 960S0 1490.193 0 960 429.807 0 960 0Zm0 101.053c-474.384 0-858.947 384.563-858.947 858.947S485.616 1818.947 960 1818.947 1818.947 1434.384 1818.947 960 1434.384 101.053 960 101.053Zm-42.074 626.795c-85.075 39.632-157.432 107.975-229.844 207.898-10.327 14.249-10.744 22.907-.135 30.565 7.458 5.384 11.792 3.662 22.656-7.928 1.453-1.562 1.453-1.562 2.94-3.174 9.391-10.17 16.956-18.8 33.115-37.565 53.392-62.005 79.472-87.526 120.003-110.867 35.075-20.198 65.9 9.485 60.03 47.471-1.647 10.664-4.483 18.534-11.791 35.432-2.907 6.722-4.133 9.646-5.496 13.23-13.173 34.63-24.269 63.518-47.519 123.85l-1.112 2.886c-7.03 18.242-7.03 18.242-14.053 36.48-30.45 79.138-48.927 127.666-67.991 178.988l-1.118 3.008a10180.575 10180.575 0 0 0-10.189 27.469c-21.844 59.238-34.337 97.729-43.838 138.668-1.484 6.37-1.484 6.37-2.988 12.845-5.353 23.158-8.218 38.081-9.82 53.42-2.77 26.522-.543 48.24 7.792 66.493 9.432 20.655 29.697 35.43 52.819 38.786 38.518 5.592 75.683 5.194 107.515-2.048 17.914-4.073 35.638-9.405 53.03-15.942 50.352-18.932 98.861-48.472 145.846-87.52 41.11-34.26 80.008-76 120.788-127.872 3.555-4.492 3.555-4.492 7.098-8.976 12.318-15.707 18.352-25.908 20.605-36.683 2.45-11.698-7.439-23.554-15.343-19.587-3.907 1.96-7.993 6.018-14.22 13.872-4.454 5.715-6.875 8.77-9.298 11.514-9.671 10.95-19.883 22.157-30.947 33.998-18.241 19.513-36.775 38.608-63.656 65.789-13.69 13.844-30.908 25.947-49.42 35.046-29.63 14.559-56.358-3.792-53.148-36.635 2.118-21.681 7.37-44.096 15.224-65.767 17.156-47.367 31.183-85.659 62.216-170.048 13.459-36.6 19.27-52.41 26.528-72.201 21.518-58.652 38.696-105.868 55.04-151.425 20.19-56.275 31.596-98.224 36.877-141.543 3.987-32.673-5.103-63.922-25.834-85.405-22.986-23.816-55.68-34.787-96.399-34.305-45.053.535-97.607 15.256-145.963 37.783Zm308.381-388.422c-80.963-31.5-178.114 22.616-194.382 108.33-11.795 62.124 11.412 115.76 58.78 138.225 93.898 44.531 206.587-26.823 206.592-130.826.005-57.855-24.705-97.718-70.99-115.729Z" fill-rule="evenodd"/></svg>' +
				message +
				'<a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a></div>'
			);
		}

		// Video Tutorial Js
		$('.play-icon').click(function (e) {
			e.preventDefault();
			var videoId = $(this).data('video-id');
			var videoURL = 'https://www.youtube.com/embed/' + videoId + '?autoplay=1';
			$('#youtube-video').attr('src', videoURL);

			// Add flex display before fadeIn
			$('#video-popup').css('display', 'flex').hide().fadeIn();
		});

		$('.close, #video-popup').click(function () {
			$('#youtube-video').attr('src', '');
			$('#video-popup').fadeOut(function () {
				$(this).css('display', 'none'); // optional cleanup
			});
		});

		$('.popup-content').click(function (e) {
			e.stopPropagation();
		});

		//Install and Active Woocommerce Plugin button
		$('#wpsswinstallactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpssw_api_dashboard').val();
			var plugin_slug = 'wpsyncsheets-woocommerce'; // Replace with your plugin slug
			$('#wpsswinstallactivebtn').html('Installing...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpsswinstallactivebtn').html('Activated');
						$('#wpsswinstallactivebtn').toggleClass('activated underline');
						$('#wpsswinstallactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);
		});

		// Active Woocommerce Plugin button
		$('#wpsswactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpssw_api_dashboard').val();
			var plugin_slug = 'wpsyncsheets-woocommerce'; // Replace with your plugin slug
			$('#wpsswactivebtn').html('Activating...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_active=true&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpsswactivebtn').html('Activated');
						$('#wpsswactivebtn').toggleClass('activated underline');
						$('#wpsswactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);
		});

		//Install and Active Elementor Plugin button
		$('#wpsseinstallactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpsse_api_dashboard').val();
			var plugin_slug = 'wpsyncsheets-elementor'; // Replace with your plugin slug
			$('#wpsseinstallactivebtn').html('Installing...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpsseinstallactivebtn').html('Activated');
						$('#wpsseinstallactivebtn').toggleClass('activated underline');
						$('#wpsseinstallactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);
		});

		// Active Elementor Plugin button
		$('#wpsseactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpsse_api_dashboard').val();
			var plugin_slug = 'wpsyncsheets-elementor'; // Replace with your plugin slug
			$('#wpsseactivebtn').html('Activating...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_active=true&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpsseactivebtn').html('Activated');
						$('#wpsseactivebtn').toggleClass('activated underline');
						$('#wpsseactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);
		});

		//Install and Active Contact Form Plugin button
		$('#wpsscinstallactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpssc_api_dashboard').val();
			var plugin_slug = 'contactsheets-lite'; // Replace with your plugin slug
			$('#wpsscinstallactivebtn').html('Installing...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpsscinstallactivebtn').html('Activated');
						$('#wpsscinstallactivebtn').toggleClass('activated underline');
						$('#wpsscinstallactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);
		});

		// Active Contact Form Plugin button
		$('#wpsscactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpssc_api_dashboard').val();
			var plugin_slug = 'contactsheets-lite'; // Replace with your plugin slug
			$('#wpsscactivebtn').html('Activating...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_active=true&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpsscactivebtn').html('Activated');
						$('#wpsscactivebtn').toggleClass('activated underline');
						$('#wpsscactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);
		});

		//Install and Active Gravity Form Plugin button
		$('#wpssginstallactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpssg_api_dashboard').val();
			var plugin_slug = 'wpsyncsheets-gravity-forms'; // Replace with your plugin slug
			$('#wpssginstallactivebtn').html('Installing...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpssginstallactivebtn').html('Activated');
						$('#wpssginstallactivebtn').toggleClass('activated underline');
						$('#wpssginstallactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);

		});

		// Active Gravity Form Plugin button
		$('#wpssgactivebtn').on('click', function () {
			var wpnonce = jQuery('#wpssg_api_dashboard').val();
			var plugin_slug = 'wpsyncsheets-gravity-forms'; // Replace with your plugin slug
			$('#wpssgactivebtn').html('Activating...');
			jQuery.ajax(
				{
					url: admin_ajax_object.ajaxurl,
					type: "post",
					data: "action=install_and_activate_plugin&plugin_active=true&plugin_slug=" + plugin_slug + "&_wpnonce=" + wpnonce,
					success: function (response) {
						$('#wpssgactivebtn').html('Activated');
						$('#wpssgactivebtn').toggleClass('activated underline');
						$('#wpssgactivebtn').attr('id', '');
					},
					error: function (s) {
						alert("Error");
					},
				}
			);
		});

	}
);

function wpsslwpCopy(id, targetid) {
	var copyText = document.getElementById(id);
	var textArea = document.createElement("textarea");
	textArea.value = copyText.textContent;
	document.body.appendChild(textArea);
	textArea.select();
	document.execCommand("Copy");
	textArea.remove();
	if (!jQuery("#" + targetid).hasClass("tooltip-click")) {
		jQuery("#" + targetid).addClass("tooltip-click");
		setTimeout(
			function () {
				jQuery("#" + targetid).removeClass("tooltip-click");
			},
			1000
		);
	}
}

function wpsslwptab(evt, tabName) {
	var i, tabContent, tabLinks;
	tabContent = document.getElementsByClassName("tabcontent");
	tabcontentlen = tabContent.length;
	for (i = 0; i < tabcontentlen; i++) {
		tabContent[i].style.display = "none";
	}
	tabLinks = document.getElementsByClassName("tablinks");
	tablinklen = tabLinks.length;
	for (i = 0; i < tablinklen; i++) {
		tabLinks[i].className = tabLinks[i].className.replace(" active", "");
	}
	document.getElementById(tabName).style.display = "block";
	var type = typeof event;
	if (type !== 'undefined') {
		evt.currentTarget.className += " active";
	}
}

function wpsslwpNavTab(evt, tabName) {
	"use strict";
	var i, tabContent, tabLinks;
	tabContent = document.getElementsByClassName("navtabcontent");
	var tabContentlength = tabContent.length;
	for (i = 0; i < tabContentlength; i++) {
		// Skip currently visible tab.
		if (tabContent[i].id === tabName && tabContent[i].style.display === "block") {
			continue;
		}
		tabContent[i].style.display = "none";
	}
	tabLinks = document.getElementsByClassName("navtablinks");
	var tablength = tabLinks.length;
	for (i = 0; i < tablength; i++) {
		// Skip currently active link.
		if (
			typeof evt !== "undefined" &&
			tabLinks[i] === evt.currentTarget &&
			tabLinks[i].className.indexOf(" active") !== -1
		) {
			continue;
		}
		tabLinks[i].className = tabLinks[i].className.replace(" active", "");
	}
	document.getElementById(tabName).style.display = "block";
	var type = typeof event;
	if (type !== "undefined") {
		evt.currentTarget.className += " active";
	}
}

function wpsslwpNavTabfreevspro(evt, tabName) {
	"use strict";
	var i, tabContent, tabLinks;
	tabContent = document.getElementsByClassName("navtabcontent");
	var tabContentlength = tabContent.length;
	for (i = 0; i < tabContentlength; i++) {
		tabContent[i].style.display = "none";
	}
	tabLinks = document.getElementsByClassName("navtablinks");
	var tablength = tabLinks.length;
	for (i = 0; i < tablength; i++) {
		tabLinks[i].className = tabLinks[i].className.replace(" active", "");
		if (tabLinks[i].classList.contains('wpsslwp-nav-freevspro')) {
			tabLinks[i].className += " active";
		}
	}
	document.getElementById(tabName).style.display = "block";
	var type = typeof evt;
	if (type !== "undefined") {
		evt.currentTarget.className += " active";
	}
	window.scrollTo({
		top: 0,
		behavior: 'smooth' // Optional for smooth animation
	});
}

function wpsslwpNavTabLetsBegin(evt, tabName) {
	"use strict";
	var i, tabContent, tabLinks;
	tabContent = document.getElementsByClassName("navtabcontent");
	var tabContentlength = tabContent.length;
	for (i = 0; i < tabContentlength; i++) {
		tabContent[i].style.display = "none";
	}
	tabLinks = document.getElementsByClassName("navtablinks");
	var tablength = tabLinks.length;
	for (i = 0; i < tablength; i++) {
		tabLinks[i].className = tabLinks[i].className.replace(" active", "");
		if (tabLinks[i].classList.contains('wpsslwp-nav-googleapi')) {
			tabLinks[i].className += " active";
		}
	}
	document.getElementById(tabName).style.display = "block";
	var type = typeof evt;
	if (type !== "undefined") {
		evt.currentTarget.className += " active";
	}
	window.scrollTo({
		top: 0,
		behavior: 'smooth' // Optional for smooth animation
	});
}

function getParameterByName(name, url) {
	if (!url) {
		url = window.location.href;
	}
	name = name.replace(/[\[\]]/g, '\\$&');
	var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
		results = regex.exec(url);
	if (!results) {
		return null;
	}
	if (!results[2]) {
		return '';
	}
	return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

function wpsslwp_copy(id, targetid) {
	var copyText = document.getElementById(id);
	var textArea = document.createElement("textarea");
	textArea.value = copyText.textContent;
	document.body.appendChild(textArea);
	textArea.select();
	document.execCommand("Copy");
	textArea.remove();
	if (!jQuery("#" + targetid).hasClass("tooltip-click")) {
		jQuery("#" + targetid).addClass("tooltip-click");
		setTimeout(
			function () {
				jQuery("#" + targetid).removeClass("tooltip-click");
			},
			1000
		);
	}
}

(function ($) {
	"use strict";
	$(document).ready(
		function () {
			"use strict";
			var spreadsheet_id = $("#wpforms-panel-field-wpsyncsheets-wpforms-spreadsheet_id").val();
			var sheetname = $("#wpforms-panel-field-wpsyncsheets-wpforms-sheetname").val();

			if (spreadsheet_id != "new" && spreadsheet_id != "" && spreadsheet_id != 0) {
				var formId = $('#wpforms-builder-form').data('id');
				var slink =
					'<div class="sheet-btns"> <a id="view_spreadsheet" target="_blank" href="https://docs.google.com/spreadsheets/d/' +
					spreadsheet_id +
					'" class="wpss-button wpss-tooltio-link view_spreadsheet"><svg width="19" height="14" viewBox="0 0 19 14" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M9.49967 10.8334C10.5413 10.8334 11.4268 10.4688 12.1559 9.73962C12.8851 9.01046 13.2497 8.12504 13.2497 7.08337C13.2497 6.04171 12.8851 5.15629 12.1559 4.42712C11.4268 3.69796 10.5413 3.33337 9.49967 3.33337C8.45801 3.33337 7.57259 3.69796 6.84342 4.42712C6.11426 5.15629 5.74967 6.04171 5.74967 7.08337C5.74967 8.12504 6.11426 9.01046 6.84342 9.73962C7.57259 10.4688 8.45801 10.8334 9.49967 10.8334ZM9.49967 9.33337C8.87467 9.33337 8.34343 9.11462 7.90593 8.67712C7.46843 8.23962 7.24967 7.70837 7.24967 7.08337C7.24967 6.45837 7.46843 5.92712 7.90593 5.48962C8.34343 5.05212 8.87467 4.83337 9.49967 4.83337C10.1247 4.83337 10.6559 5.05212 11.0934 5.48962C11.5309 5.92712 11.7497 6.45837 11.7497 7.08337C11.7497 7.70837 11.5309 8.23962 11.0934 8.67712C10.6559 9.11462 10.1247 9.33337 9.49967 9.33337ZM9.49967 13.3334C7.4719 13.3334 5.62467 12.7674 3.95801 11.6355C2.29134 10.5035 1.08301 8.98615 0.333008 7.08337C1.08301 5.1806 2.29134 3.66324 3.95801 2.53129C5.62467 1.39935 7.4719 0.833374 9.49967 0.833374C11.5275 0.833374 13.3747 1.39935 15.0413 2.53129C16.708 3.66324 17.9163 5.1806 18.6663 7.08337C17.9163 8.98615 16.708 10.5035 15.0413 11.6355C13.3747 12.7674 11.5275 13.3334 9.49967 13.3334ZM9.49967 11.6667C11.0691 11.6667 12.5101 11.2535 13.8226 10.4271C15.1351 9.60073 16.1386 8.48615 16.833 7.08337C16.1386 5.6806 15.1351 4.56601 13.8226 3.73962C12.5101 2.91323 11.0691 2.50004 9.49967 2.50004C7.93023 2.50004 6.48926 2.91323 5.17676 3.73962C3.86426 4.56601 2.86079 5.6806 2.16634 7.08337C2.86079 8.48615 3.86426 9.60073 5.17676 10.4271C6.48926 11.2535 7.93023 11.6667 9.49967 11.6667Z" fill="#383E46"/> </svg><span class="tooltip-text">View Spreadsheet</span></a>';
				if (sheetname != "") {
					slink = slink + '<a id="clear_spreadsheet" href="javascript:void(0)" class="wpss-button wpss-tooltio-link wpsyncsheets-wpforms-clear-button" data-form-id="' + formId + '"><svg width="19" height="19" viewBox="0 0 19 19" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M8.66674 8.66671H10.3334V2.83337C10.3334 2.59726 10.2535 2.39935 10.0938 2.23962C9.9341 2.0799 9.73619 2.00004 9.50008 2.00004C9.26397 2.00004 9.06605 2.0799 8.90633 2.23962C8.7466 2.39935 8.66674 2.59726 8.66674 2.83337V8.66671ZM3.66674 12H15.3334V10.3334H3.66674V12ZM2.45841 17H4.50008V15.3334C4.50008 15.0973 4.57994 14.8993 4.73966 14.7396C4.89938 14.5799 5.0973 14.5 5.33341 14.5C5.56952 14.5 5.76744 14.5799 5.92716 14.7396C6.08688 14.8993 6.16674 15.0973 6.16674 15.3334V17H8.66674V15.3334C8.66674 15.0973 8.7466 14.8993 8.90633 14.7396C9.06605 14.5799 9.26397 14.5 9.50008 14.5C9.73619 14.5 9.9341 14.5799 10.0938 14.7396C10.2535 14.8993 10.3334 15.0973 10.3334 15.3334V17H12.8334V15.3334C12.8334 15.0973 12.9133 14.8993 13.073 14.7396C13.2327 14.5799 13.4306 14.5 13.6667 14.5C13.9029 14.5 14.1008 14.5799 14.2605 14.7396C14.4202 14.8993 14.5001 15.0973 14.5001 15.3334V17H16.5417L15.7084 13.6667H3.29174L2.45841 17ZM16.5417 18.6667H2.45841C1.91674 18.6667 1.47924 18.4514 1.14591 18.0209C0.812577 17.5903 0.715354 17.1112 0.854243 16.5834L2.00008 12V10.3334C2.00008 9.87504 2.16327 9.48268 2.48966 9.15629C2.81605 8.8299 3.20841 8.66671 3.66674 8.66671H7.00008V2.83337C7.00008 2.13893 7.24313 1.54865 7.72924 1.06254C8.21535 0.57643 8.80563 0.333374 9.50008 0.333374C10.1945 0.333374 10.7848 0.57643 11.2709 1.06254C11.757 1.54865 12.0001 2.13893 12.0001 2.83337V8.66671H15.3334C15.7917 8.66671 16.1841 8.8299 16.5105 9.15629C16.8369 9.48268 17.0001 9.87504 17.0001 10.3334V12L18.1459 16.5834C18.3265 17.1112 18.2466 17.5903 17.9063 18.0209C17.566 18.4514 17.1112 18.6667 16.5417 18.6667Z" fill="#383E46"/> </svg><span class="tooltip-text">Clear Spreadsheet</span> </a><img src="images/spinner.gif" id="clearloader">';
				}
				slink = slink + '<a id="down_spreadsheet" target="_blank" href="https://docs.google.com/spreadsheets/d/' +
					spreadsheet_id +
					'/export?format=xlsx" class="wpss-button wpss-tooltio-link down_spreadsheet"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><path fill="none" stroke="#383E46" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 3.33c2.98 1.72 5 4.96 5 8.66 0 5.52-4.48 10-10 10 -5.53 0-10-4.48-10-10 0-3.71 2.01-6.94 5-8.67m1 8.66l4 4m0 0l4-4m-4 4v-14"/></svg><span class="tooltip-text">Download Spreadsheet</span></a> </div>';
				$("#wpforms-panel-field-wpsyncsheets-wpforms-spreadsheet_id").after(slink);
			}
			$('input[name="spreadsheetselection"]').on(
				"change",
				function () {
					var newRequest = $(this).val();
					if (String(newRequest) === "new") {
						$("#wpforms-panel-field-wpsyncsheets-wpforms-is_createanew_spreadsheet").prop('checked', true);
						$("#wpforms-panel-field-wpsyncsheets-wpforms-is_createanew_spreadsheet").addClass('checked');
						$("#wpforms-panel-field-wpsyncsheets-wpforms-newspreadsheetname-wrap").show();
						$(".synctr").hide();
						$("#wpsyncsheets-wpforms_spreadsheet_container").hide();
						$("#view_spreadsheet").hide();
						$("#clear_spreadsheet").hide();
						$("#down_spreadsheet").hide();
					} else {
						$("#wpforms-panel-field-wpsyncsheets-wpforms-is_createanew_spreadsheet").prop('checked', false);
						$("#wpforms-panel-field-wpsyncsheets-wpforms-is_createanew_spreadsheet").removeClass('checked');
						$("#wpforms-panel-field-wpsyncsheets-wpforms-newspreadsheetname-wrap").hide();
						$("#wpsyncsheets-wpforms_spreadsheet_container").show();
						$("#view_spreadsheet").show();
						$("#clear_spreadsheet").show();
						$("#down_spreadsheet").show();
						if (String(newRequest) === "" || parseInt(newRequest) === 0) {
							$(".synctr").hide();
						} else {
							$(".synctr").show();

						}
					}
				}
			);
			let previousValue = $('#wpforms-panel-field-wpsyncsheets-wpforms-spreadsheet_id').val();

			$('#wpforms-panel-field-wpsyncsheets-wpforms-spreadsheet_id').on('focus', function () {
				previousValue = $(this).val(); // Save current value on focus.
			});


			$('#wpforms-panel-field-wpsyncsheets-wpforms-spreadsheet_id').on('change', function () {
				const selectedValue = $(this).val();

				if (selectedValue === 'new') {
					const $radio = $('input[name="spreadsheetselection"][value="new"]');
					// Set it to checked if not already.
					if (!$radio.prop('checked')) {
						$radio.prop('checked', true).trigger('change');
					} else {
						// If already checked, still trigger change in case other logic depends on it.
						$radio.trigger('change');
					}
					// Revert the select back to previous value.
					$(this).val(previousValue).trigger('change.select2');

				} else {
					previousValue = selectedValue;
				}
			});

			$("#wpsyncsheets-wpforms-provider .headers_chk").each(function () {
				var headerInput = jQuery(this); // the input
				var customSwap = headerInput.closest('.custom-swap');
				var siblingLabel = customSwap.siblings('label');
				var sheetRight = siblingLabel.find('.sheet-right');
				var targetLabel = customSwap.find('label.inline');

				// 1. Clear text inside the inline label.
				targetLabel.empty();

				// 2. Apply important styles.
				if (targetLabel.length) {
					targetLabel[0].style.setProperty('display', 'flex', 'important');
					targetLabel[0].style.setProperty('margin', '0', 'important');
				}


				// 3. Move siblingLabel's inner contents to label.inline.
				siblingLabel.contents().appendTo(targetLabel);

				// 4. Prepend input to .sheet-right.
				sheetRight.prepend(headerInput);

				// 5. Hide the original label.
				siblingLabel.hide();
			});

		}
	);
})(jQuery);

function wpsslwp_wc_closeTopHeader() {
	const header = document.querySelector('.wpsslwp_order_top_header');
	if (header) {
		header.style.display = 'none';
	}
}