/**
 * Admin Enqueue Script
 *
 * @package contactsheets-lite
 */

jQuery( document ).ready(
	function () {

		/**
		* Verify the api code
		 *
		* @since 1.0
		*/
		jQuery( document ).on(
			'click',
			'#save-gs-code',
			function () {
				jQuery( ".loading-sign" ).addClass( "loading" );
				var data = {
					action: 'verify_gs_integation',
					code: jQuery( '#gs-code' ).val(),
					security: jQuery( '#gs-ajax-nonce' ).val()
				};
				jQuery.post(
					ajaxurl,
					data,
					function (response ) {
						if ( ! response.success ) {
							jQuery( ".loading-sign" ).removeClass( "loading" );
							jQuery( "#gs-validation-message" ).empty();
							jQuery( "<span class='error-message'>Access code Can't be blank</span>" ).appendTo( '#gs-validation-message' );
						} else {
							jQuery( ".loading-sign" ).removeClass( "loading" );
							jQuery( "#gs-validation-message" ).empty();
							jQuery( "<span class='gs-valid-message'>Access Code Saved. But do check the debug log for invalid access code.</span>" ).appendTo( '#gs-validation-message' );
						}
					}
				);

			}
		);
		jQuery( document ).on(
			'click',
			'#setheaders',
			function () {
				jQuery( ".loading-sign-header" ).addClass( "loading" );
				var data = {
					action: 'set_headers_func'
				};
				jQuery.post(
					ajax_login_object.ajaxurl,
					data,
					function (response ) {
						if ( ! response.success ) {
							jQuery( ".loading-sign-header" ).removeClass( "loading" );
							jQuery( "#gs-validation-message" ).empty();
							jQuery( "<span class='error-message'>Access code Can't be blank</span>" ).appendTo( '#gs-validation-message' );
						} else {
							jQuery( "loading-sign-header" ).removeClass( "loading" );
							jQuery( "#gs-validation-message" ).empty();
							jQuery( "<span class='gs-valid-message'>Access Code Saved. But do check the debug log for invalid access code.</span>" ).appendTo( '#gs-validation-message' );
						}
					}
				);
			}
		);
		jQuery( document ).on(
			'click',
			"#clear_spreadsheet",
			function (e) {
				e.preventDefault();

				var formId = jQuery( this ).attr( 'data-form-id' );

				jQuery.ajax(
					{
						url : ajax_login_object.ajaxurl,
						type : 'post',
						data :"action=wpsslc_clear_sheet&form_id=" + formId + "&wpnonce=" + jQuery( '#wpsslc_sheet_settings' ).val(),
						beforeSend:function () {
							if (confirm( "Are you sure?" )) {
								jQuery( '#clearloader' ).attr( 'src',jQuery( '#syncloader' ).attr( 'src' ) );
								jQuery( '#clearloader' ).show();
							} else {
								return false;
							}
						},
						success : function ( response ) {
							if (response == 'successful') {
								alert( 'Spreadsheet Cleared successfully' );
								jQuery( '#clearloader' ).hide();
							} else {
								alert( response );
								jQuery( '#clearloader' ).hide();
							}
						},
						error: function (s) {
							alert( 'Error' );
							jQuery( '#clearloader' ).hide();
						}
					}
				);
			}
		);

		jQuery( document ).on(
			"change",
			".wpsslc_active",
			function () {
				if (jQuery( this ).prop( "checked" ) == false) {
					var i = window.confirm(
						"Are you sure? you want to inactive map fields settings which maps sheet headers and fields headers, then the data which belongs to that column will deleted completely."
					);
					if (i == true) {
						jQuery( this ).prop( "checked", false );
					} else {
						jQuery( this ).prop( "checked", true );
					}
				}
			}
		);
		// Select all headers.
		jQuery( "#selectall" ).on(
			'click',
			function () {
				jQuery( ".wpsslc_active" ).prop( 'checked', true );
			}
		);
		// Select all headers.
		jQuery( "#selectnone" ).on(
			'click',
			function () {
				jQuery( ".wpsslc_active" ).prop( 'checked', false );
			}
		);
		if ( jQuery( '#wpsslc_all_settings' ).is( ':checked' ) ) {
			jQuery( '.all_settings_div' ).show();
		} else {
			jQuery( '.all_settings_div' ).hide();
		}
		jQuery( document ).on(
			'change',
			'#wpsslc_all_settings',
			function () {
				if (jQuery( this ).is( ':checked' ) == false) {
					jQuery( ".all_settings_div" ).fadeOut();
				} else {
					jQuery( ".all_settings_div" ).fadeIn();
				}
			}
		);

	}
);
jQuery( document ).ready(
	function () {
		jQuery( "#authlink" ).click(
			function (e) {
				jQuery( '#authbtn' ).hide();
				document.getElementById( "authtext" ).style.display = "block";
			}
		);
		jQuery( "#revoke" ).click(
			function (e) {
				document.getElementById( "authtext" ).style.display     = "none";
				document.getElementById( "client_token" ).style.display = "none";
			}
		);
		jQuery( "#reset_settings" ).on(
			"click",
			function (e) {
				e.preventDefault();
				var wpnonce = jQuery( '#wpsslc_api_settings' ).val();
				jQuery.ajax(
					{
						url : ajax_login_object.ajaxurl,
						type : 'post',
						data :"action=wpsslc_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') {
								window.location.href = ajax_login_object.adminSettingsPageUrl;
							} else {
								alert( response );
							}
						},
						error: function (s) {
							alert( 'Error' );
						}
					}
				);
			}
		);

	}
);
jQuery( document ).ready(
	function ($) {
		/* 
		 * Helpers
		 *  
		 */

		function getSpreadsheetState() {
			return {
				radio: $('input[name="spreadsheetselection"]:checked').val(),
				spreadsheet: $('#spreadsheetid').val(),
				sheet: $('#sheetname').val(),
				newSpreadsheetName: $('#new_spreadsheetname').val().trim(),
				newSheetName: $('#new_sheetname').val().trim()
			};
		}

		function updateSpreadsheetUI() {

			if($('#wpsslcform_spreadsheet_container').length === 0){ 
				return;
			}
			const state = getSpreadsheetState();

			$('#new_sheetname').removeClass('wpsslc-new-sheet-spacing');

			$('#wpsslcform_spreadsheet_container').hide();
			$('.custom-sheet').hide();
			$('.custom-new-sheet-div').hide();
			$('.custom-new-sheet').hide();
			$('.new_sheetname').hide();

			/* 
			 * RADIO = NEW
			 *  
			 */
			if ( state.radio === 'new' ) {

				$('.custom-new-sheet-div').show();
				$('.custom-new-sheet').show();
				$('.new_sheetname').show();
				return;
			}

			/* 
			 * RADIO = EXISTING
			 *  
			 */

			// Spreadsheet dropdown always visible.
			$('#wpsslcform_spreadsheet_container').show();

			// Spreadsheet empty.
			if ( ! state.spreadsheet ) {
				$('.custom-sheet').show();
				return;
			}

			// Spreadsheet = NEW.
			if ( state.spreadsheet === 'new' ) {
				$('.custom-new-sheet-div').show();
				$('.custom-new-sheet').show();
				$('.new_sheetname').show();
				return;
			}

			// Spreadsheet = EXISTING.
			$('.custom-sheet').show();

			// Sheet = NEW.
			if ( state.sheet === 'new' ) {
				$('.custom-new-sheet-div').show();
				$('.custom-new-sheet').hide();
				$('.new_sheetname').addClass('wpsslc-new-sheet-spacing');
				$('.new_sheetname').show();
			}
		}

		/* 
		 * Initial Load
		 *  
		 */

		updateSpreadsheetUI();

		jQuery( 'input[name="spreadsheetselection"]' ).on(
			"change",
			function () {
				$('.wpsslc_error_setting').text('');
				var newRequest = jQuery( this ).val();
				$('#new_spreadsheetname, #new_sheetname').val('');
				updateSpreadsheetUI();				
			}
		);

		$('#spreadsheetid').on('change', function () {

			const val = this.value;

			$('.wpsslc_error_setting').text('');
			$('#new_spreadsheetname, #new_sheetname').val('');

			if ( val === 'new' ) {
				$('input[name="spreadsheetselection"][value="new"]')
					.prop('checked', true);
			}

			if ( val && val !== 'new' ) {
				wpsslcGetSheetsList( val );
			}

			updateSpreadsheetUI();
		});


		$('#sheetname').on('change', updateSpreadsheetUI);

		jQuery( '#wpcf7-admin-form-element' ).on(
			'submit',
			function () {

				if ( ! $("#wpsslc_all_settings").is(":checked") ) {
					return true;
				}

				const state = getSpreadsheetState();

				const stopSubmit = ( msg, selector ) => {
					alert( msg );
					$("#publishing-action .spinner").hide();

					const $tab = $("#google_sheets-tab");

			        // If the tab is not active, open it.
			        if (!$tab.hasClass("active")) {
			            $tab.trigger("click");
			        }

			        // Give it a tiny delay so the panel has time to open before scrolling.
			        setTimeout(() => {
			            $('html, body').animate({
			                scrollTop: $(selector).offset().top - 20
			            }, 400);
			        }, 100);

					return false;
				};

				$("#publishing-action .spinner").show();

				/* 
				 * RADIO = NEW
				 *  
			 	 */
				if ( state.radio === 'new' ) {

					if ( ! state.newSpreadsheetName || ! state.newSheetName ) {
						return stopSubmit(
							"Spreadsheet name and Sheet name can not be blank",
							".wpsslcform_spreadsheet_row.googleSpreadsheet-section"
						);
					}

					return true;
				}

				/* 
				 * RADIO = EXISTING
				 *  
				 */

				if ( ! state.spreadsheet ) {
					return stopSubmit(
						"Please select spreadsheet",
						".wpsslcform_spreadsheet_row.googleSpreadsheet-section"
					);
				}

				if ( state.spreadsheet === 'new' ) {

					if ( ! state.newSpreadsheetName || ! state.newSheetName ) {
						return stopSubmit(
							"Spreadsheet name and Sheet name can not be blank",
							".wpsslcform_spreadsheet_row.googleSpreadsheet-section"
						);
					}

					return true;
				}

				if ( ! state.sheet ) {
					return stopSubmit(
						"Please select sheet or create a new sheet.",
						".wpsslcform_spreadsheet_row.googleSpreadsheet-section"
					);
				}

				if ( state.sheet === 'new' && ! state.newSheetName ) {
					return stopSubmit(
						"Please enter sheet name",
						".wpsslcform_spreadsheet_row.googleSpreadsheet-section"
					);
				}

				/* 
				 * Headers validation
				 *  
			 	 */
				if (
					$("#sortable").length &&
					$('#sortable input[type="checkbox"]:checked').length < 1
				) {
					return stopSubmit(
						"Please select at least one sheet headers to get it work in spreadsheet",
						".wpsslcform_spreadsheet_row.sheetHeaders-section"
					);
				}

				if (state.radio === 'new') {
			        $("#spreadsheetid").val('new');
			    }

				return true;
			}
		);
		var prohtml = jQuery( '#informationdivwpsslc' ).wrap( '<p/>' ).parent().html();
		jQuery( '#informationdivwpsslc' ).remove();
		jQuery( '#informationdiv' ).before( prohtml );

	}
);
jQuery( document ).ready(
	function ($) {
		if($('.wpsslc-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 urlParams = new URLSearchParams(window.location.search);
	        var view      = urlParams.get('view');
	        var form_id   = urlParams.get('form_id');
	        var page   	  = urlParams.get('page');
	        var paged   	  = urlParams.get('paged');
	        var s   = urlParams.get('s');

	        var tab   = urlParams.get('tab');
		       
			if ( (view === "entries" && form_id !== null) || (view === "forms" || view === "entry" ) || (page === "wpsyncsheets-for-contact-form-7" && paged !== null) ) {

	            wpsslcNavTab(event, "wpssc-nav-forms");
	            $(".wpssc-nav-forms").addClass("active");

	        }else if(client_id == "" || tab === 'wpsslc-nav-dashboard') {
				$( ".wpsslc-nav-dashboard" ).addClass( "active" );
		    } else if (client_token == "" || 1 === parseInt( tokenError ) || tab === 'wpsslc-nav-googleapi') {
				wpsslcNavTab( event, "wpsslc-nav-googleapi" );
				$( ".wpsslc-nav-googleapi" ).addClass( "active" );
			}else {
				wpsslcNavTab( event, "wpsslc-nav-dashboard" );
				$( ".wpsslc-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: ajax_login_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: ajax_login_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: ajax_login_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: ajax_login_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 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: ajax_login_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: ajax_login_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" );
					},
				}
			);
		});

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

		});

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

		$(document).on('change', '.wpssc-toggle-status', function () {

	        let checkbox = $(this);
	        let form_id  = checkbox.data('id');
	        let status   = checkbox.is(':checked') ? 'yes' : 'no';

	        $.post(ajax_login_object.ajaxurl, {
	            action: 'wpssc_toggle_form_status',
	            nonce: ajax_login_object.nonce,
	            form_id: form_id,
	            status: status
	        }).fail(function () {
	            alert('Error updating status.');
	            checkbox.prop('checked', !checkbox.is(':checked'));
	        });

	    });

	    function toggleDbSection() {
	        if ($('#wpssc_enable_db_save').is(':checked')) {
	            $('#wpsscsynctr').slideDown(200);
	        } else {
	            $('#wpsscsynctr').slideUp(200);
	        }
	    }

	    // Run on page load
	    toggleDbSection();

	    // Run when checkbox changes
	    $('#wpssc_enable_db_save').on('change', function () {
	        toggleDbSection();
	    });


	    $(document).on('change', '.wpssc-toggle-db-status', function () {

	        let checkbox = $(this);
	        let form_id  = checkbox.data('id');
	        let status   = checkbox.is(':checked') ? 'yes' : 'no';

	        updateStatusUI(checkbox);

	        $.post(ajax_login_object.ajaxurl, {
	            action: 'wpssc_toggle_db_status',
	            nonce: ajax_login_object.nonce,
	            form_id: form_id,
	            status: status
	        }).fail(function () {
	            alert('Error updating status.');
	            checkbox.prop('checked', !checkbox.is(':checked'));

	            updateStatusUI(checkbox);
	        });

	    });

	    function updateStatusUI(checkbox) {
		    let label  = checkbox.closest('.wpssc-switch');
		    let slider = label.find('.wpssc-slider');

		    if (checkbox.is(':checked')) {
		        label.removeClass('wpssc-toggle-db-status-inactive')
		             .addClass('wpssc-toggle-db-status-active');
		        slider.text('Active');
		    } else {
		        label.removeClass('wpssc-toggle-db-status-active')
		             .addClass('wpssc-toggle-db-status-inactive');
		        slider.text('Inactive');
		    }
		}
	}

);
jQuery( document ).ready(
	function ($) {
		"use strict";
		var formActiveTab = getParameterByName( "active-tab" );
		if('google-sheets' === formActiveTab && jQuery('#contact-form-editor').length > 0){
			if(jQuery('#contact-form-editor #contact-form-editor-tabs #google-sheets-tab').length > 0){
				jQuery('#contact-form-editor #contact-form-editor-tabs li').removeClass('active').attr('aria-selected', 'false');
				jQuery('#contact-form-editor #contact-form-editor-tabs button').removeClass('active').attr('aria-selected', 'false');
				jQuery('#contact-form-editor #contact-form-editor-tabs #google-sheets-tab').addClass('active').attr('aria-selected', 'true');
				jQuery('#contact-form-editor .contact-form-editor-panel').attr('hidden', 'hidden');
				jQuery('#contact-form-editor .contact-form-editor-panel').removeClass('active');
				jQuery('#contact-form-editor .contact-form-editor-panel#google-sheets').addClass('active');
				jQuery('#contact-form-editor .contact-form-editor-panel.active').removeAttr('hidden');
			}
		}
	}
);
function wpsslcCopy( 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 wpsslcTab(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 wpsslcNavTab(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";
	}

	/* --------------------------
	   ADD view=forms SILENTLY
	-------------------------- */

	if (tabName === "wpssc-nav-forms") {

		var url = new URL(window.location.href);

		if (!url.searchParams.has("view")) {
			url.searchParams.set("view", "forms");

			// Update URL without reloading
			window.history.replaceState({}, "", url.toString());
		}
	}
}
function wpsslcNavTabfreevspro(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].className == 'navtablinks wpsslc-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 wpsslcNavTabLetsBegin(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( 'wpsslc-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) {
	"use strict";
	if ( ! url) {
		url = window.location.href;
	}
	name        = name.replace( /[\[\]]/g, '\\jQuery&' );
	var regex   = new RegExp( '[?&]' + name + '(=([^&#]*)|&|#|jQuery)' ),
		results = regex.exec( url );
	if ( ! results) {
		return null;
	}
	if ( ! results[2]) {
		return '';
	}
	return decodeURIComponent( results[2].replace( /\+/g, ' ' ) );
}
function wc_closeTopHeader() {
    const header = document.querySelector('.wpssw_order_top_header');
    if (header) {
        header.style.display = 'none';
    }
}
function wpsslcGetSheetsList(spreadsheetid) {

    // Show loader.
    jQuery("#sheetname").html('<option>Loading...</option>');

    jQuery.ajax({
        url: ajax_login_object.ajaxurl,
        type: "POST",
        data: {
            action: "wpsslc_get_sheets_list",
            spreadsheetid: spreadsheetid,
            wpnonce: jQuery("#wpsslc_sheet_settings").val()
        },
        success: function (response) {

        	let optionsHtml = '';

            if (response && response.success && response.data.html) {
                // If backend returned proper sheet list HTML.
                optionsHtml = response.data.html;
            } else {
                // If backend failed or empty response → SAFE fallback.
                optionsHtml =
                    '<option value="">Select Sheet</option>' +
                    '<option value="new">Create New Sheet</option>';
            }

            jQuery("#sheetname").html(optionsHtml);
        },

        error: function () {
            // Critical fallback (AJAX failed completely).
            jQuery("#sheetname").html(
                '<option value="">Select Sheet</option>' +
                '<option value="new">Create New Sheet</option>'
            );
        }
    });
}
