$("#goupEditTable").fancyTable({
	sortCsortColumn: 1,
	sortable: true,
	pagination: true,
	searchable: true,
	globalSearch: true,
	perPage: 10,
})

$('#selectAddDevice').scombobox({
	invalidAsValue: true,
	fullMatch: true
});

function addNewUser(idGroup) {
	let value = $("#addNewUser").val();
	if (value) {
		let toPost = createPostDate(idGroup, value);
		$.ajax({
			type: "POST",
			data: JSON.stringify(toPost),
			url: "/Home/Group/EditGroup?handler=AddNewUser",
			headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (response) {
				if (response === "done") {
					handleResponse(response);
				} else {
					alert(response);
				}
			},
		})
	} else {
		alert("Please fill email address of user you wish to add to your group.")
	}
}

function addDevice(idGroup) {
	let value = $("#selectAddDevice option:selected").val();
	if (value !== "null") {
		let toPost = createPostDate(idGroup, value);
		$.ajax({
			type: "POST",
			data: JSON.stringify(toPost),
			url: "/Home/Group/EditGroup?handler=AddNewDevice",
			headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (response) {
				handleResponse(response)
			},
		})
	}
}

function deleteFromGroup(idGroup, idDevice) {
	let toPost = createPostDate(idGroup, idDevice);
	$.ajax({
		type: "POST",
		data: JSON.stringify(toPost),
		url: "/Home/Group/EditGroup?handler=DeleteDevice",
		headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (response) {
			handleResponse(response);
		},
	})
}

function handleResponse(response) {
	if (response) {
		location.reload();
	}
}

function createPostDate(id, value) {
	let ret = {
		IdGroup: id,
		Value: value
	}
	return ret;
}

$(document).ready(function () {
	$("#partID").text("Group");
	$("#sectionID").text("Detail");
	
})