jQuery(document).ready(function(){
	 
	 jQuery('#dap_security_scan_table').DataTable();
	 
	 jQuery(".dap_save_setting").on("click", function(){
		 
		var  scan_name ="scan_type";
		var scan_scheduled = jQuery("#scan_scheduled_automation").val();	
		jQuery(".dap_save_setting").text("Please Wait...");
		jQuery.post(ajaxurl, {
			action: 'dapSecurityScan',
			scan_name: scan_name,
			scan_scheduled: scan_scheduled,
		}, function(response) {
			  jQuery(".dap_save_setting").text("Saved");
			  setTimeout(function() {
				 jQuery(".dap_save_setting").text("Save");
			 }, 1000);
		 
		 
		});	

	});
	
	jQuery(document).on('click', '.del_file_full_path',  function(e){
		e.preventDefault();
		jQuery(this).text('Deleting');
		
		var id= jQuery(this).attr('data-id');
		var fullPath = jQuery('#file_full_path_'+id).val();
		jQuery.post(ajaxurl, {
			action: 'dapDeleteComparedFiles',
			fullPath: fullPath,
			id: id,
		}, function(response) {
			 // location.reload();
			 
			 getComparedFilesFromTable();
			 
		});	
		
	});
	
	
	jQuery('#manual_compare_files').click(function(e){
		e.preventDefault();
		jQuery("#manual_compare_files").text("Please Wait...");
		jQuery.post(ajaxurl, {
			action: 'dapCompareFiles',
			callByAjax: 'yes',
		}, function(response) {
			response = JSON.parse(response);
			if(typeof response.error != 'undefined' && response.error != ''){
				jQuery('#showDapVersionError').html(response.error);
				jQuery('#showDapVersionError').show();
			} 
			getComparedFilesFromTable();
			 
		});	
		
		
	})
});


function delete_chk_single(id){
 	var del_checkout_id = jQuery('#' + id).attr('data-id');
	 
		swal({
		  title: "Are you sure you want to delete?",
		  text: "You cannot recover the settings.",
		  type: "warning",
		  showCancelButton: true,
		  confirmButtonClass: "btn-danger",
		  confirmButtonText: "Yes, delete!",
		  confirmButtonColor: '#dc3545',
		  cancelButtonText: "Cancel"
		 }).then(function(isConfirm){
			if (isConfirm) {	 
				jQuery.post(ajaxurl, {
					action: 'dapSecurityScanDelete',
					id: del_checkout_id,
				}, function(response) {
					console.log(response);
					window.location.reload();
				});				 
		   }  
		});

}


function getComparedFilesFromTable(){
	jQuery.post(ajaxurl, {
		action: 'getComparedFilesFromTable',
	}, function(response) {
		response = JSON.parse(response);
		console.log(response);
		jQuery('.compareFilesTableOuter').html(response);
		var table = jQuery('#dap_security_scan_table').DataTable(); 
		table.draw();
		jQuery("#manual_compare_files").text("Click Here To Start Manual Scan");
		jQuery(".del_file_full_path").text('Delete');
	});	
}
