document.addEventListener('DOMContentLoaded', function () { const experienceWrapper = document.getElementById('experience-wrapper'); const addExperienceBtn = document.getElementById('add-experience-btn'); let experienceCount = 0; const educationWrapper = document.getElementById('education-wrapper'); const addEducationBtn = document.getElementById('add-education-btn'); let educationCount = 0; // Function to add a new experience block const addExperience = () => { experienceCount++; const div = document.createElement('div'); div.classList.add('dynamic-section'); div.innerHTML = `
`; experienceWrapper.appendChild(div); }; // Function to add a new education block const addEducation = () => { educationCount++; const div = document.createElement('div'); div.classList.add('dynamic-section'); div.innerHTML = `
`; educationWrapper.appendChild(div); }; // Event listeners addExperienceBtn.addEventListener('click', addExperience); addEducationBtn.addEventListener('click', addEducation); // Remove button delegation experienceWrapper.addEventListener('click', (e) => { if (e.target.classList.contains('btn-remove')) { e.target.closest('.dynamic-section').remove(); } }); educationWrapper.addEventListener('click', (e) => { if (e.target.classList.contains('btn-remove')) { e.target.closest('.dynamic-section').remove(); } }); // Add one of each by default addExperience(); addEducation(); // Form Submission const resumeForm = document.getElementById('resume-form'); const loadingOverlay = document.getElementById('loading-overlay'); resumeForm.addEventListener('submit', function (e) { e.preventDefault(); loadingOverlay.classList.add('show'); const formData = new FormData(resumeForm); const data = { fullName: formData.get('fullName'), email: formData.get('email'), phone: formData.get('phone'), linkedin: formData.get('linkedin'), summary: formData.get('summary'), skills: formData.get('skills'), experience: [], education: [] }; // Process dynamic fields const expSections = experienceWrapper.querySelectorAll('.dynamic-section'); expSections.forEach((sec, index) => { data.experience.push({ title: sec.querySelector(`input[name='experience[${index + 1}][title]']`).value, company: sec.querySelector(`input[name='experience[${index + 1}][company]']`).value, startDate: sec.querySelector(`input[name='experience[${index + 1}][startDate]']`).value, endDate: sec.querySelector(`input[name='experience[${index + 1}][endDate]']`).value, responsibilities: sec.querySelector(`textarea[name='experience[${index + 1}][responsibilities]']`).value }); }); const eduSections = educationWrapper.querySelectorAll('.dynamic-section'); eduSections.forEach((sec, index) => { data.education.push({ degree: sec.querySelector(`input[name='education[${index + 1}][degree]']`).value, school: sec.querySelector(`input[name='education[${index + 1}][school]']`).value, startDate: sec.querySelector(`input[name='education[${index + 1}][startDate]']`).value, endDate: sec.querySelector(`input[name='education[${index + 1}][endDate]']`).value }); }); fetch('api/generate.php', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }) .then(response => response.json()) .then(result => { if (result.success) { window.location.href = 'result.php'; } else { alert('An error occurred: ' + result.message); loadingOverlay.classList.remove('show'); } }) .catch(error => { console.error('Error:', error); alert('A network error occurred. Please try again.'); loadingOverlay.classList.remove('show'); }); }); });