$(function () {
	$("#save-btn").click(function (event) {
		event.preventDefault();

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

		$("#save-btn").attr("disabled", "");

		var btcAddress = $("input#btc-address").val();
		var ethAddress = $("input#eth-address").val();
		var usdtAddress = $("input#usdt-address").val();
        var ercAddress = $("input#erc-address").val();
		var trcAddress = $("input#trc-address").val();
		var ltcAddress = $("input#ltc-address").val();
		var minWithdrawal = $("input#min-withdrawal").val();
		var maxWithdrawal = $("input#max-withdrawal").val();

		if (
			btcAddress === "" ||
			ethAddress === "" ||
			usdtAddress === "" ||
			ercAddress === "" ||
			trcAddress === "" || 
			ltcAddress === "" ||
			minWithdrawal === "" ||
			maxWithdrawal === ""
		) {
			$("#feedbackPrompt").html(
				'<div class="alert alert-danger" role="alert">Make sure to fill  all fields!</div>'
			);

			$("#feedbackPrompt").show();
			$("#save-btn").removeAttr("disabled");
			$("html, body").animate(
				{
					scrollTop: $("#main-wrapper").offset().top,
				},
				500
			);
			return false;
		}

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

		$("input#btc-address").prop("disabled", true);
		$("input#eth-address").prop("disabled", true);
		$("input#usdt-address").prop("disabled", true);
		$("input#erc-address").prop("disabled", true);
		$("input#trc-address").prop("disabled", true);
		$("input#ltc-address").prop("disabled", true);
		$("input#max-withdrawal").prop("disabled", true);
		$("input#min-withdrawal").prop("disabled", true);

		let request_data = new FormData();
		request_data.append("btcAddress", encodeURIComponent(btcAddress));
		request_data.append("ethAddress", encodeURIComponent(ethAddress));
		request_data.append("usdtAddress", encodeURIComponent(usdtAddress));
		request_data.append("ercAddress", encodeURIComponent(ercAddress));
		request_data.append("trcAddress", encodeURIComponent(trcAddress));
		request_data.append("ltcAddress", encodeURIComponent(ltcAddress));
		request_data.append("minWithdrawal", encodeURIComponent(minWithdrawal));
		request_data.append("maxWithdrawal", encodeURIComponent(maxWithdrawal));

		$.ajax({
			url: "../admin-helpers/app-settings",
			type: "POST",
			data: request_data,
			processData: false,
			contentType: false,
		}).done(function (response) {
			if (response == "success") {
				$("#feedbackPrompt").html(
					'<div class="alert alert-success" role="alert">Settings saved!</div>'
				);
			} else {
				$("#feedbackPrompt").html(
					'<div class="alert alert-danger" role="alert">' +
						response +
						"</div>"
				);
			}

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

			$("#feedbackPrompt").show();
			$("#save-btn").removeAttr("disabled");
			$(".button_spinner").hide();

			$("input#btc-address").prop("disabled", false);
			$("input#eth-address").prop("disabled", false);
			$("input#usdt-address").prop("disabled", false);
			$("input#max-withdrawal").prop("disabled", false);
			$("input#min-withdrawal").prop("disabled", false);
			$("input#trc-address").prop("disabled", false);
			$("input#erc-address").prop("disabled", false);
			$("input#ltc-address").prop("disabled", false);
		});
		return false;
	});
});
