var dateFrom = $("input#dateFrom");
var dateTo = $("input#dateTo");

var users_history = $("#users_history");

var search_field = $("input#search_field");
var search_button = $("#search_button");

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");

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

var clear_search = false;

var dateFormat = "dd/mm/yy";

$(document).ready(function () {
	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);
		}
	});

	search_button.click(function (event) {
		var search_id = search_field.val();

		if (search_id != "") {
			fetchHistory(1);
		}
	});

	search_field.on("input", function (e) {
		var search_id = search_field.val();

		if (search_id == "" && clear_search) {
			fetchHistory(1);
		}
	});

	function fetchHistory(page) {
		if (validate(dateFrom.val()) && validate(dateTo.val())) {
			search_button.prop("disabled", true);
			search_field.prop("disabled", true);
			dateFrom.prop("disabled", true);
			dateTo.prop("disabled", true);

			var search_id = search_field.val();

			if (search_id == "") {
				search_id = "default";
				clear_search = false;
			} else {
				clear_search = true;
			}

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

			loading_status.html("loading");

			var form_data =
				"dateFrom=" +
				dateFrom.val() +
				"&dateTo=" +
				dateTo.val() +
				"&page=" +
				Number(page) +
				"&search_id=" +
				encodeURIComponent(search_id);

			$.ajax({
				url: "../admin-helpers/users-referrals",
				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 () {
						users_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) {
						users_history.html(
							"<tr><td colspan='9' 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) {
						users_history.html(
							"<tr><td colspan='9' class='tx-color-03 tx-normal text-center'>" +
								response +
								"</td></tr>"
						);

						pagination.hide();
					} else {
						pagination.show();
					}
				}

				search_button.prop("disabled", false);
				search_field.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;
	}
});
