86 lines
4.0 KiB
JavaScript
86 lines
4.0 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
// Select all forms with the class lpa-form
|
|
const lpaForms = document.querySelectorAll('.lpa-form');
|
|
|
|
lpaForms.forEach(form => {
|
|
form.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
const submitBtn = document.activeElement && document.activeElement.type === 'submit' ? document.activeElement : form.querySelector('button[type="submit"]');
|
|
const originalBtnText = submitBtn.innerText;
|
|
|
|
// Show loading state
|
|
submitBtn.disabled = true;
|
|
const originalHTML = submitBtn.innerHTML;
|
|
submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span> Saving...';
|
|
|
|
const formData = new FormData(form);
|
|
|
|
// If the button clicked was one of the Step 2 buttons, ensure next_action is set
|
|
if (form.id === 'lpaFormStep2' && document.activeElement && document.activeElement.name === 'next_action') {
|
|
formData.set('next_action', document.activeElement.value);
|
|
}
|
|
|
|
fetch('api/save_lpa.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
// Show success state
|
|
submitBtn.classList.remove('btn-primary', 'btn-secondary', 'btn-outline-primary');
|
|
submitBtn.classList.add('btn-success');
|
|
submitBtn.innerHTML = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="me-2"><polyline points="20 6 9 17 4 12"></polyline></svg> Saved!';
|
|
|
|
setTimeout(() => {
|
|
// Go to next step or reload to add another
|
|
window.location.href = 'apply.php?step=' + data.next_step + '&id=' + data.id;
|
|
if (data.next_step === parseInt(new URLSearchParams(window.location.search).get('step'))) {
|
|
window.location.reload();
|
|
}
|
|
}, 800);
|
|
} else {
|
|
alert(data.error || 'An error occurred. Please try again.');
|
|
submitBtn.disabled = false;
|
|
submitBtn.innerHTML = originalHTML;
|
|
submitBtn.classList.remove('btn-success');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alert('Something went wrong. Please check your connection.');
|
|
submitBtn.disabled = false;
|
|
submitBtn.innerHTML = originalHTML;
|
|
});
|
|
});
|
|
});
|
|
|
|
// Handle attorney deletion
|
|
const deleteBtns = document.querySelectorAll('.btn-delete-attorney');
|
|
deleteBtns.forEach(btn => {
|
|
btn.addEventListener('click', function() {
|
|
if (confirm('Are you sure you want to remove this attorney?')) {
|
|
const attorneyId = btn.getAttribute('data-id');
|
|
const lpaId = btn.getAttribute('data-lpa-id') || new URLSearchParams(window.location.search).get('id');
|
|
|
|
const formData = new FormData();
|
|
formData.append('action', 'delete_attorney');
|
|
formData.append('attorney_id', attorneyId);
|
|
formData.append('lpa_id', lpaId);
|
|
|
|
fetch('api/save_lpa.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
window.location.reload();
|
|
} else {
|
|
alert(data.error || 'Failed to remove attorney.');
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}); |