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='7' 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/bot-trading-balances",
			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='7' 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='7' 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_trade_amount, usr_trade_margin, usr_profit, usr_charges, usr_withdrawn) {
	$("input#balance_account_id").val(accountId);
	$("input#trade_amount").val(usr_trade_amount);
	$("input#trade_margin").val(usr_trade_margin);
	$("input#profit").val(usr_profit);
	$("input#charges").val(usr_charges);
	$("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 trade_amount = $("input#trade_amount").val();
	var trade_margin = $("input#trade_margin").val();
	var profit = $("input#profit").val();
	var charges = $("input#charges").val();
	var withdrawn = $("input#withdrawn").val();

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

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

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

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

	$("input#trade_amount").prop("disabled", true);
	$("input#profit").prop("disabled", true);
	$("input#charges").prop("disabled", true);
	$("input#withdrawn").prop("disabled", true);

	var form_data = {
		accountId: accountId,
		trade_amount: trade_amount,
		trade_margin: trade_margin,
		profit: profit,
		charges: charges,
		withdrawn: withdrawn,
	};

	$.ajax({
		url: "../admin-helpers/edit-bot-balances",
		type: "POST",
		data: form_data,
	}).done(function (response) {
		$("input#trade_amount").prop("disabled", false);
		$("input#profit").prop("disabled", false);
		$("input#charges").prop("disabled", false);
		$("input#withdrawn").prop("disabled", false);
		$("#save_btn").removeAttr("disabled");
		$(".save_spinner").hide();

		if (response == "success") {
			$("input#trade_amount").val("");
			$("input#profit").val("");
			$("input#charges").val("");
			$("input#withdrawn").val("");

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

			window.location.href = "bot-trading-balances";
		} else {
			$("#balance_errorPrompt").html(response);
			$("#balance_errorPrompt").show();
		}
	});
});
