`);
});
}
function renderAffiliateSelect() {
affiliateSelect.innerHTML = affiliates.map(s => `
`).join('');
}
// --- EVENT HANDLERS & FORMS ---
affiliateSelect.addEventListener('change', renderDashboard);
weightsForm.addEventListener('input', (e) => {
if (e.target.classList.contains('weight-input')) {
const key = e.target.dataset.key;
scoreWeights[key] = parseFloat(e.target.value) || 0;
updateTotalWeight();
renderAll();
}
});
affiliateForm.addEventListener('submit', (e) => {
e.preventDefault();
const data = {};
Object.keys(metricKeys).forEach(key => data[key] = parseFloat(document.getElementById(`affiliate-${key}`).value));
data.name = document.getElementById('affiliate-name').value;
if (editingAffiliateId) { Object.assign(affiliates.find(s => s.id === editingAffiliateId), data); } else { data.id = Date.now(); affiliates.push(data); }
resetAffiliateForm(); renderAll();
});
document.getElementById('affiliate-list').addEventListener('click', (e) => {
if (e.target.classList.contains('affiliate-edit')) setupEditForm(parseInt(e.target.dataset.id));
if (e.target.classList.contains('affiliate-delete')) deleteAffiliate(parseInt(e.target.dataset.id));
});
document.getElementById('affiliate-form').addEventListener('click', (e) => {
if (e.target.id === 'cancel-edit-btn') resetAffiliateForm();
});
function resetAffiliateForm() { affiliateForm.reset(); editingAffiliateId = null; document.getElementById('affiliate-form-title').textContent = 'Add New Affiliate'; document.getElementById('cancel-edit-btn').style.display = 'none'; }
function setupEditForm(id) { const s=affiliates.find(s=>s.id===id); if(!s)return; document.getElementById('affiliate-id').value=s.id; document.getElementById('affiliate-name').value=s.name; Object.keys(metricKeys).forEach(k=>document.getElementById(`affiliate-${k}`).value=s[k]); editingAffiliateId=id; document.getElementById('affiliate-form-title').textContent='Edit Affiliate'; document.getElementById('cancel-edit-btn').style.display='inline-block';}
function deleteAffiliate(id) { if (confirm('Delete affiliate?')) { affiliates = affiliates.filter(s => s.id !== id); renderAll(); } }
function updateTotalWeight() { const total = Object.values(scoreWeights).reduce((a, b) => a + b, 0); const el = document.getElementById('total-weight'); el.textContent = `${total}%`; el.style.color = total !== 100 ? '#ef4444' : '#22c55e'; }
downloadPdfBtn.addEventListener('click', generatePdf);
// --- TABS & NAVIGATION ---
window.switchTab = (tabName) => { currentTab = tabName; Object.values(tabBtns).forEach(b=>b.classList.replace('tab-active', 'tab-inactive')); Object.values(tabContents).forEach(c=>c.style.display='none'); tabBtns[tabName].classList.replace('tab-inactive', 'tab-active'); tabContents[tabName].style.display = 'block'; };
window.navigateTabs = (dir) => { if (dir==='next' && currentTab==='finder') switchTab('config'); else if (dir==='prev' && currentTab==='config') switchTab('finder'); };
// --- PDF GENERATION ---
async function generatePdf() {
const { jsPDF } = window.jspdf;
const pdfReportElement = document.getElementById('pdf-report');
const affiliate = affiliates.find(s => s.id === parseInt(affiliateSelect.value));
if (!affiliate) return alert('Please select an affiliate.');
const { score } = calculateScore(affiliate);
document.getElementById('pdf-date').textContent = new Date().toLocaleDateString('en-US');
document.getElementById('pdf-affiliate-name').textContent = affiliate.name;
document.getElementById('pdf-score').textContent = score.toFixed(0);
document.getElementById('pdf-assessment').textContent = document.getElementById('partner-assessment').textContent;
document.getElementById('pdf-score-card').style.backgroundColor = {'Excellent Fit': '#f0fdf4', 'Good Fit': '#fefce8', 'Consider Alternatives': '#fee2e2'}[document.getElementById('partner-assessment').textContent];
document.querySelector('#pdf-score-card p').style.color = document.getElementById('score-circle').style.borderColor;
const pdfTableBody = document.getElementById('pdf-table-body');
pdfTableBody.innerHTML = '';
Object.keys(metricKeys).forEach(key => {
const value = affiliate[key].toLocaleString();
pdfTableBody.insertAdjacentHTML('beforeend', `
| ${metricKeys[key].label} | ${value} | ${scoreWeights[key]}% |
`);
});
document.getElementById('pdf-chart-image').src = scoreChart.toBase64Image();
const canvas = await html2canvas(pdfReportElement, { scale: 2 });
const imgData = canvas.toDataURL('image/jpeg', 0.85);
const pdf = new jsPDF('p', 'mm', 'a4');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, pdfHeight);
pdf.save(`Affiliate-Analysis-${affiliate.name}.pdf`);
}
// --- INITIALIZATION ---
function init() {
scoreWeights = { audience: 20, engagement: 30, conversion: 40, relevance: 10 };
affiliates = [
{ id: 1, name: 'Tech Gadget Reviews (USA)', audience: 1200000, engagement: 8.5, conversion: 4.2, relevance: 5 },
{ id: 2, name: 'Modern Home Living (USA)', audience: 450000, engagement: 4.2, conversion: 2.1, relevance: 3 },
{ id: 3, name: 'Finance Unpacked (Blog)', audience: 80000, engagement: 2.5, conversion: 1.5, relevance: 2 },
{ id: 4, name: 'Gamer Central (Twitch)', audience: 750000, engagement: 9.5, conversion: 3.5, relevance: 4 }
];
renderAll();
}
init();
});