`;
planOutput.innerHTML = html;
initialMessage.style.display = 'none';
planOutput.style.display = 'block';
pdfWrapper.style.display = 'block';
showTab(0); // Switch to dashboard
}
// --- PDF Generation ---
async function downloadPDF() {
try {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
let yPos = 20;
// Title
doc.setFontSize(20);
doc.setTextColor('#8338ec');
doc.text(state.episodeTitle, 14, yPos);
yPos += 15;
// Details
doc.setFontSize(12);
doc.setTextColor('#212529');
doc.text(`Host(s): ${state.hosts}`, 14, yPos);
yPos += 7;
if(state.guests) {
doc.text(`Guest(s): ${state.guests}`, 14, yPos);
yPos += 10;
}
// Segments
let adCounter = 0;
let totalRevenue = 0;
for (const segment of state.segments) {
if (yPos > 260) { // New page check
doc.addPage();
yPos = 20;
}
doc.setFontSize(14);
doc.setTextColor('#8338ec');
doc.text(segment.name, 14, yPos);
yPos += 8;
doc.setFontSize(11);
doc.setTextColor('#212529');
let segmentText = '';
if (segment.name.toLowerCase().includes('main')) {
segmentText = "Discussion Points:\n" + state.keyTopics.map(t => `- ${t}`).join('\n');
} else if (segment.name.toUpperCase() === 'AD') {
const sponsor = state.sponsors[adCounter % state.sponsors.length];
if (sponsor) {
segmentText = `Sponsor: ${sponsor.name}\nRate: $${sponsor.rate}\nScript: ${sponsor.script}`;
totalRevenue += sponsor.rate;
adCounter++;
}
} else {
segmentText = '(Content for this segment...)';
}
const splitText = doc.splitTextToSize(segmentText, 180);
doc.text(splitText, 14, yPos);
yPos += (splitText.length * 5) + 8;
}
doc.save(`${state.episodeTitle.replace(/\s/g, '_')}_Plan.pdf`);
} catch (e) {
console.error("PDF Generation Failed:", e);
alert("An error occurred generating the PDF. Make sure you are connected to the internet.");
}
}
// --- IV.C: Event Listeners ---
tabs.forEach((tab, index) => tab.addEventListener('click', () => showTab(index)));
nextBtn.addEventListener('click', () => showTab(currentTabIndex + 1));
prevBtn.addEventListener('click', () => showTab(currentTabIndex - 1));
generatePlanBtn.addEventListener('click', generatePlan);
addSegmentBtn.addEventListener('click', handleAddSegment);
segmentsList.addEventListener('click', handleSegmentActions);
addSponsorBtn.addEventListener('click', handleAddSponsor);
sponsorsList.addEventListener('click', handleDeleteSponsor);
// --- PDF Dependency Loader & Listener ---
(function loadPdfDependencies() {
const scripts = [
"https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.8.2/jspdf.plugin.autotable.min.js",
];
let scriptsLoaded = 0;
downloadPdfBtn.disabled = true;
downloadPdfBtn.title = "Loading PDF libraries...";
scripts.forEach(url => {
const script = document.createElement('script');
script.src = url;
script.async = true;
script.onload = () => {
scriptsLoaded++;
if (scriptsLoaded === scripts.length) {
downloadPdfBtn.disabled = false;
downloadPdfBtn.title = "Download Plan as PDF";
downloadPdfBtn.addEventListener('click', downloadPDF);
}
};
script.onerror = () => {
console.error(`Error loading script: ${url}`);
downloadPdfBtn.title = "PDF download disabled due to network error.";
};
document.head.appendChild(script);
});
})();
// --- Initial Setup ---
renderSegments();
renderSponsors();
showTab(0);
}
// --- START THE APP ---
initializePlanner();
})();
