var deviceMarkers = [];

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

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

function showTrip(i, idDevice) {
	let cells = $("#" + i).children("td");
	let star = cells[5].innerText;
	let end = cells[7].innerText;
	let toPost = {
		Id: idDevice,
		Start: star,
		End: end,
		Distance: "",
		Identificator: "",
		User: "",
		PlaceFrom: "",
		PlaceTo: ""
	};
	$.ajax({
		type: "POST",
		data: JSON.stringify(toPost),
		url: "/Home/Trip/Overwiev?handler=ShowTrip",
		headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (data) {
			clearMarkers();
			setCenterMap(data);
		},
	})
}

function getLocation() {
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(showPosition);
	}
}
function showPosition(position) {
	lati = position.coords.latitude;
	longi = position.coords.longitude;
	console.log(lati + " " + longi);
	initMap(lati, longi);
}
function initMap(lati, longi) {
	map = new google.maps.Map(document.getElementById('map'), {
		center: { lat: lati, lng: longi },
		zoom: 16
	});
}

function setCenterMap(data) {
	if (data.length != 0) {
		map.setCenter({ lat: data[data.length - 1].latitude, lng: data[data.length - 1].longitude });
		setDeviceMarks(data);
	}
}

function setDeviceMarks(device) {
	for (var i in device) {
		let marker = new google.maps.Marker({
			position: { lat: device[i].latitude, lng: device[i].longitude },
			map: map,
			title: device[i].dateTime,
			icon: {
				path: google.maps.SymbolPath.CIRCLE,
				scale: 5
			},
			draggable: false,
		})
		deviceMarkers.push(marker);
	}
	$("#map").show();
}

function clearMarkers() {
	for (var i = 0; i < deviceMarkers.length; i++) {
		deviceMarkers[i].setMap(null);
	}
}

function print() {
	let checkboxes = $(":checkbox");
	let trips = [];
	for (var i = 0; i < checkboxes.length; i++) {
		if (checkboxes[i].checked) {
			let tr = checkboxes[i].parentElement.parentElement;
			let id = tr.id;
			let cells = $("#" + id).children("td");
			let from = cells[5].innerText;
			let to = cells[7].innerText;
			let distance = cells[9].innerText;
			let iden = cells[3].innerText;
			let user = cells[4].innerText;
			let placeFrom = cells[6].innerText;
			let placeTo = cells[8].innerText;
			let toPost = {
				Id: parseInt(id),
				Start: from,
				End: to,
				Distance: distance,
				Identificator: iden,
				User: user,
				PlaceFrom: placeFrom,
				PlaceTo: placeTo
			};
			trips.push(toPost);
		}
	}
		$.ajax({
			type: "POST",
			data: JSON.stringify(trips),
			url: "/Home/Trip/Overwiev?handler=Download",
			headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (data) {
				console.log(data);
				window.open('/Home/Trip/DownloadTrip');
			},
		})
	
	console.log(trips);
}