/** * TechPackHub Admin */ document.addEventListener('DOMContentLoaded', function () { const modal = document.getElementById('tph-client-modal'); if (!modal) { return; } const form = document.getElementById('tph-client-form'); const addButton = document.getElementById('tph-add-client'); const title = modal.querySelector('.tph-modal-header h2'); const submitButton = form.querySelector('[type="submit"]'); const hiddenID = document.getElementById('tph-client-id'); let editMode = false; /* |-------------------------------------------------------------------------- | Reset Form |-------------------------------------------------------------------------- */ function resetForm() { form.reset(); editMode = false; if (hiddenID) { hiddenID.value = ''; } title.textContent = 'Add Client'; submitButton.textContent = 'Create Client'; } /* |-------------------------------------------------------------------------- | Open Modal |-------------------------------------------------------------------------- */ if (addButton) { addButton.addEventListener('click', function () { resetForm(); modal.style.display = 'flex'; }); } /* |-------------------------------------------------------------------------- | Close Modal |-------------------------------------------------------------------------- */ modal.querySelectorAll( '.tph-modal-close, .tph-modal-overlay, .tph-modal-cancel' ).forEach(function (button) { button.addEventListener('click', function () { modal.style.display = 'none'; resetForm(); }); }); /* |-------------------------------------------------------------------------- | Edit Client |-------------------------------------------------------------------------- */ document.querySelectorAll('.tph-edit-client').forEach(function (button) { button.addEventListener('click', function () { fetch(tphAdmin.ajax_url, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ action: 'tph_get_client', nonce: tphAdmin.nonce, client_id: this.dataset.client }) }) .then(response => response.json()) .then(function (response) { if (!response.success) { alert(response.data.message); return; } const data = response.data; form.first_name.value = data.first_name; form.last_name.value = data.last_name; form.email.value = data.email; form.phone.value = data.phone; form.company.value = data.company; form.brand.value = data.brand; form.country.value = data.country; form.website.value = data.website; form.notes.value = data.notes; if (hiddenID) { hiddenID.value = data.id; } editMode = true; title.textContent = 'Edit Client'; submitButton.textContent = 'Update Client'; modal.style.display = 'flex'; }); }); }); /* |-------------------------------------------------------------------------- | Submit |-------------------------------------------------------------------------- */ form.addEventListener('submit', function (e) { e.preventDefault(); submitButton.disabled = true; submitButton.textContent = editMode ? 'Updating...' : 'Creating...'; const formData = new FormData(form); formData.append( 'action', editMode ? 'tph_update_client' : 'tph_create_client' ); formData.append( 'nonce', tphAdmin.nonce ); fetch(tphAdmin.ajax_url, { method: 'POST', body: formData }) .then(response => response.json()) .then(function (response) { submitButton.disabled = false; submitButton.textContent = editMode ? 'Update Client' : 'Create Client'; if (!response.success) { alert(response.data.message); return; } if (!editMode) { alert( "Client Created Successfully\n\n" + "Username: " + response.data.username + "\n" + "Password: " + response.data.password ); } else { alert('Client updated successfully.'); } modal.style.display = 'none'; resetForm(); location.reload(); }) .catch(function (error) { console.error(error); submitButton.disabled = false; submitButton.textContent = editMode ? 'Update Client' : 'Create Client'; alert('Something went wrong.'); }); }); }); https://techpackhub.com/post-sitemap.xml 2026-02-13T17:13:21+00:00 https://techpackhub.com/page-sitemap.xml 2026-08-04T07:30:05+00:00 https://techpackhub.com/rr-services-sitemap.xml 2026-08-04T07:37:30+00:00 https://techpackhub.com/category-sitemap.xml 2026-02-13T17:13:21+00:00