';
$.each(data.errors, function (field, message) {
// $('#add_device_form #' + field).addClass('is-invalid');
$(".selectpicker").selectpicker("refresh");
html += "
" + message + "
";
});
html += "
";
displayErrorMessage(html);
}
if (data.success) {
$(".selectpicker").selectpicker("refresh");
html = '
' + data.success + "
";
displaySuccessMessage(html);
$("#add_device_form")[0].reset();
$("#employee_devices-table").DataTable().ajax.reload();
}
},
});
});
{{-- Restart --}}
$(document).on("click", ".restart_device", function () {
let device_id = $(this).attr("id");
{{-- let employee = "{{$employee->id}}"; --}}
if (confirm('{{__("Are you sure you want to restart the device?")}}')) {
$.ajax({
url: "{{ route('devices.restartDevice') }}",
method: "POST",
data: {
{{-- employee: employee, --}}
device_id: device_id,
},
success: function (data) {
var html = "";
html = '
' + data.success + "
";
displaySuccessMessage(html);
$("#employee_devices-table").DataTable().ajax.reload();
},
});
}
});
$(document).on("click", ".device_delete", function () {
let delete_id = $(this).attr("id");
let employee = "{{$employee->id}}";
if (confirm('{{__("Are You Sure you want to delete this data")}}')) {
$.ajax({
url: "{{ route('employee_devices.delete_from_device') }}",
method: "POST",
data: {
employee: employee,
device: delete_id,
},
success: function (data) {
var html = "";
html = '
' + data.success + "
";
displaySuccessMessage(html);
$("#employee_devices-table").DataTable().ajax.reload();
},
});
}
});
$(document).on("click", ".add_fingerprint", function () {
let device_id = $(this).attr("id");
let employee = "{{$employee->id}}";
$("#employee_fingerprint_modal").modal("show");
$("#employee_fingerprint_modal svg .finger.index").removeClass(
"activeFinger"
);
let submitFingerButton = $(".submit-finger-button");
let fingerIndex;
$("#employee_fingerprint_modal svg .finger.index").on("click", function () {
let index = $(this).attr("data-index");
// Check if corresponding .finger-icon already has activeFinger from DB
let isTaken = $('.finger-icon[data-index="' + index + '"]').hasClass(
"activeFinger"
);
if (isTaken) {
// Block interaction
console.log("This finger is already registered.");
return;
}
// Deselect any previously selected
$("#employee_fingerprint_modal svg .finger.index").removeClass(
"activeFinger"
);
// Add active class to the selected one
$(this).addClass("activeFinger");
// Update selected index
fingerIndex = index;
submitFingerButton.prop("disabled", false);
});
// First AJAX call to fetch the current finger indexes
$.ajax({
url: "{{ route('employee_devices.get_finger_index') }}",
method: "POST",
data: {
employee: employee,
device: device_id,
fingerIndex: fingerIndex,
},
success: function (data) {
if (data.fingerindexes && Array.isArray(data.fingerindexes)) {
// Remove all current highlights
$(".finger-icon").removeClass("activeFinger");
// Highlight received finger indexes
data.fingerindexes.forEach(function (index) {
$('.finger-icon[data-index="' + index + '"]').addClass(
"activeFinger"
);
});
}
},
});
submitFingerButton.on("click", function () {
var fingerscanLoader = $("#employee_fingerprint_modal .loading-fingerscan");
fingerscanLoader.addClass("activated");
$.ajax({
url: "{{ route('employee_devices.add_finger_index') }}",
method: "POST",
data: {
employee: employee,
device: device_id,
fingerIndex: fingerIndex,
},
success: function (data) {
var html = "";
if (data.error) {
displayErrorMessage(data.error);
}
if (data.errors) {
html += '
';
$.each(data.errors, function (message) {
// $('#add_device_form #' + field).addClass('is-invalid');
$(".selectpicker").selectpicker("refresh");
html += "
" + message + "
";
});
html += "
";
}
if (data.success) {
// Start checking the finger index status every 10 seconds
let checkInterval = setInterval(function () {
$.ajax({
url: "{{ route('employee_devices.check_finger_index') }}",
method: "POST",
data: {
employee: employee,
device: device_id,
fingerIndex: fingerIndex,
},
success: function (data) {
// If the finger index is successfully added, add the activeFinger class
if (data.status === "success") {
// Clear the interval to stop checking
clearInterval(checkInterval);
// Add the active class to the selected finger icon
$('.finger-icon[data-index="' + fingerIndex + '"]').addClass(
"activeFinger"
);
fingerscanLoader.removeClass("activated");
$(
'#employee_fingerprint_modal svg .finger.index[data-index="' +
fingerIndex +
'"]'
).removeClass("activeFinger");
displaySuccessMessage(data.message);
submitFingerButton.prop("disabled", true);
fingerIndex = "";
}
if (data.status === "error") {
// Clear the interval to stop checking
clearInterval(checkInterval);
fingerscanLoader.removeClass("activated");
$(
'#employee_fingerprint_modal svg .finger.index[data-index="' +
fingerIndex +
'"]'
).removeClass("activeFinger");
displayErrorMessage(data.message);
submitFingerButton.prop("disabled", true);
fingerIndex = "";
}
},
});
}, 5000); // Every 5 seconds
}
},
});
});
});