var account_balances_history = $("#account_balances_history");

var search_field = $("input#search_field");
var search_button = $("#search_button");

var loading_status = $("#loading_status");

var history_page = $("input#history_page");
var total_pages = $("input#total_pages");
var all_history = $("input#all_history");

var prev_history_btn = $("#prev_history");
var next_history_btn = $("#next_history");

var pagination = $("#pagination");


var clear_search = false;

$(document).ready(function() {


	prev_history_btn.click(function(event) {

		var isDisabled = prev_history_btn.prop('disabled');

		if (!isDisabled && loading_status.html() != "loading") {
			var page = Number(history_page.val());

			if (page <=1) {
				$('#prev_history').prop('disabled', true);
			}else {
				page = page - 1

				fetchHistory(page);
			}


		}


	});

	next_history_btn.click(function(event) {

		var isDisabled = next_history_btn.prop('disabled');

		if (!isDisabled && loading_status.html() != "loading") {

			var page = Number(history_page.val());

			page = page + 1;

			fetchHistory(page);
		}
		
	});


	search_button.click(function(event) {
		var search_id = search_field.val();

		if (search_id != "") {

			fetchHistory(1);
		}
	});

	search_field.on('input',function(e){
		var search_id = search_field.val();

		if (search_id == "" && clear_search) {

			fetchHistory(1);
		}
	});

	function fetchHistory(page) {

		search_button.prop('disabled', true);
		search_field.prop('disabled', true);

		var search_id = search_field.val();

		if (search_id == "" ) {
			search_id = "default";
			clear_search = false;
		}else {
			clear_search = true;
		}

		account_balances_history.html("<tr><td colspan='8' class='tx-color-03 tx-normal text-center'>Loading...</td></tr>");


		loading_status.html("loading");

		var form_data = 
		'page='+ Number(page) +
		'&search_id='+encodeURIComponent(search_id);

		$.ajax({
			url: "../admin-helpers/admin_get_bot_account_balances_history",
			type: "POST",    
			data: form_data,    

		}).done(function(response) {

			if (isJson(response)) {
				var json_response = jQuery.parseJSON(response);

				var history = json_response.history;

				var h_total_pages = Number(json_response.total_pages);

				setTimeout(function() {

					account_balances_history.html(history);

					if (page <=1) {

						history_page.val(1);
						total_pages.val(h_total_pages);

						prev_history_btn.prop('disabled', true);
						next_history_btn.prop('disabled', false);
						prev_history_btn.addClass( "disabled" );
						next_history_btn.removeClass( "disabled" );

					}else if (page >= h_total_pages) {
							//Disable next
							history_page.val(Number(page));
							next_history_btn.prop('disabled', true);
							next_history_btn.addClass( "disabled" );
							prev_history_btn.prop('disabled', false);
							prev_history_btn.removeClass( "disabled" );

						}else {
							//enable next
							history_page.val(Number(page));
							prev_history_btn.prop('disabled', false);
							next_history_btn.prop('disabled', false);

							prev_history_btn.removeClass( "disabled" );
							next_history_btn.removeClass( "disabled" );
						}

						if (h_total_pages < 2) {
							pagination.hide();
						}else {
							pagination.show();
						}

						
					}, 1000);

			}else if (response == "empty") {
				if (page <=1) {
					account_balances_history.html("<tr><td colspan='8' class='tx-color-03 tx-normal text-center'>No data available</td></tr>");


					history_page.val(Number(page));

					pagination.hide();
				}else {
					prev_history_btn.prop('disabled', false);
					next_history_btn.prop('disabled', true);
					next_history_btn.addClass( "disabled" );
					prev_history_btn.removeClass( "disabled" );
					pagination.show();
				}

			}else {
				if (page <=1) {
					account_balances_history.html("<tr><td colspan='8' class='tx-color-03 tx-normal text-center'>" + response + "</td></tr>");

					pagination.hide();
				}else {
					pagination.show();
				}

			}

			search_button.prop('disabled', false);
			search_field.prop('disabled', false);

			loading_status.html("loaded");

		});

	}


	function getDate( element ) {
		var date;
		try {
			date = $.datepicker.parseDate( dateFormat, element.value );
		} catch( error ) {
			date = null;
		}

		return date;
	}

	function validate(dateString) {
		if (isValidDate(dateString)) {
			return true;
		} else {
			return false;
		}
	}

	function isValidDate(sText) {
		var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
		return reDate.test(sText);
	}

	function isJson(str) {
		try {
			JSON.parse(str);
		} catch (e) {
			return false;
		}
		return true;
	}


});



function editBalances(accountId, usr_total_bonus, usr_withdrawn) {

	$("input#balance_account_id").val(accountId);
	$("input#total_bonus").val(usr_total_bonus);
	$("input#withdrawn").val(usr_withdrawn);

	$('#modalEditBalances').modal({
		show: true,
		backdrop: 'static', 
		keyboard: false
	}); 
}

$('#save_btn').click(function(event){

	$('#save_btn').prop('disabled', true);

	$("#balance_errorPrompt").hide();
	$("#balance_errorPrompt").html("");      

	$('.profit_feedback').hide();
	var total_bonus = $("input#total_bonus").val();
	var withdrawn = $("input#withdrawn").val();

	var accountId = $("input#balance_account_id").val();

	if (total_bonus == "" || withdrawn == "") {
		$(".form_feedback").html("Please fill all fields.");
		$(".form_feedback").show();
		$("input#weekly_roi").focus();

		$("#save_btn").removeAttr("disabled");
		return false;
	}

	$(".save_spinner").show();

	$("input#total_bonus").prop('disabled', true);
	$("input#withdrawn").prop('disabled', true);

	var form_data = 
	'accountId='+accountId+
	'&total_bonus='+ total_bonus+
	'&withdrawn='+ withdrawn;

	$.ajax({
		url: "../admin-helpers/admin_edit_bot_account_balances",
		type: "POST",      
		data: form_data,    

	}).done(function(response) {

		$("input#total_bonus").prop('disabled', false);
		$("input#withdrawn").prop('disabled', false);
		$("#save_btn").removeAttr("disabled");
		$(".save_spinner").hide(); 

		if (response == "success") {
			
			$("input#total_bonus").val("");
			$("input#withdrawn").val("");

			$('#modalEditBalances').modal('toggle');

			window.location.href = "bot-account-balances";

		}else {
			$("#balance_errorPrompt").html(response);    
			$("#balance_errorPrompt").show();
		}

	});
});