
var status_option = $("#package_status");
var dateFrom = $("input#dateFrom");
var dateTo = $("input#dateTo");
var withdrawals_history = $("#withdrawals_history");

var loading_status = $("#loading_status");

var history_page = $("input#history_page");
var total_pages = $("input#total_pages");
var all_history = $("input#all_history");

var prev_history_btn = $("#prev_history");
var next_history_btn = $("#next_history");

var pagination = $("#pagination");

$( document ).ready(function() {

	$('.withdraw').click(function(){ 
		window.location.href = "withdraw?account=bot";
	});

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

	status_option.on('change', function() {
		fetchHistory(1);
	});

	var dateFormat = 'dd/mm/yy';

	var from = dateFrom.datepicker({
		format: "dd/mm/yyyy",
		todayHighlight: true,
		startDate: '01/01/2020',
		endDate: '0',
		autoclose: true
	}).on('changeDate', function (ev) {
		to.datepicker('setStartDate', dateFrom.val());
		fetchHistory(1);
	});

	var to = dateTo.datepicker({
		format: "dd/mm/yyyy",
		todayHighlight: true,
		autoclose: true,
		endDate: '0'
	}).on('changeDate', function (ev) {
		from.datepicker('setEndDate', dateTo.val());
		fetchHistory(1);
	});


	prev_history_btn.click(function(event) {

		var isDisabled = prev_history_btn.prop('disabled');

		if (!isDisabled && loading_status.html() != "loading") {
			var page = Number(history_page.val());

			if (page <=1) {
				$('#prev_history').prop('disabled', true);
			}else {
				page = page - 1

				fetchHistory(page);
			}


		}


	});

	next_history_btn.click(function(event) {

		var isDisabled = next_history_btn.prop('disabled');

		if (!isDisabled && loading_status.html() != "loading") {

			var page = Number(history_page.val());

			page = page + 1;

			fetchHistory(page);
		}
		
	});


	function fetchHistory(page) {

		if (validate(dateFrom.val()) && validate(dateTo.val())) {

			status_option.prop('disabled', true);
			dateFrom.prop('disabled', true);
			dateTo.prop('disabled', true);

			withdrawals_history.html("<tr><td colspan='6' class='tx-color-03 tx-normal text-center'>Loading...</td></tr>");

			loading_status.html("loading");

			var form_data = 
			'status='+$("#package_status option:selected").val()+
			'&dateFrom='+dateFrom.val()+
			'&dateTo='+dateTo.val()+
			'&page='+Number(page);

			$.ajax({
				url: "../helpers/user_get_bot_withdrawals_history",
				type: "POST",    
				data: form_data,    

			}).done(function(response) {

				if (isJson(response)) {
					var json_response = jQuery.parseJSON(response);

					var history = json_response.history;
					var h_total_pages = Number(json_response.total_pages);

					setTimeout(function() {

						withdrawals_history.html(history);

						if (page <=1) {

							history_page.val(1);
							total_pages.val(h_total_pages);

							prev_history_btn.prop('disabled', true);
							next_history_btn.prop('disabled', false);
							prev_history_btn.addClass( "disabled" );
							next_history_btn.removeClass( "disabled" );

						}else if (page >= h_total_pages) {
							//Disable next
							history_page.val(Number(page));
							next_history_btn.prop('disabled', true);
							next_history_btn.addClass( "disabled" );
							prev_history_btn.prop('disabled', false);
							prev_history_btn.removeClass( "disabled" );

						}else {
							//enable next
							history_page.val(Number(page));
							prev_history_btn.prop('disabled', false);
							next_history_btn.prop('disabled', false);

							prev_history_btn.removeClass( "disabled" );
							next_history_btn.removeClass( "disabled" );
						}

						if (h_total_pages < 2) {
							pagination.hide();
						}else {
							pagination.show();
						}

						
					}, 1000);
					
				}else if (response == "empty") {
					if (page <=1) {
						withdrawals_history.html("<tr><td colspan='6' class='tx-color-03 tx-normal text-center'>No data available</td></tr>");

						history_page.val(Number(page));

						pagination.hide();
					}else {
						prev_history_btn.prop('disabled', false);
						next_history_btn.prop('disabled', true);
						next_history_btn.addClass( "disabled" );
						prev_history_btn.removeClass( "disabled" );
						pagination.show();
					}
					
				}else {
					if (page <=1) {
						withdrawals_history.html("<tr><td colspan='6' class='tx-color-03 tx-normal text-center'>An error occurred...</td></tr>");
						pagination.hide();
					}else {
						pagination.show();
					}
					
				}

				status_option.prop('disabled', false);
				dateFrom.prop('disabled', false);
				dateTo.prop('disabled', false);

				loading_status.html("loaded");
				
			});

		}

	}


	function getDate( element ) {
		var date;
		try {
			date = $.datepicker.parseDate( dateFormat, element.value );
		} catch( error ) {
			date = null;
		}

		return date;
	}

	function validate(dateString) {
		if (isValidDate(dateString)) {
			return true;
		} else {
			return false;
		}
	}

	function isValidDate(sText) {
		var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
		return reDate.test(sText);
	}

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


function cancelWithdrawal(id) {

	$("input#cancel_id").val(id);

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

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

	$(".cancel_spinner").show();

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

	var id = $("input#cancel_id").val();

	window.location.href = "?action=cancelWithdrawal&id=" + id;
});



