`;
return;
}
// Sort by days left (soonest first)
const processed = events.map(e => {
return { ...e, stats: calculateStats(e.date) };
}).sort((a, b) => a.stats.daysLeft - b.stats.daysLeft);
processed.forEach(e => {
const { daysLeft, yearsDiff, nextDate } = e.stats;
// Get Gift
let giftText = "";
if (e.type === 'Wedding') {
const trad = gifts[yearsDiff];
if (trad) giftText = `
`;
listContainer.appendChild(card);
});
renderDocList(processed);
}
function renderDocList(sortedEvents) {
docList.innerHTML = '';
sortedEvents.forEach(e => {
const { nextDate, yearsDiff } = e.stats;
const item = document.createElement('div');
item.className = 'at-doc-item';
item.innerHTML = `
Traditional Gift: ${trad}
`;
}
const card = document.createElement('div');
card.className = 'at-card';
card.innerHTML = `
${nextDate.toLocaleDateString(undefined, {month:'long', day:'numeric'})}
${e.names}
${e.type}
${daysLeft}
Days Until
${yearsDiff}${getOrdinal(yearsDiff)} Anniversary
${giftText}
${nextDate.toLocaleDateString(undefined, {month:'short', day:'numeric'})}
${e.names} (${e.type})
Turning ${yearsDiff} Years
`;
docList.appendChild(item);
});
}
function getOrdinal(n) {
const s = ["th", "st", "nd", "rd"];
const v = n % 100;
return s[(v - 20) % 10] || s[v] || s[0];
}
// --- Actions ---
window.addEvent = function() {
const names = document.getElementById('in-names').value;
const date = document.getElementById('in-date').value;
const type = document.getElementById('in-type').value;
if(!names || !date) {
alert("Please fill in names and date.");
return;
}
events.push({
id: Date.now(),
names,
date,
type
});
// Clear form
document.getElementById('in-names').value = '';
document.getElementById('in-date').value = '';
renderDashboard();
// Go to dashboard
document.querySelector('[data-tab="tab-dashboard"]').click();
};
window.deleteEvent = function(id) {
if(confirm("Remove this anniversary?")) {
events = events.filter(e => e.id !== id);
renderDashboard();
}
};
// --- Export ---
window.exportPDF = async function() {
const btn = document.querySelector('button[onclick="exportPDF()"]'); // Select specifically
const origText = btn.innerHTML;
btn.innerHTML = ' Generating...';
const element = document.getElementById('at-doc-render');
try {
const canvas = await html2canvas(element, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', 'a4');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
// Simple logic for single page fit
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, imgHeight);
pdf.save('Anniversary_Calendar.pdf');
} catch (err) {
console.error(err);
alert("Error generating PDF");
} finally {
btn.innerHTML = origText;
}
};
});
Turning ${yearsDiff} Years
