var account_option = $("#account");

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

account_option.on('change', function() {
	checkAccount();
});

$( document ).ready(function() {

	$('#amountUSD').keyup(function(event){

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

		if (amountUSD > 0) {

			$.getJSON( "http://api.coindesk.com/v1/bpi/currentprice/usd.json", function( data) {
				var exchangeRate = parseInt(data.bpi.USD.rate_float);
				var amount = amountUSD / exchangeRate;
				$("input#amountBTC").val("" + amount);
			});
		}else{
		    $("input#amountBTC").val("");
		}

	});


	$('#amountBTC').keyup(function(event){

		var amountBTC = $("input#amountBTC").val();

		if (amountBTC > 0) {

			$.getJSON( "https://api.coindesk.com/v1/bpi/currentprice/usd.json", function( data) {
				var exchangeRate = parseInt(data.bpi.USD.rate_float);
				var amount = amountBTC * exchangeRate;
				$("input#amountUSD").val("" + amount);
			});
		}

	});

	$('#proceed_btn').click(function(event){

		event.preventDefault();

		$("#errorPrompt").hide();
		$("#errorPrompt").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;
		}

		var amountBTC = $("input#amountBTC").val();

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

		var form_data = 
		'accountId='+accountId;

		$.ajax({
			url: "../admin-helpers/admin_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 {
				$("#errorPrompt").html(response); 
				$("#errorPrompt").show();

				$("input#accountId").prop('disabled', false);
				$("input#amountUSD").prop('disabled', false);
				$("input#amountBTC").prop('disabled', true);
				account_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 amountUSD = $("input#amountUSD").val();

	$('#withdraw_message').html("Are you sure you want to withdraw <b>" + amountUSD  + " USD</b> from <b>" + user_name +
		"</b> with account ID <b>" + accountId + "</b>?");

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

	$('#modalWithdraw').on('hidden.bs.modal', function () {
		$("input#accountId").prop('disabled', false);
		$("input#amountUSD").prop('disabled', false);
		$("input#amountBTC").prop('disabled', true);
		account_option.prop('disabled', false); 

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


$('#withdraw_btn').click(function(event){

	$(".withdraw_spinner").show();
	$("#withdraw_errorPrompt").hide();

	$('#withdraw_btn').prop('disabled', true);

	var accountId = $("input#accountId").val();
	var amountUSD = $("input#amountUSD").val();
	var amountBTC = $("input#amountBTC").val();
	var account = $("#account option:selected").val();

	var form_data = 
	'amountUSD='+amountUSD+
	'&amountBTC='+amountBTC+
	'&accountId='+accountId+
	'&account='+account;

	$.ajax({
		url: "../admin-helpers/admin_withdraw_user_profit",
		type: "POST",      
		data: form_data,    

	}).done(function(response) {
		$('#withdraw_btn').prop('disabled', false);
		$(".withdraw_spinner").hide();

		if (response == "success" ) {
			$("#withdraw_errorPrompt").hide();
			$("#errorPrompt").html("Account debited successfully."); 
			$("#errorPrompt").show();

			$("input#accountId").val("");
			$("input#amountUSD").val("");
			$("input#amountBTC").val("");

			$('#modalWithdraw').modal('toggle');

		}else {
			$("#withdraw_errorPrompt").show();
			$("#withdraw_errorPrompt").html(response); 
		}

	});

});


function checkAccount() {

	var account = $("#account option:selected").val();

	if (account == "ultimate") {

		$("input#amountBTC").prop('disabled', false);
		$("input#amountUSD").prop('disabled', true);

		var amountBTC = $("input#amountBTC").val();

		$.getJSON( "https://api.coindesk.com/v1/bpi/currentprice/usd.json", function( data) {
			var exchangeRate = parseInt(data.bpi.USD.rate_float);
			var amount = amountBTC * exchangeRate;
			$("input#amountUSD").val("" + amount);
			$("input#amountBTC").val("" + amountBTC);
		});

	}else {
		$("input#amountBTC").prop('disabled', true);
		$("input#amountUSD").prop('disabled', false);
	}

}