$(':checkbox').checkboxpicker();


$(document).ready(function () {
	$("#partID").text("Devices");
	$("#sectionID").text("Overview");
})

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

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

function deleteItem(id) {
	console.log(id);
	$.ajax({
		type: "POST",
		data: JSON.stringify(id),
		url: "/Home/Device/Overwiev?handler=DeleteDevice",
		headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
		contentType: "application/json; charset=utf-8",
		dataType: "string",
		success: function () {
			window.location.reload();
		},
		error: function () {
			window.location.reload();	
		}
	})
}

$(document).ready(function () {
	$("#save").click(function () {
		let smsEmail = $("input[type='checkbox']");
		var sms = smsEmail[smsEmail.length - 1].checked;
		var email = smsEmail[smsEmail.length - 2].checked;
		var zone;
		console.log($("#selectZone option:selected").val());
		if ($("#selectZone option:selected").val() != undefined) {
			zone = $("#selectZone option:selected").val().toString();
		} else {
			zone = "null";
		}
		var name = $("#nameDevice").val();
		var toPost = {
			Name: name,
			Identificator: "null",
			ZoneId: [ZoneCheckbox = {
				Id: zone,
				Status: "True"
			}],
			AlertEmail: email.toString(),
			AlertSMS: sms.toString(),
			Id: 0
		}
		if (name) {
			$.ajax({
				type: "POST",
				data: JSON.stringify(toPost),
				url: "/Home/Device/Overwiev?handler=SaveDevice",
				headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function () {

					location.reload();
				},
			})
		}
	})
})
