var package_option = $("#package");

var classes = $("#invest").prop("class");
$("#invest").prop("class", classes + " active");

$(document).ready(function () {
	$("#proceed_btn").click(function (event) {
		event.preventDefault();

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

		$("#proceed_btn").attr("disabled", "");

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

		if (accountId == "") {
			$(".accountId_feedback").html("Account ID cannot be empty.");
			$(".accountId_feedback").show();
			$("input#accountId").focus();

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

		$(".amount_feedback").hide();
		var amountUSD = $("input#amountUSD").val();

		if (amountUSD == "") {
			$(".amount_feedback").html("Amount cannot be empty.");
			$(".amount_feedback").show();
			$("input#amountUSD").focus();

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

		$("input#accountId").prop("disabled", true);
		$("input#amountUSD").prop("disabled", true);
		package_option.prop("disabled", true);

		var form_data = "accountId=" + accountId;

		$.ajax({
			url: "../helpers/get_user_account",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			if (isJson(response)) {
				var json_response = jQuery.parseJSON(response);

				var user_name = json_response.user_name;

				showPopUp(user_name);
			} else {
				$("#feedbackPrompt").html(
					'<div class="alert alert-danger" role="alert">' +
						response +
						"</div>"
				);
				$("#feedbackPrompt").show();

				$("input#accountId").prop("disabled", false);
				$("input#amountUSD").prop("disabled", false);
				package_option.prop("disabled", false);

				$(".button_spinner").hide();
				$("#proceed_btn").removeAttr("disabled");
			}
		});

		return false;
	});

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

function showPopUp(user_name) {
	var accountId = $("input#accountId").val();
	var amount = $("input#amountUSD").val();

	$("#inv_message").html(
		"Are you sure you want to transfer <b>" +
        amount +
			" USD </b>" +
			" to <b>" +
			user_name +
			"</b> with account ID <b>" +
			accountId +
			"</b>?"
	);

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

	$("#modalTransferFunds").on("hidden.bs.modal", function () {
		$("input#accountId").prop("disabled", false);
		$("input#amountUSD").prop("disabled", false);

		$(".button_spinner").hide();
		$("#proceed_btn").removeAttr("disabled");
	});
}

$("#transfer_btn").click(function (event) {
	$(".create_spinner").show();
	$("#create_errorPrompt").hide();

	$("#transfer_btn").prop("disabled", true);

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

	var form_data =
		"amountUSD=" +
		amountUSD +
		"&accountId=" +
		accountId;

	$.ajax({
		url: "../helpers/transfer_funds",
		type: "POST",
		data: form_data,
	}).done(function (response) {
		$("#transfer_btn").prop("disabled", false);
		$(".create_spinner").hide();

		if (response == "success") {
			$("#create_errorPrompt").hide();
			$("#feedbackPrompt").html(
				'<div class="alert alert-success" role="alert">Funds transferred successfully.</div>'
			);
			$("#feedbackPrompt").show();

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

            $("input#accountId").val("");
            $("input#amountUSD").val("");
		} else {
			$("#create_errorPrompt").show();
			$("#create_errorPrompt").html(
				'<div class="alert alert-danger" role="alert">' +
					response +
					"</div>"
			);
		}
	});
});
