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

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

function check() {
	
	let l = $("input[type='checkbox']");
	l.splice(-1, 1);
	l.splice(-1, 1);
	let ret = [];
	for (var i = 0; i < l.length; i++) {
		if (l[i].checked) {
			ret.push(ZoneCheckbox = {
					Id: l[i].id,
					Status: "True"
				});
		} else {
			ret.push(ZoneCheckbox = {
					Id: l[i].id,
					Status: "False"
				});
		}
	}
	return ret;
}

$(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 name = $("#nameDevice").val();
		var identificator = $("#identificatorDevice").text();
		var toPost = {
			Name: name,
			Identificator: "null",
			ZoneId: check(),
			AlertEmail: email.toString(),
			AlertSMS: sms.toString(),
			ZoneName: "null",
			Identificator: identificator,
			Id: $("#save").val(),

		}
		console.log(toPost);
		if (name) {
			$.ajax({
				type: "POST",
				data: JSON.stringify(toPost),
				url: "/Home/Device/EditDevice?handler=UpdateDevice",
				headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function () {

					window.location.href = "/Home/Device/Overwiev";
				},
			})
		}
	})
})

$(document).ready(function () {
	$("#backButton").click(function () {
		window.location.href = "/Home/Device/Overwiev";
	})
})

$(document).ready(function () {
	$("#partID").text("Device");
	$("#sectionID").text("Edit");
})