2025-07-05 16:01:39 -06:00

578 lines
20 KiB
JavaScript

// Admin panel JavaScript
let adminMap = null;
let startMarker = null;
let storedQRCodes = {};
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
checkAdminAuth();
initializeAdminMap();
loadCurrentStartLocation();
setupEventListeners();
setupNavigation();
loadWalkSheetConfig();
});
// Check if user is authenticated as admin
async function checkAdminAuth() {
try {
const response = await fetch('/api/auth/check');
const data = await response.json();
if (!data.authenticated || !data.user?.isAdmin) {
window.location.href = '/login.html';
return;
}
// Display admin info
document.getElementById('admin-info').innerHTML = `
<span>👤 ${escapeHtml(data.user.email)}</span>
<button id="logout-btn" class="btn btn-secondary btn-sm">Logout</button>
`;
document.getElementById('logout-btn').addEventListener('click', handleLogout);
} catch (error) {
console.error('Auth check failed:', error);
window.location.href = '/login.html';
}
}
// Initialize the admin map
function initializeAdminMap() {
adminMap = L.map('admin-map').setView([53.5461, -113.4938], 11);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 19,
minZoom: 2
}).addTo(adminMap);
// Add click handler to set location
adminMap.on('click', handleMapClick);
// Update coordinates when map moves
adminMap.on('moveend', updateCoordinatesFromMap);
}
// Load current start location
async function loadCurrentStartLocation() {
try {
const response = await fetch('/api/admin/start-location');
const data = await response.json();
if (data.success) {
const { latitude, longitude, zoom } = data.location;
// Update form fields
document.getElementById('start-lat').value = latitude;
document.getElementById('start-lng').value = longitude;
document.getElementById('start-zoom').value = zoom;
// Update map
adminMap.setView([latitude, longitude], zoom);
updateStartMarker(latitude, longitude);
// Show source info
if (data.source) {
const sourceText = data.source === 'database' ? 'Loaded from database' :
data.source === 'environment' ? 'Using environment defaults' :
'Using system defaults';
showStatus(sourceText, 'info');
}
}
} catch (error) {
console.error('Failed to load start location:', error);
showStatus('Failed to load current start location', 'error');
}
}
// Handle map click
function handleMapClick(e) {
const { lat, lng } = e.latlng;
document.getElementById('start-lat').value = lat.toFixed(6);
document.getElementById('start-lng').value = lng.toFixed(6);
updateStartMarker(lat, lng);
}
// Update marker position
function updateStartMarker(lat, lng) {
if (startMarker) {
startMarker.setLatLng([lat, lng]);
} else {
startMarker = L.marker([lat, lng], {
draggable: true,
title: 'Start Location'
}).addTo(adminMap);
// Update coordinates when marker is dragged
startMarker.on('dragend', (e) => {
const position = e.target.getLatLng();
document.getElementById('start-lat').value = position.lat.toFixed(6);
document.getElementById('start-lng').value = position.lng.toFixed(6);
});
}
}
// Update coordinates from current map view
function updateCoordinatesFromMap() {
const center = adminMap.getCenter();
const zoom = adminMap.getZoom();
document.getElementById('start-zoom').value = zoom;
}
// Setup event listeners
function setupEventListeners() {
// Use current view button
document.getElementById('use-current-view').addEventListener('click', () => {
const center = adminMap.getCenter();
const zoom = adminMap.getZoom();
document.getElementById('start-lat').value = center.lat.toFixed(6);
document.getElementById('start-lng').value = center.lng.toFixed(6);
document.getElementById('start-zoom').value = zoom;
updateStartMarker(center.lat, center.lng);
showStatus('Captured current map view', 'success');
});
// Save button
document.getElementById('save-start-location').addEventListener('click', saveStartLocation);
// Coordinate input changes
document.getElementById('start-lat').addEventListener('change', updateMapFromInputs);
document.getElementById('start-lng').addEventListener('change', updateMapFromInputs);
document.getElementById('start-zoom').addEventListener('change', updateMapFromInputs);
// Walk Sheet buttons
document.getElementById('save-walk-sheet').addEventListener('click', saveWalkSheetConfig);
document.getElementById('preview-walk-sheet').addEventListener('click', generateWalkSheetPreview);
document.getElementById('print-walk-sheet').addEventListener('click', printWalkSheet);
document.getElementById('refresh-preview').addEventListener('click', generateWalkSheetPreview);
// Auto-update preview on input change
const walkSheetInputs = document.querySelectorAll(
'#walk-sheet-title, #walk-sheet-subtitle, #walk-sheet-footer, ' +
'[id^="qr-code-"][id$="-url"], [id^="qr-code-"][id$="-label"]'
);
walkSheetInputs.forEach(input => {
input.addEventListener('input', debounce(() => {
generateWalkSheetPreview();
}, 500));
});
// Add URL change listeners to detect when QR codes need regeneration
for (let i = 1; i <= 3; i++) {
const urlInput = document.getElementById(`qr-code-${i}-url`);
let previousUrl = urlInput.value;
urlInput.addEventListener('change', () => {
if (urlInput.value !== previousUrl) {
// URL changed, clear stored QR code
delete storedQRCodes[i];
previousUrl = urlInput.value;
}
});
}
}
// Update map from input fields
function updateMapFromInputs() {
const lat = parseFloat(document.getElementById('start-lat').value);
const lng = parseFloat(document.getElementById('start-lng').value);
const zoom = parseInt(document.getElementById('start-zoom').value);
if (!isNaN(lat) && !isNaN(lng) && !isNaN(zoom)) {
adminMap.setView([lat, lng], zoom);
updateStartMarker(lat, lng);
}
}
// Save start location
async function saveStartLocation() {
const lat = parseFloat(document.getElementById('start-lat').value);
const lng = parseFloat(document.getElementById('start-lng').value);
const zoom = parseInt(document.getElementById('start-zoom').value);
// Validate
if (isNaN(lat) || isNaN(lng) || isNaN(zoom)) {
showStatus('Please enter valid coordinates and zoom level', 'error');
return;
}
if (lat < -90 || lat > 90 || lng < -180 || lng > 180) {
showStatus('Coordinates out of valid range', 'error');
return;
}
if (zoom < 2 || zoom > 19) {
showStatus('Zoom level must be between 2 and 19', 'error');
return;
}
try {
const response = await fetch('/api/admin/start-location', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
latitude: lat,
longitude: lng,
zoom: zoom
})
});
const data = await response.json();
if (data.success) {
showStatus('Start location saved successfully!', 'success');
} else {
throw new Error(data.error || 'Failed to save');
}
} catch (error) {
console.error('Save error:', error);
showStatus(error.message || 'Failed to save start location', 'error');
}
}
// Save walk sheet configuration
async function saveWalkSheetConfig() {
const config = {
walk_sheet_title: document.getElementById('walk-sheet-title').value,
walk_sheet_subtitle: document.getElementById('walk-sheet-subtitle').value,
walk_sheet_footer: document.getElementById('walk-sheet-footer').value,
qr_code_1_url: document.getElementById('qr-code-1-url').value,
qr_code_1_label: document.getElementById('qr-code-1-label').value,
qr_code_2_url: document.getElementById('qr-code-2-url').value,
qr_code_2_label: document.getElementById('qr-code-2-label').value,
qr_code_3_url: document.getElementById('qr-code-3-url').value,
qr_code_3_label: document.getElementById('qr-code-3-label').value
};
// Show loading state
const saveButton = document.getElementById('save-walk-sheet');
const originalText = saveButton.textContent;
saveButton.textContent = 'Saving...';
saveButton.disabled = true;
try {
const response = await fetch('/api/admin/walk-sheet-config', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(config)
});
const data = await response.json();
if (data.success) {
showStatus('Walk sheet configuration saved successfully!', 'success');
// Update stored QR codes if new ones were generated
if (data.qrCodes) {
for (let i = 1; i <= 3; i++) {
if (data.qrCodes[`qr_code_${i}_image`]) {
storedQRCodes[i] = data.qrCodes[`qr_code_${i}_image`];
}
}
}
// Refresh preview with new QR codes
generateWalkSheetPreview();
} else {
throw new Error(data.error || 'Failed to save');
}
} catch (error) {
console.error('Save error:', error);
showStatus(error.message || 'Failed to save walk sheet configuration', 'error');
} finally {
saveButton.textContent = originalText;
saveButton.disabled = false;
}
}
// Generate walk sheet preview
function generateWalkSheetPreview() {
const title = document.getElementById('walk-sheet-title').value || 'Campaign Walk Sheet';
const subtitle = document.getElementById('walk-sheet-subtitle').value || 'Door-to-Door Canvassing Form';
const footer = document.getElementById('walk-sheet-footer').value || 'Thank you for your support!';
let previewHTML = `
<div class="ws-header">
<h1 class="ws-title">${escapeHtml(title)}</h1>
<p class="ws-subtitle">${escapeHtml(subtitle)}</p>
</div>
`;
// Add QR codes section
const qrCodesHTML = [];
for (let i = 1; i <= 3; i++) {
const url = document.getElementById(`qr-code-${i}-url`).value;
const label = document.getElementById(`qr-code-${i}-label`).value;
if (url) {
// Check if we have a stored QR code image
if (storedQRCodes[i] && storedQRCodes[i].url) {
// Use stored QR code image
qrCodesHTML.push(`
<div class="ws-qr-item">
<div class="ws-qr-code">
<img src="${storedQRCodes[i].url}" alt="QR Code ${i}" style="width: 80px; height: 80px;">
</div>
<div class="ws-qr-label">${escapeHtml(label) || `QR Code ${i}`}</div>
</div>
`);
} else {
// Generate QR code client-side as fallback
qrCodesHTML.push(`
<div class="ws-qr-item">
<div class="ws-qr-code" id="preview-qr-${i}"></div>
<div class="ws-qr-label">${escapeHtml(label) || `QR Code ${i}`}</div>
</div>
`);
}
}
}
if (qrCodesHTML.length > 0) {
previewHTML += `
<div class="ws-qr-section">
${qrCodesHTML.join('')}
</div>
`;
}
// Add form fields based on the main map form
previewHTML += `
<div class="ws-form-section">
<div class="ws-form-row">
<div class="ws-form-group">
<label class="ws-form-label">First Name</label>
<div class="ws-form-field"></div>
</div>
<div class="ws-form-group">
<label class="ws-form-label">Last Name</label>
<div class="ws-form-field"></div>
</div>
</div>
<div class="ws-form-row">
<div class="ws-form-group">
<label class="ws-form-label">Email</label>
<div class="ws-form-field"></div>
</div>
<div class="ws-form-group">
<label class="ws-form-label">Phone</label>
<div class="ws-form-field"></div>
</div>
</div>
<div class="ws-form-row">
<div class="ws-form-group">
<label class="ws-form-label">Address</label>
<div class="ws-form-field"></div>
</div>
<div class="ws-form-group">
<label class="ws-form-label">Unit Number</label>
<div class="ws-form-field"></div>
</div>
</div>
<div class="ws-form-row">
<div class="ws-form-group">
<label class="ws-form-label">Support Level (1-4)</label>
<div class="ws-form-field"></div>
</div>
<div class="ws-form-group">
<label class="ws-form-label">Sign Request ☐ Yes ☐ No</label>
<div class="ws-form-field"></div>
</div>
</div>
<div class="ws-form-row">
<div class="ws-form-group">
<label class="ws-form-label">Category</label>
<div class="ws-form-field"></div>
</div>
<div class="ws-form-group">
<label class="ws-form-label">Visited Date</label>
<div class="ws-form-field"></div>
</div>
</div>
</div>
<div class="ws-notes-section">
<div class="ws-notes-label">Notes & Comments</div>
<div class="ws-notes-area"></div>
</div>
`;
// Update preview
const previewContent = document.getElementById('walk-sheet-preview-content');
previewContent.innerHTML = previewHTML;
// Add footer (positioned absolutely in CSS)
if (footer) {
const footerDiv = document.createElement('div');
footerDiv.className = 'ws-footer';
footerDiv.innerHTML = escapeHtml(footer);
previewContent.appendChild(footerDiv);
}
// Generate client-side QR codes for items without stored images
setTimeout(() => {
for (let i = 1; i <= 3; i++) {
const url = document.getElementById(`qr-code-${i}-url`).value;
if (url && !storedQRCodes[i]) {
const qrContainer = document.getElementById(`preview-qr-${i}`);
if (qrContainer && typeof QRCode !== 'undefined') {
qrContainer.innerHTML = '';
new QRCode(qrContainer, {
text: url,
width: 80,
height: 80,
correctLevel: QRCode.CorrectLevel.M
});
}
}
}
}, 100);
}
// Print walk sheet
function printWalkSheet() {
// First generate fresh preview
generateWalkSheetPreview();
// Wait for QR codes to generate
setTimeout(() => {
window.print();
}, 500);
}
// Setup navigation between sections
function setupNavigation() {
const navLinks = document.querySelectorAll('.admin-nav a');
const sections = document.querySelectorAll('.admin-section');
navLinks.forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const targetId = link.getAttribute('href').substring(1);
// Update active states
navLinks.forEach(l => l.classList.remove('active'));
link.classList.add('active');
// Show/hide sections
sections.forEach(section => {
section.style.display = section.id === targetId ? 'block' : 'none';
});
});
});
}
// Load walk sheet configuration
async function loadWalkSheetConfig() {
try {
const response = await fetch('/api/admin/walk-sheet-config');
const data = await response.json();
if (data.success && data.config) {
// Populate form fields
document.getElementById('walk-sheet-title').value = data.config.walk_sheet_title || '';
document.getElementById('walk-sheet-subtitle').value = data.config.walk_sheet_subtitle || '';
document.getElementById('walk-sheet-footer').value = data.config.walk_sheet_footer || '';
// QR codes
for (let i = 1; i <= 3; i++) {
document.getElementById(`qr-code-${i}-url`).value = data.config[`qr_code_${i}_url`] || '';
document.getElementById(`qr-code-${i}-label`).value = data.config[`qr_code_${i}_label`] || '';
// Store QR code image data if available
if (data.config[`qr_code_${i}_image`]) {
storedQRCodes[i] = data.config[`qr_code_${i}_image`];
}
}
// Generate preview
generateWalkSheetPreview();
}
} catch (error) {
console.error('Failed to load walk sheet config:', error);
}
}
// Handle logout
async function handleLogout() {
if (!confirm('Are you sure you want to logout?')) {
return;
}
try {
const response = await fetch('/api/auth/logout', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (response.ok) {
window.location.href = '/login.html';
} else {
showStatus('Logout failed. Please try again.', 'error');
}
} catch (error) {
console.error('Logout error:', error);
showStatus('Logout failed. Please try again.', 'error');
}
}
// Show status message
function showStatus(message, type = 'info') {
const container = document.getElementById('status-container');
const messageDiv = document.createElement('div');
messageDiv.className = `status-message ${type}`;
messageDiv.textContent = message;
container.appendChild(messageDiv);
// Auto-remove after 5 seconds
setTimeout(() => {
messageDiv.remove();
}, 5000);
}
// Escape HTML
function escapeHtml(text) {
if (text === null || text === undefined) {
return '';
}
const div = document.createElement('div');
div.textContent = String(text);
return div.innerHTML;
}
// Debounce function for input events
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}