//Go Between the Tabs
( function ( $ ){
    "use strict";
	let $widgetkitsingletemplate = $(".widgetkit-single-template-wrapper");
	if($widgetkitsingletemplate.length) {
		$(".ele-template-importer").on("click",function(event){
			event.preventDefault();
			let $button = $(this);
			let $tid = $(this).data('td');
			$.ajax( {
				url: settings.ajaxurl,
				type: 'POST',
				data: {
					action: 'widgetkit_save_import_elementor_template',
					tid: $tid,
				},
				beforeSend: function(){
					$button.addClass("importing");
				},
				success: function( response ) {
					$button.removeClass("importing");
					if( response.data.postid ) {
						swal({
							title: 'Template Imported!',
							type: 'success',
							showCancelButton: true,
							confirmButtonText: 'Edit With Elementor',
							cancelButtonText: 'Preview',
							confirmButtonColor: '#d30c5c'
						  }).then(function() {
							window.location.href = settings.site_admin_url + '?post='+ response.data.postid +'&action=elementor';
						  }, function(dismiss) {
							// dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
							if (dismiss === 'cancel') {
								window.location.href = response.data.permalink;
							}
						});
					}
				},
				error: function() {
					$button.removeClass("importing");
					swal(
					  'Oops...',
					  'Something Wrong!',
					);
				}
			} );
		});

		$widgetkitsingletemplate.find(".page-import-form").on("submit",function(e){
			e.preventDefault();
			let $pagetitle = $(this).find('input[name="single-template-page-name"]');
			let $button = $(this).find('.button');
			let $tid = $(this).data('td');
			$.ajax( {
				url: settings.ajaxurl,
				type: 'POST',
				data: {
					action: 'widgetkit_save_import_remote_template_page',
					pagename: $pagetitle.val(),
					tid: $tid
				},
				beforeSend: function(){
					$button.addClass("importing");
				},
				success: function( response ) {
					$button.removeClass("importing");
					if( response.data.postid ) {
						$pagetitle.val('');
						swal({
							title: 'Template Imported!',
							type: 'success',
							showCancelButton: true,
							confirmButtonText: 'Edit With Elementor',
							cancelButtonText: 'Preview',
							confirmButtonColor: '#d30c5c',
						  }).then(function() {
							window.location.href = settings.site_admin_url + '?post='+ response.data.postid +'&action=elementor';
						  }, function(dismiss) {
							// dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
							if (dismiss === 'cancel') {
								window.location.href = response.data.permalink;
							}
						});
					}
				},
				error: function() {
					$button.removeClass("importing");
					swal(
					  'Oops...',
					  'Something Wrong!',
					);
				}
			} );
		});
	}
    
} )(jQuery);