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

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

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

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

		$(".amount_received_feedback").hide();
		var amount_received = $("input#amount_received").val();

		if (amount_received == "") {
			$(".amount_received_feedback").html(
				"Amount received cannot be empty."
			);
			$(".amount_received_feedback").show();
			$("input#amount_received").focus();

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

		$(".button_spinner").show();

		$("input#amount_received").prop("disabled", true);

		var form_data = "amount_received=" + amount_received;

		$.ajax({
			url: "../admin-helpers/approve_invoice",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			$("#approve_investment").removeAttr("disabled");
			$(".button_spinner").hide();
			$("input#amount_received").prop("disabled", false);

			$("#errorPrompt").show();

			if (response == "success") {
				window.location.href = "invoices";
			} else if (response == "success-bot") {
			    window.location.href = "bot-invoices";
			} else {
				$("#errorPrompt").html(
					'<div class="alert alert-danger" role="alert">' +
						response +
						"</div>"
				);

				$("html, body").animate(
					{
						scrollTop: $("#top_area").offset().top,
					},
					500
				);
			}
		});

		return false;
	});
});
