`;
ruleList.appendChild(ruleEl);
});
}
};
addRuleBtn.addEventListener('click', () => {
const condition = ruleCondition.value;
const value = ruleValue.value.trim();
const folder = ruleFolder.value.trim();
if (value && folder) {
rules.push({ condition, value, folder });
renderRules();
ruleValue.value = '';
ruleFolder.value = '';
} else {
// Simple feedback, could be a modal
alert('Please fill in both Value and Folder name.');
}
});
ruleList.addEventListener('click', (e) => {
if (e.target.closest('.delete-rule-btn')) {
const index = e.target.closest('.delete-rule-btn').dataset.index;
rules.splice(index, 1);
renderRules();
}
});
// --- Sorting Simulation ---
const parseEmail = (rawEmail) => {
const lines = rawEmail.split('\n');
const email = { sender: '', subject: '', body: '', raw: rawEmail };
let bodyStartIndex = -1;
lines.forEach((line, index) => {
if (line.toLowerCase().startsWith('from:')) {
email.sender = line.substring(5).trim();
} else if (line.toLowerCase().startsWith('subject:')) {
email.subject = line.substring(8).trim();
} else if (line.trim() === '' && bodyStartIndex === -1) {
bodyStartIndex = index + 1;
}
});
if (bodyStartIndex !== -1) {
email.body = lines.slice(bodyStartIndex).join('\n').trim();
} else {
// Fallback if no clear separation
email.body = rawEmail;
}
return email;
};
const runSimulation = () => {
const rawEmails = emailInput.value.split('---').map(e => e.trim()).filter(e => e);
const emails = rawEmails.map(parseEmail);
let sorted = { 'Unsorted': [] };
emails.forEach(email => {
let matched = false;
for (const rule of rules) {
const targetText = email[rule.condition] || '';
if (targetText.toLowerCase().includes(rule.value.toLowerCase())) {
if (!sorted[rule.folder]) {
sorted[rule.folder] = [];
}
sorted[rule.folder].push(email);
matched = true;
break; // First rule match wins
}
}
if (!matched) {
sorted['Unsorted'].push(email);
}
});
displayResults(sorted);
};
const displayResults = (sortedData) => {
sortedResults.innerHTML = '';
Object.keys(sortedData).forEach(folderName => {
const emailsInFolder = sortedData[folderName];
if (emailsInFolder.length === 0) return;
const folderEl = document.createElement('div');
folderEl.className = 'p-4 border rounded-lg bg-gray-50';
folderEl.innerHTML = `
${folderName} ${emailsInFolder.length}
${emailsInFolder.map(email => `
`).join('')}
`;
sortedResults.appendChild(folderEl);
});
};
// --- PDF Generation ---
downloadPdfButton.addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const margin = 15;
const pageWidth = doc.internal.pageSize.getWidth();
const usableWidth = pageWidth - (margin * 2);
let y = margin;
// Header
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.text('Email Sorting System Report', pageWidth / 2, y, { align: 'center' });
y += 15;
// Rules Section
doc.setFontSize(14);
doc.text('Applied Sorting Rules', margin, y);
y += 8;
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
if (rules.length > 0) {
rules.forEach(rule => {
const ruleText = `• IF ${rule.condition} contains "${rule.value}" THEN move to ${rule.folder}`;
const splitText = doc.splitTextToSize(ruleText, usableWidth);
if (y + (splitText.length * 5) > doc.internal.pageSize.getHeight() - margin) {
doc.addPage(); y = margin;
}
doc.text(splitText, margin, y);
y += (splitText.length * 4) + 2;
});
} else {
doc.text('No rules were applied.', margin, y);
y += 6;
}
y += 5;
doc.setDrawColor('#E5E7EB').line(margin, y, pageWidth - margin, y);
y += 10;
// Results Section
const resultFolders = sortedResults.querySelectorAll('.p-4.border.rounded-lg');
resultFolders.forEach(folderNode => {
const folderTitle = folderNode.querySelector('h3').textContent.trim();
const emailNodes = folderNode.querySelectorAll('.p-3.bg-white');
if (y + 10 > doc.internal.pageSize.getHeight() - margin) {
doc.addPage(); y = margin;
}
doc.setFont('helvetica', 'bold');
doc.setFontSize(14);
doc.text(folderTitle, margin, y);
y += 8;
emailNodes.forEach(emailNode => {
const from = emailNode.querySelector('p:nth-child(1)').textContent;
const subject = emailNode.querySelector('p:nth-child(2)').textContent;
const emailText = ` - ${from}\n ${subject}`;
const splitEmailText = doc.splitTextToSize(emailText, usableWidth - 5);
if (y + (splitEmailText.length * 5) > doc.internal.pageSize.getHeight() - margin) {
doc.addPage(); y = margin;
}
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
doc.text(splitEmailText, margin + 5, y);
y += (splitEmailText.length * 4) + 2;
});
y += 5;
});
doc.save('Email-Sorting-Report.pdf');
});
// --- Initial Setup ---
showTab(0);
renderRules();
});
From: ${email.sender || 'N/A'}
Subject: ${email.subject || 'N/A'}
Show body
${email.body || 'No body content.'}
