class ActivitiesPage { constructor() { this.currentPage = 1; this.pageSize = 25; this.totalPages = 1; this.activities = []; this.filters = {}; this.init(); } init() { this.loadActivities(); this.setupEventListeners(); } async loadActivities() { try { const params = new URLSearchParams({ page: this.currentPage, per_page: this.pageSize, ...this.filters }); const response = await fetch(`/api/activities?${params}`); if (!response.ok) { throw new Error('Failed to load activities'); } const data = await response.json(); this.activities = data.activities; this.totalPages = Math.ceil(data.total / this.pageSize); this.renderTable(); this.renderPagination(); } catch (error) { console.error('Failed to load activities:', error); this.showError('Failed to load activities'); } } renderTable() { const tbody = document.getElementById('activities-tbody'); if (!tbody) return; if (!this.activities || this.activities.length === 0) { tbody.innerHTML = 'No activities found'; return; } tbody.innerHTML = ''; this.activities.forEach((activity, index) => { const row = this.createTableRow(activity, index); tbody.appendChild(row); }); } createTableRow(activity, index) { const row = document.createElement('tr'); row.className = index % 2 === 0 ? 'row-even' : 'row-odd'; row.innerHTML = ` ${Utils.formatDate(activity.start_time)} ${activity.activity_type || '-'} ${Utils.formatDuration(activity.duration)} ${Utils.formatDistance(activity.distance)} ${Utils.formatHeartRate(activity.max_heart_rate)} ${Utils.formatHeartRate(activity.avg_heart_rate)} ${Utils.formatPower(activity.avg_power)} ${activity.calories ? activity.calories.toLocaleString() : '-'} `; return row; } renderPagination() { const pagination = document.getElementById('pagination'); if (!pagination) return; if (this.totalPages <= 1) { pagination.innerHTML = ''; return; } let paginationHtml = ''; // Previous button paginationHtml += `
  • Previous
  • `; // Page numbers for (let i = 1; i <= this.totalPages; i++) { if (i === 1 || i === this.totalPages || (i >= this.currentPage - 2 && i <= this.currentPage + 2)) { paginationHtml += `
  • ${i}
  • `; } else if (i === this.currentPage - 3 || i === this.currentPage + 3) { paginationHtml += '
  • ...
  • '; } } // Next button paginationHtml += `
  • Next
  • `; pagination.innerHTML = paginationHtml; } changePage(page) { if (page < 1 || page > this.totalPages) return; this.currentPage = page; this.loadActivities(); } setupEventListeners() { // We can add filter event listeners here if needed } showError(message) { const tbody = document.getElementById('activities-tbody'); if (tbody) { tbody.innerHTML = `Error: ${message}`; } } } // Initialize activities page when DOM is loaded let activitiesPage; document.addEventListener('DOMContentLoaded', function() { activitiesPage = new ActivitiesPage(); });