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

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

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

		$(".address_feedback").hide();
		var address = $("input#address").val();

		if (address == "") {
			$(".address_feedback").html("Address cannot be empty.");
			$(".address_feedback").show();
			$("input#address").focus();

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

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

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

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

		$(".button_spinner").show();
		$("#withdrawal_method").prop("disabled", true);
		$("input#address").prop("disabled", true);
		$("input#amount").prop("disabled", true);

		var form_data =
			"address=" +
			address +
			"&amount=" +
			amount +
			"&withdrawal_method=" +
			$("#withdrawal_method option:selected").val();

		$.ajax({
			url: "../helpers/withdraw-profit",
			type: "POST",
			data: form_data,
			dataType: 'json',
		}).done(function (response) {
			$("#feedbackPrompt").show();
			$(".button_spinner").hide();
			$("#proceed_btn").removeAttr("disabled");

			$("input#address").prop("disabled", false);
			$("input#amount").prop("disabled", false);
			$("#withdrawal_method").prop("disabled", false);

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

		return false;
	});
});
