function getSystemSettingsPage(page) {
	var todo = "system_settings&todo=get_system_settings&page=" + page;

	getAjax(todo, getSystemSettingsPageReponse);
}

function getSystemSettingsPageReponse(response) {
	console.log(response);
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		document.getElementById("system_settings_main_container").innerHTML = responseArray.htmlData;
		initDatepicker();
		if(responseArray.js_files != undefined) {
			responseArray.js_files.forEach(function (item, index) {
				addJSFileToHeader(item);
			});
		}
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

function editSystemUser(userID) {
	var todo = "system_settings&todo=edit_system_user&user_id=" + userID;

	getAjax(todo, editSystemUserReponse);
}

function editSystemUserReponse(response) {
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		document.getElementById("system_settings_main_container").innerHTML = responseArray.htmlData;
		initDatepicker();
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

function saveUserData(thisForm) {
	if (!(thisForm instanceof HTMLFormElement)) {
		console.error("thisForm is not a valid form element");
		return;
	}
	let formData = new FormData(thisForm);
    let todo = "system_settings&todo=save_user_data";
	for (let [key, value] of formData.entries()) {
		console.log(key, value); // Logs key-value pairs to ensure data is added correctly.
	}	
    postAjax(todo, formData, saveUserDataReponse);	
}

function saveUserDataReponse(response) {
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		popupMessage(responseArray.popupMessage, "lightgreen")
	} else {
		popupMessage(responseArray.errorMessage);
	}

	return false;
}

function updateUserPermission(uid, functionId, permission) {
	let todo = "system_settings&todo=update_user_permission&user_id=" + uid + "&permission_lvl=" + permission + "&function_id=" + functionId;

	getAjax(todo, updateUserPermissionReponse);
}

function updateUserPermissionReponse(response) {
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		popupMessage(responseArray.popupMessage, "lightgreen")
	} else {
		popupMessage(responseArray.errorMessage);
	}
}