No policies selected. Go back to the "Policy Selection" tab to choose policies.
`;
return;
}
this.selectedPolicies.forEach(key => {
const policy = this.policyTemplates[key];
// Initialize custom text if not already set
if (!this.customPolicyText[key]) {
this.customPolicyText[key] = this.personalizeText(policy.text);
}
const div = document.createElement('div');
div.innerHTML = `
`;
div.querySelector('textarea').addEventListener('change', e => {
this.customPolicyText[key] = e.target.value;
});
this.dom.policyCustomizationArea.appendChild(div);
});
},
renderHandbookPreview() {
if (this.selectedPolicies.size === 0) {
this.dom.handbookPreview.innerHTML = `
No policies selected. Your generated handbook will appear here.
`;
return;
}
let handbookHtml = `
${this.companyDetails.name || 'Company'} Employee Handbook
`;
this.selectedPolicies.forEach(key => {
handbookHtml += `
${this.policyTemplates[key].title}
${this.customPolicyText[key].replace(/\n/g, '
')}
`;
});
this.dom.handbookPreview.innerHTML = handbookHtml;
},
// --- ACTIONS ---
togglePolicy(key, isChecked) {
if (isChecked) {
this.selectedPolicies.add(key);
} else {
this.selectedPolicies.delete(key);
}
},
personalizeText(text) {
return text.replace(/\[COMPANY_NAME\]/g, this.companyDetails.name || 'Your Company Name');
},
// --- NAVIGATION ---
changeTab(tabIndex) {
// Validation before proceeding
if (tabIndex > this.currentTab) {
if (this.currentTab === 0 && (!this.companyDetails.name || !this.companyDetails.state)) {
alert("Please enter your company name and state to continue.");
return;
}
}
// Logic to run when entering a tab
if (tabIndex === 2) this.renderPolicyCustomization();
if (tabIndex === 3) this.renderHandbookPreview();
this.dom.tabButtons[this.currentTab].classList.remove('active');
this.dom.tabContents[this.currentTab].classList.remove('active');
this.currentTab = tabIndex;
this.dom.tabButtons[this.currentTab].classList.add('active');
this.dom.tabContents[this.currentTab].classList.add('active');
this.updateNavButtons();
},
navigateTabs(direction) {
const newTab = direction === 'next' ? this.currentTab + 1 : this.currentTab - 1;
if (newTab >= 0 && newTab < this.dom.tabButtons.length) {
this.changeTab(newTab);
}
},
updateNavButtons() {
this.dom.prevBtn.style.visibility = this.currentTab === 0 ? 'hidden' : 'visible';
this.dom.nextBtn.style.visibility = this.currentTab === this.dom.tabButtons.length - 1 ? 'hidden' : 'visible';
this.dom.pdfSection.style.display = this.currentTab === this.dom.tabButtons.length - 1 ? 'block' : 'none';
},
// --- PDF GENERATION ---
generatePdf() {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ unit: 'pt', format: 'a4' });
const content = document.getElementById('handbook-preview');
pdf.html(content, {
callback: function(doc) {
doc.save('Employee-Handbook.pdf');
},
x: 40,
y: 40,
width: 515, // A4 width in points is 595, leaving margins
windowWidth: content.scrollWidth
});
}
};
window.app = {
changeTab: app.changeTab.bind(app),
navigateTabs: app.navigateTabs.bind(app),
togglePolicy: app.togglePolicy.bind(app),
};
app.init();
});