var typingTimer;                
var doneTypingInterval = 500; 
var $input = $('#username');

$input.on('keyup', function () {
  clearTimeout(typingTimer);
  typingTimer = setTimeout(doneTyping, doneTypingInterval);
});

$input.on('keydown', function () {
  clearTimeout(typingTimer);
});

function doneTyping () {
	var url = "Install/dashboard";
 	// ajax(url)
}
// function ajax(url){
// 	$.ajax({
// 	  url: `${urlroot}/${url}`,
// 	  type: 'GET',
// 	  dataType: 'JSON',
// 	  success: function(res) {
// 	   console.log(res)
// 	  }
// 	});

// }

function nextUser(){
	

	  $('#databse_error').hide();


	var database_name =  $('#database_name');
	var database_username =  $('#database_username');
	var databse_password =  $('#databse_password');
	var base_url =  $('#base_url');
	

	database_name.removeClass('is-invalid');
	database_username.removeClass('is-invalid');
	databse_password.removeClass('is-invalid');
	base_url.removeClass('is-invalid');

	$('#databse_name_error').css('display','none');
	$('#databse_username_error').css('display','none');
	$('#databse_password_error').css('display','none');
	
	 if(database_name.val() == ""){
		database_name.addClass('is-invalid');
		$('#databse_name_error').css('display','block');
	}else if(database_username.val() == ""){
		database_username.addClass('is-invalid');
		$('#databse_username_error').css('display','block');
	}else if(databse_password.val() == ""){
		databse_password.addClass('is-invalid');
		$('#databse_password_error').css('display','block');
	}else if(base_url.val() == ""){
		base_url.addClass('is-invalid');
	}else{

		var data = [database_name.val(),database_username.val(),databse_password.val(),base_url.val()];
		base_url_set = base_url.val();
		$.ajax({
	  url: `${base_url_set}/Install/databaseConnect`,
	  type: 'POST',
	  dataType: 'JSON',
	  data:{data},
	  success: function(res) {
	  	console.log(res)
	  	if(res === true){
	  		$('#save_button').show();
				$('#nextbutton').hide();
				$('#step2').show();
				$('#step1').hide();
	  	}else{
	  		$('#databse_error').html(res).show();
	  	}
	  	
	  }
	});
		
	}
}
function saveUser(){
	var username =  $('#username');
	var password =  $('#password');
	var database_name =  $('#database_name');
	var database_username =  $('#database_username');
	var databse_password =  $('#databse_password');
	var base_url =  $('#base_url');
	
	username.removeClass('is-invalid');
	password.removeClass('is-invalid');
	$('#username_error').css('display','none');
	$('#password_error').css('display','none');

	if(username.val() == ""){
		username.addClass('is-invalid');
		$('#username_error').css('display','block').html('User name is required');
	}else if(password.val() == ""){
		password.addClass('is-invalid');
		$('#password_error').css('display','block');
	}else{
		// base_url_set = base_url.val();
		var data = [username.val(),password.val()];
		var url = "UserController/saveUser";
		ajaxUrl(url,data)
	}
}

function ajaxUrl(url,data){

	$.ajax({
	  url: `${base_url_set}/${url}`,
	  type: 'POST',
	  dataType: 'JSON',
	  data:{data},
	  success: function(res) {
	  	console.log(res)
	  	if(res === true){
	  		var message = "User has been created successfully!";
	  		var title = "installation succeeded"
	  		LoadMessage(message,title);
	  		setTimeout(function(){
	  			window.open(`${base_url_set}/login`,"_self")
	  		},2000)

	  	}else{
	  		var message = "user already exists."
	  		var title = "installation Error"
	  		LoadMessageError(message,title)
	  	}
	  }
	});

}

function LoadMessage(message,title){

	   toastr.options = {
                  "closeButton": false,
                  "debug": false,
                  "newestOnTop": false,
                  "progressBar": false,
                  "positionClass": "toast-top-right",
                  "preventDuplicates": false,
                  "onclick": null,
                  "showDuration": "300",
                  "hideDuration": "1000",
                  "timeOut": "5000",
                  "extendedTimeOut": "1000",
                  "showEasing": "swing",
                  "hideEasing": "linear",
                  "showMethod": "fadeIn",
                  "hideMethod": "fadeOut"
                };

                toastr.success(message);
}
function LoadMessageError(message,title){

	 toastr.options = {
  "closeButton": false,
  "debug": false,
  "newestOnTop": false,
  "progressBar": false,
  "positionClass": "toast-top-right",
  "preventDuplicates": false,
  "onclick": null,
  "showDuration": "300",
  "hideDuration": "1000",
  "timeOut": "5000",
  "extendedTimeOut": "1000",
  "showEasing": "swing",
  "hideEasing": "linear",
  "showMethod": "fadeIn",
  "hideMethod": "fadeOut"
};

toastr.error(message);

}