File size: 27,618 Bytes
bf8ba08 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
// WiFi Attendance Tracker - Advanced JavaScript
class AttendanceTracker {
constructor() {
this.isMonitoring = false;
this.updateInterval = null;
this.employees = [];
this.events = [];
this.summary = [];
this.searchQuery = '';
this.init();
}
init() {
this.setupEventListeners();
this.loadInitialData();
this.startAutoUpdate();
this.updateCurrentTime();
// Set today's date in summary date input
const today = new Date().toISOString().split('T')[0];
document.getElementById('summaryDate').value = today;
}
setupEventListeners() {
// Control buttons
document.getElementById('startBtn').addEventListener('click', () => this.startMonitoring());
document.getElementById('stopBtn').addEventListener('click', () => this.stopMonitoring());
document.getElementById('refreshBtn').addEventListener('click', () => this.refreshData());
document.getElementById('exportBtn').addEventListener('click', () => this.exportCSV());
// Search functionality
document.getElementById('searchBtn').addEventListener('click', () => this.performSearch());
document.getElementById('clearSearchBtn').addEventListener('click', () => this.clearSearch());
document.getElementById('searchInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') this.performSearch();
});
document.getElementById('searchInput').addEventListener('input', (e) => {
if (e.target.value === '') this.clearSearch();
});
// Modal buttons
document.getElementById('addEmployeeBtn').addEventListener('click', () => this.openModal('addEmployeeModal'));
document.getElementById('settingsBtn').addEventListener('click', () => this.openModal('settingsModal'));
// Form submissions
document.getElementById('addEmployeeForm').addEventListener('submit', (e) => this.handleAddEmployee(e));
document.getElementById('changePasswordForm').addEventListener('submit', (e) => this.handleChangePassword(e));
document.getElementById('deleteEmployeeForm').addEventListener('submit', (e) => this.handleDeleteEmployee(e));
document.getElementById('modifyEmployeeForm').addEventListener('submit', (e) => this.handleModifyEmployee(e));
// Summary date change
document.getElementById('summaryDate').addEventListener('change', () => this.loadDailySummary());
document.getElementById('summaryRefreshBtn').addEventListener('click', () => this.loadDailySummary());
// Event date filter
document.getElementById('eventDateFilter').addEventListener('change', () => this.loadEvents());
// Modal close buttons
document.querySelectorAll('.modal-close').forEach(btn => {
btn.addEventListener('click', (e) => {
const modal = e.target.closest('.modal');
this.closeModal(modal.id);
});
});
// Close modal when clicking outside
document.querySelectorAll('.modal').forEach(modal => {
modal.addEventListener('click', (e) => {
if (e.target === modal) {
this.closeModal(modal.id);
}
});
});
}
async loadInitialData() {
await Promise.all([
this.loadSystemStatus(),
this.loadEmployees(),
this.loadEvents(),
this.loadDailySummary(),
this.loadSummaryStats()
]);
}
async loadSystemStatus() {
try {
const response = await fetch('/api/status');
const status = await response.json();
document.getElementById('systemStatus').textContent = status.is_monitoring ? 'Monitoring' : 'Stopped';
document.getElementById('systemStatus').className = `status-value ${status.is_monitoring ? 'monitoring' : 'stopped'}`;
document.getElementById('employeeCount').textContent = status.employee_count;
document.getElementById('scanInterval').textContent = `${status.scan_interval} seconds`;
document.getElementById('officeTimeout').textContent = status.office_timeout;
this.isMonitoring = status.is_monitoring;
this.updateControlButtons();
} catch (error) {
console.error('Error loading system status:', error);
this.showNotification('Error loading system status', 'error');
}
}
async loadEmployees() {
try {
const response = await fetch('/api/employees');
this.employees = await response.json();
this.renderEmployees();
} catch (error) {
console.error('Error loading employees:', error);
this.showNotification('Error loading employees', 'error');
}
}
async loadEvents() {
try {
const dateFilter = document.getElementById('eventDateFilter').value;
const url = dateFilter ? `/api/attendance_events?date=${dateFilter}&limit=50` : '/api/attendance_events?limit=50';
const response = await fetch(url);
this.events = await response.json();
this.renderEvents();
} catch (error) {
console.error('Error loading events:', error);
this.showNotification('Error loading events', 'error');
}
}
async loadDailySummary() {
try {
const date = document.getElementById('summaryDate').value;
const response = await fetch(`/api/daily_summary?date=${date}`);
this.summary = await response.json();
this.renderDailySummary();
} catch (error) {
console.error('Error loading daily summary:', error);
this.showNotification('Error loading daily summary', 'error');
}
}
async loadSummaryStats() {
try {
const date = document.getElementById('summaryDate').value;
const response = await fetch(`/api/summary_stats?date=${date}`);
const stats = await response.json();
document.getElementById('presentCount').textContent = stats.present_count || 0;
document.getElementById('absentCount').textContent = stats.absent_count || 0;
document.getElementById('breakCount').textContent = stats.on_break_count || 0;
document.getElementById('timeoutCount').textContent = stats.timed_out_count || 0;
} catch (error) {
console.error('Error loading summary stats:', error);
}
}
renderEmployees() {
const container = document.getElementById('employeeList');
if (this.employees.length === 0) {
container.innerHTML = '<div class="loading">No employees found</div>';
return;
}
// Filter employees based on search query
const filteredEmployees = this.searchQuery
? this.employees.filter(emp =>
emp.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
emp.mac.toLowerCase().includes(this.searchQuery.toLowerCase())
)
: this.employees;
if (filteredEmployees.length === 0) {
container.innerHTML = '<div class="loading">No employees match your search</div>';
return;
}
container.innerHTML = filteredEmployees.map(employee => `
<div class="employee-card" onclick="attendanceTracker.showEmployeeDetails('${employee.mac}')">
<div class="employee-avatar">
${employee.picture ?
`<img src="${employee.picture}" alt="${employee.name}" onerror="this.style.display='none'; this.parentNode.textContent='${employee.name.charAt(0).toUpperCase()}'">` :
employee.name.charAt(0).toUpperCase()
}
</div>
<div class="employee-info">
<div class="employee-name">${employee.name}</div>
<div class="employee-mac">${employee.mac}</div>
</div>
<div class="employee-status">
<span class="status-badge ${employee.status.toLowerCase().replace(' ', '')}">${employee.status}</span>
<div class="employee-time">
${employee.time_in !== 'N/A' ? `In: ${employee.time_in}` : 'Not checked in'}
</div>
</div>
</div>
`).join('');
}
renderEvents() {
const container = document.getElementById('eventsList');
if (this.events.length === 0) {
container.innerHTML = '<div class="loading">No recent events</div>';
return;
}
container.innerHTML = this.events.map(event => `
<div class="event-item ${event.event_type}">
<div class="event-icon">
<i class="fas ${this.getEventIcon(event.event_type)}"></i>
</div>
<div class="event-content">
<div class="event-name">${event.employee_name}</div>
<div class="event-type">${this.formatEventType(event.event_type)}</div>
</div>
<div class="event-time">${event.time_ago}</div>
</div>
`).join('');
}
renderDailySummary() {
const container = document.getElementById('summaryTable');
if (this.summary.length === 0) {
container.innerHTML = '<div class="loading">No summary data for selected date</div>';
return;
}
container.innerHTML = `
<table>
<thead>
<tr>
<th>Employee</th>
<th>Time In</th>
<th>Time Out</th>
<th>Work Duration</th>
<th>Break Duration</th>
<th>Status</th>
</tr>
</thead>
<tbody>
${this.summary.map(emp => `
<tr>
<td>
<div style="display: flex; align-items: center; gap: 8px;">
<div class="employee-avatar" style="width: 32px; height: 32px; font-size: 12px;">
${emp.name.charAt(0).toUpperCase()}
</div>
<div>
<div style="font-weight: 600;">${emp.name}</div>
<div style="font-size: 11px; color: var(--text-muted);">${emp.mac_address}</div>
</div>
</div>
</td>
<td>${emp.time_in || 'N/A'}</td>
<td>${emp.time_out || 'N/A'}</td>
<td>${emp.total_work_formatted}</td>
<td>${emp.total_break_formatted}</td>
<td><span class="status-badge ${emp.status.toLowerCase().replace(' ', '')}">${emp.status}</span></td>
</tr>
`).join('')}
</tbody>
</table>
`;
}
getEventIcon(eventType) {
const icons = {
'time_in': 'fa-sign-in-alt',
'time_out': 'fa-sign-out-alt',
'break_start': 'fa-coffee',
'break_end': 'fa-play',
'timeout_5pm': 'fa-clock'
};
return icons[eventType] || 'fa-circle';
}
formatEventType(eventType) {
const formats = {
'time_in': 'Time In',
'time_out': 'Time Out',
'break_start': 'Break Start',
'break_end': 'Break End',
'timeout_5pm': '5 PM Timeout'
};
return formats[eventType] || eventType;
}
async startMonitoring() {
try {
const response = await fetch('/api/start_monitoring', { method: 'POST' });
const result = await response.json();
if (result.success) {
this.isMonitoring = true;
this.updateControlButtons();
this.showNotification('Monitoring started successfully', 'success');
await this.loadSystemStatus();
} else {
this.showNotification(result.message, 'error');
}
} catch (error) {
console.error('Error starting monitoring:', error);
this.showNotification('Error starting monitoring', 'error');
}
}
async stopMonitoring() {
try {
const response = await fetch('/api/stop_monitoring', { method: 'POST' });
const result = await response.json();
if (result.success) {
this.isMonitoring = false;
this.updateControlButtons();
this.showNotification('Monitoring stopped', 'warning');
await this.loadSystemStatus();
} else {
this.showNotification(result.message, 'error');
}
} catch (error) {
console.error('Error stopping monitoring:', error);
this.showNotification('Error stopping monitoring', 'error');
}
}
async refreshData() {
document.getElementById('lastUpdate').textContent = new Date().toLocaleTimeString();
await this.loadInitialData();
this.showNotification('Data refreshed', 'success');
}
async exportCSV() {
try {
const date = document.getElementById('summaryDate').value;
const response = await fetch(`/api/export_csv?date=${date}`);
const result = await response.json();
if (result.success) {
this.showNotification('CSV exported successfully', 'success');
} else {
this.showNotification(result.message, 'error');
}
} catch (error) {
console.error('Error exporting CSV:', error);
this.showNotification('Error exporting CSV', 'error');
}
}
performSearch() {
this.searchQuery = document.getElementById('searchInput').value.trim();
this.renderEmployees();
if (this.searchQuery) {
this.showNotification(`Searching for: ${this.searchQuery}`, 'info');
}
}
clearSearch() {
this.searchQuery = '';
document.getElementById('searchInput').value = '';
this.renderEmployees();
}
updateControlButtons() {
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
startBtn.disabled = this.isMonitoring;
stopBtn.disabled = !this.isMonitoring;
}
startAutoUpdate() {
// Update data every 10 seconds
this.updateInterval = setInterval(() => {
if (this.isMonitoring) {
this.loadEmployees();
this.loadEvents();
this.loadSummaryStats();
}
}, 10000);
}
updateCurrentTime() {
const updateTime = () => {
const now = new Date();
document.getElementById('currentTime').textContent = now.toLocaleTimeString();
};
updateTime();
setInterval(updateTime, 1000);
}
openModal(modalId) {
const modal = document.getElementById(modalId);
modal.classList.add('show');
// Load settings data if opening settings modal
if (modalId === 'settingsModal') {
this.loadSystemStatus();
}
}
closeModal(modalId) {
const modal = document.getElementById(modalId);
modal.classList.remove('show');
// Reset forms
const forms = modal.querySelectorAll('form');
forms.forEach(form => form.reset());
}
async handleAddEmployee(e) {
e.preventDefault();
const formData = new FormData(e.target);
const employeeData = {
name: formData.get('name'),
mac: formData.get('mac').toLowerCase(),
picture: formData.get('picture'),
password: formData.get('password')
};
try {
const response = await fetch('/api/add_employee', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(employeeData)
});
const result = await response.json();
if (result.success) {
this.showNotification('Employee added successfully', 'success');
this.closeModal('addEmployeeModal');
await this.loadEmployees();
await this.loadSystemStatus();
} else {
this.showNotification(result.message, 'error');
}
} catch (error) {
console.error('Error adding employee:', error);
this.showNotification('Error adding employee', 'error');
}
}
async handleChangePassword(e) {
e.preventDefault();
const formData = new FormData(e.target);
const newPassword = formData.get('newPassword');
const confirmPassword = formData.get('confirmPassword');
if (newPassword !== confirmPassword) {
this.showNotification('Passwords do not match', 'error');
return;
}
const passwordData = {
currentPassword: formData.get('currentPassword'),
newPassword: newPassword
};
try {
const response = await fetch('/api/change_password', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(passwordData)
});
const result = await response.json();
if (result.success) {
this.showNotification('Password changed successfully', 'success');
this.closeModal('settingsModal');
} else {
this.showNotification(result.message, 'error');
}
} catch (error) {
console.error('Error changing password:', error);
this.showNotification('Error changing password', 'error');
}
}
showEmployeeDetails(mac) {
const employee = this.employees.find(emp => emp.mac === mac);
if (!employee) return;
const modal = document.getElementById('employeeDetailsModal');
const content = document.getElementById('employeeDetailsContent');
content.innerHTML = `
<div style="text-align: center; margin-bottom: 20px;">
<div class="employee-avatar" style="width: 80px; height: 80px; font-size: 32px; margin: 0 auto 12px;">
${employee.picture ?
`<img src="${employee.picture}" alt="${employee.name}" style="width: 100%; height: 100%; object-fit: cover;">` :
employee.name.charAt(0).toUpperCase()
}
</div>
<h3>${employee.name}</h3>
<p style="color: var(--text-muted); font-family: monospace;">${employee.mac}</p>
</div>
<div class="info-grid">
<div class="info-item">
<span class="info-label">Current Status:</span>
<span class="status-badge ${employee.status.toLowerCase().replace(' ', '')}">${employee.status}</span>
</div>
<div class="info-item">
<span class="info-label">Time In:</span>
<span class="info-value">${employee.time_in}</span>
</div>
<div class="info-item">
<span class="info-label">Last Seen:</span>
<span class="info-value">${employee.last_seen}</span>
</div>
<div class="info-item">
<span class="info-label">Is Present:</span>
<span class="info-value">${employee.is_present ? 'Yes' : 'No'}</span>
</div>
</div>
`;
this.openModal('employeeDetailsModal');
// Store current employee for delete/modify operations
this.currentEmployee = employee;
// Add event listeners for delete and modify buttons
document.getElementById('deleteEmployeeBtn').onclick = () => this.openDeleteEmployeeModal();
document.getElementById('modifyEmployeeBtn').onclick = () => this.openModifyEmployeeModal();
}
openDeleteEmployeeModal() {
if (!this.currentEmployee) return;
// Populate delete employee info
document.getElementById('deleteEmployeeInfo').innerHTML = `
<div style="text-align: center; padding: 15px; background: #fee; border: 1px solid #fcc; border-radius: 8px;">
<h4>${this.currentEmployee.name}</h4>
<p style="font-family: monospace; color: #666;">${this.currentEmployee.mac}</p>
<p style="color: #999;">Status: ${this.currentEmployee.status}</p>
</div>
`;
// Clear password field
document.getElementById('deleteAdminPassword').value = '';
this.closeModal('employeeDetailsModal');
this.openModal('deleteEmployeeModal');
}
openModifyEmployeeModal() {
if (!this.currentEmployee) return;
// Pre-fill the form with current employee data
document.getElementById('modifyEmployeeName').value = this.currentEmployee.name;
document.getElementById('modifyEmployeeMac').value = this.currentEmployee.mac;
document.getElementById('modifyEmployeePicture').value = this.currentEmployee.picture || '';
document.getElementById('modifyAdminPassword').value = '';
this.closeModal('employeeDetailsModal');
this.openModal('modifyEmployeeModal');
}
async handleDeleteEmployee(e) {
e.preventDefault();
if (!this.currentEmployee) {
this.showNotification('No employee selected', 'error');
return;
}
const formData = new FormData(e.target);
const password = formData.get('password');
if (!password) {
this.showNotification('Please enter admin password', 'error');
return;
}
try {
const response = await fetch('/api/delete_employee', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
employee_id: this.getEmployeeIdByMac(this.currentEmployee.mac),
password: password
})
});
const result = await response.json();
if (result.success) {
this.showNotification(result.message, 'success');
this.closeModal('deleteEmployeeModal');
await this.loadEmployees();
await this.loadSummaryStats();
} else {
this.showNotification(result.message, 'error');
}
} catch (error) {
console.error('Error deleting employee:', error);
this.showNotification('Error deleting employee', 'error');
}
}
async handleModifyEmployee(e) {
e.preventDefault();
if (!this.currentEmployee) {
this.showNotification('No employee selected', 'error');
return;
}
const formData = new FormData(e.target);
const name = formData.get('name');
const mac_address = formData.get('mac_address');
const picture_path = formData.get('picture_path');
const password = formData.get('password');
if (!password) {
this.showNotification('Please enter admin password', 'error');
return;
}
if (!name || !mac_address) {
this.showNotification('Please fill in all required fields', 'error');
return;
}
try {
const response = await fetch('/api/modify_employee', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
employee_id: this.getEmployeeIdByMac(this.currentEmployee.mac),
name: name,
mac_address: mac_address,
picture_path: picture_path,
password: password
})
});
const result = await response.json();
if (result.success) {
this.showNotification(result.message, 'success');
this.closeModal('modifyEmployeeModal');
await this.loadEmployees();
await this.loadSummaryStats();
} else {
this.showNotification(result.message, 'error');
}
} catch (error) {
console.error('Error modifying employee:', error);
this.showNotification('Error modifying employee', 'error');
}
}
getEmployeeIdByMac(mac) {
// Since we don't have employee ID in the frontend data, we'll need to get it from the API
// For now, we'll use the MAC address as identifier and let the backend handle the ID lookup
return mac;
}
showNotification(message, type = 'info') {
const container = document.getElementById('notifications');
const notification = document.createElement('div');
notification.className = `notification ${type}`;
const title = type.charAt(0).toUpperCase() + type.slice(1);
notification.innerHTML = `
<div class="notification-title">${title}</div>
<div class="notification-message">${message}</div>
`;
container.appendChild(notification);
// Auto remove after 5 seconds
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
}, 5000);
}
}
// Global functions for modal management
function closeModal(modalId) {
if (window.attendanceTracker) {
window.attendanceTracker.closeModal(modalId);
}
}
// Initialize the application when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
window.attendanceTracker = new AttendanceTracker();
});
|