//Go Between the Tabs
( function ( $ ){
    "use strict";
    $(".widgetkit-settings-tabs").tabs();
    
    
    $("a.widgetkit-tab-list-item").on("click", function () {
        var tabHref = $(this).attr('href');
        window.location.hash = tabHref;
        $("html , body").scrollTop(tabHref);
    });
    
    $(".widgetkit-checkbox").on("click", function(){
       if($(this).prop("checked") == true) {
           $(".widgetkit-elements-table input").prop("checked", 1);
       }else if($(this).prop("checked") == false){
           $(".widgetkit-elements-table input").prop("checked", 0);
       }
    });
   
    
    $( 'form#widgetkit-free-settings' ).on( 'submit', function(e) {
		e.preventDefault();
		$.ajax( {
			url: settings.ajaxurl,
			type: 'post',
			data: {
				action: 'widgetkit_free_save_admin_addons_settings',
				fields: $( 'form#widgetkit-free-settings' ).serialize(),
			},
            success: function( response ) {
				swal(
				  'Settings Saved!',
				  'Click OK to continue',
				  'success'
				);
			},
			error: function() {
				swal(
				  'Oops...',
				  'Something Wrong!',
				);
			}
		} );

	} );

	    $( 'form#widgetkit-pro-settings' ).on( 'submit', function(e) {
		e.preventDefault();
		$.ajax( {
			url: settings.ajaxurl,
			type: 'post',
			data: {
				action: 'widgetkit_pro_save_admin_addons_settings',
				fields: $( 'form#widgetkit-pro-settings' ).serialize(),
			},
            success: function( response ) {
				swal(
				  'Settings Saved!',
				  'Click OK to continue',
				  'success'
				);
			},
			error: function() {
				swal(
				  'Oops...',
				  'Something Wrong!',
				);
			}
		} );

	} );

	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);