`;
overallSummaryDiv.innerHTML = summaryHtml;
}
/**
* Renders the table of all direct mail campaigns.
*/
function renderCampaignsTable() {
if (!campaignsTableBody) return; // Null check
campaignsTableBody.innerHTML = ''; // Clear existing rows
// Sort data by sent date in descending order for display
const sortedCampaigns = [...campaigns].sort((a, b) => new Date(b.sentDate) - new Date(a.sentDate));
sortedCampaigns.forEach(campaign => {
const responseRate = calculateResponseRate(campaign.responseCount, campaign.quantitySent);
const roi = calculateROI(campaign.revenueGenerated, campaign.campaignCost);
const row = campaignsTableBody.insertRow();
row.innerHTML = `
`;
});
}
/**
* Renders the entire dashboard content.
*/
function renderDashboard() {
renderOverallSummary();
renderCampaignsTable();
}
// --- Data Configuration Logic ---
/**
* Generates a unique ID for a new campaign entry.
* @returns {string} A unique ID.
*/
function generateUniqueId() {
return 'dmc' + Date.now() + Math.floor(Math.random() * 1000);
}
// Add/Update Campaign Form Submission
if (addUpdateCampaignForm) {
addUpdateCampaignForm.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
const id = document.getElementById('campaignId').value;
const campaignName = document.getElementById('campaignName').value;
const sentDate = document.getElementById('sentDate').value;
const quantitySent = parseInt(document.getElementById('quantitySent').value);
const responseCount = parseInt(document.getElementById('responseCount').value);
const revenueGenerated = parseFloat(document.getElementById('revenueGenerated').value);
const campaignCost = parseFloat(document.getElementById('campaignCost').value);
const campaignStatus = document.getElementById('campaignStatus').value;
// Basic validation
if (!campaignName.trim()) {
showCustomModal('Input Error', 'Campaign Name cannot be empty.');
return;
}
if (isNaN(quantitySent) || quantitySent < 0 || isNaN(responseCount) || responseCount < 0 ||
isNaN(revenueGenerated) || revenueGenerated < 0 || isNaN(campaignCost) || campaignCost < 0) {
showCustomModal('Input Error', 'Please ensure all numerical fields are filled correctly with non-negative numbers.');
return;
}
if (responseCount > quantitySent) {
showCustomModal('Input Error', 'Response Count cannot be greater than Quantity Sent.');
return;
}
const newCampaign = {
id: id || generateUniqueId(), // Use existing ID if editing, otherwise generate new
campaignName: campaignName,
sentDate: sentDate,
quantitySent: quantitySent,
responseCount: responseCount,
revenueGenerated: revenueGenerated,
campaignCost: campaignCost,
status: campaignStatus
};
if (id) {
// Update existing entry
const index = campaigns.findIndex(entry => entry.id === id);
if (index > -1) {
campaigns[index] = newCampaign;
} else {
console.error('Campaign data entry not found for update:', id);
}
} else {
// Add new entry
campaigns.push(newCampaign);
}
renderDashboard(); // Re-render dashboard with new/updated data
addUpdateCampaignForm.reset(); // Clear the form
campaignIdInput.value = ''; // Clear hidden ID
formSubmitBtn.textContent = 'Add Campaign Data'; // Reset button text
switchTab('dashboard'); // Switch back to dashboard to see changes
});
}
/**
* Populates the form for editing an existing campaign entry.
* @param {string} id - The ID of the campaign entry to edit.
*/
window.editCampaign = function(id) {
const entryToEdit = campaigns.find(entry => entry.id === id);
if (!entryToEdit) {
console.error('Campaign data entry not found for editing:', id);
return;
}
document.getElementById('campaignId').value = entryToEdit.id;
document.getElementById('campaignName').value = entryToEdit.campaignName;
document.getElementById('sentDate').value = entryToEdit.sentDate;
document.getElementById('quantitySent').value = entryToEdit.quantitySent;
document.getElementById('responseCount').value = entryToEdit.responseCount;
document.getElementById('revenueGenerated').value = entryToEdit.revenueGenerated;
document.getElementById('campaignCost').value = entryToEdit.campaignCost;
document.getElementById('campaignStatus').value = entryToEdit.status;
formSubmitBtn.textContent = 'Update Campaign Data'; // Change button text
switchTab('data-config'); // Switch to data config tab
};
/**
* Deletes a campaign entry from the array after confirmation.
* @param {string} id - The ID of the campaign entry to delete.
*/
window.deleteCampaign = function(id) {
showCustomModal('Confirm Deletion', 'Are you sure you want to delete this campaign data entry?', () => {
campaigns = campaigns.filter(entry => entry.id !== id);
renderDashboard(); // Re-render dashboard
});
};
// --- Custom Modal Logic (replaces alert/confirm) ---
let currentConfirmCallback = null;
/**
* Shows a custom modal with a message and optional confirmation.
* @param {string} title - The title of the modal.
* @param {string} message - The message to display.
* @param {function} [onConfirm] - Callback function to execute on confirm. If null, it's an alert.
*/
function showCustomModal(title, message, onConfirm = null) {
if (!confirmModal || !modalTitle || !modalMessage || !confirmActionBtn) return;
modalTitle.textContent = title;
modalMessage.textContent = message;
if (onConfirm) {
confirmActionBtn.style.display = 'inline-block'; // Show confirm button
confirmActionBtn.textContent = 'Confirm';
confirmActionBtn.classList.remove('btn-primary');
confirmActionBtn.classList.add('btn-danger');
currentConfirmCallback = onConfirm;
} else {
confirmActionBtn.style.display = 'none'; // Hide confirm button for alerts
currentConfirmCallback = null;
}
confirmModal.style.display = 'flex'; // Show modal
}
/**
* Closes the specified modal.
* @param {string} modalId - The ID of the modal to close.
*/
window.closeModal = function(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.style.display = 'none';
}
};
// Event listener for the custom modal's confirm button
if (confirmActionBtn) {
confirmActionBtn.addEventListener('click', function() {
if (currentConfirmCallback) {
currentConfirmCallback();
}
closeModal('confirmModal');
});
}
// Close modal when clicking outside of it
window.onclick = function(event) {
if (event.target === confirmModal) {
closeModal('confirmModal');
}
};
// --- PDF Download Logic ---
/**
* Downloads the dashboard content as a PDF.
*/
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', function() {
const input = document.getElementById('dashboardTabContent'); // Target the dashboard content
if (!input) {
console.error('Dashboard content element not found for PDF generation.');
return;
}
// Temporarily hide elements that should not be in the PDF
const elementsToHide = input.querySelectorAll('.no-print');
elementsToHide.forEach(el => el.style.display = 'none');
// Use html2canvas to capture the content as an image
html2canvas(input, {
scale: 2, // Increase scale for better quality
useCORS: true, // Needed if external images are used (not in this case, but good practice)
logging: false // Disable logging for cleaner console
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new window.jspdf.jsPDF('p', 'mm', 'a4'); // 'p' for portrait, 'mm' for units, 'a4' for size
const imgWidth = 210; // A4 width in mm
const pageHeight = 297; // A4 height in mm
const imgHeight = canvas.height * imgWidth / canvas.width;
let heightLeft = imgHeight;
let position = 0;
// Add image to PDF
pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
// If content spans multiple pages, add new pages
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
pdf.save('Direct_Mail_Campaign_Dashboard.pdf');
// Restore visibility of hidden elements
elementsToHide.forEach(el => el.style.display = '');
}).catch(error => {
console.error('Error generating PDF:', error);
// Restore visibility of hidden elements in case of error
elementsToHide.forEach(el => el.style.display = '');
});
});
}
// Initial render of the dashboard when the page loads
renderDashboard();
});