HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/CW-techs/wp-content/themes/cw-techs/js/account.js
jQuery(document).ready(function($) {
  $('#ChanagePasswordModal').on('hidden.bs.modal', function() {
		// Reset the form
		$('#password-form')[0].reset();
		$('#password-form').parsley().reset();
	});
  $('#editProfileModal').on('hidden.bs.modal', function() {
		// Reset the form
		$('#edit-profile-form')[0].reset();
		$('#edit-profile-form').parsley().reset();
	});
  // Initialize Parsley validation on the form
  $('#password-form').parsley();

  $('#password-form').on('submit', function(e) {
    e.preventDefault();

    if ($(this).parsley().isValid()) {
      var formData = $(this).serialize();

      $.ajax({
        url: ajax_obj.ajaxurl, // WordPress AJAX URL
        type: "POST",
        data: formData + '&action=change_user_password', // include action
        beforeSend: function() {
          // Optional: disable submit button or show loader
          $('#password-form button[type="submit"]').prop('disabled', true);
        },
        beforeSend: function() {
            $("#chngepswd-btn").prop("disabled", true).text("Submitting...");
        },
        success: function(response) {
          $("#chngepswd-btn").prop("disabled", false).text("Submit");
          // Enable button again
          console.log('Redirect URL:', response.data.logout_url);
          $('#password-form button[type="submit"]').prop('disabled', false);

          if (response.success) {
            // Hide any open modal
            $('.modal.show').modal('hide');

            // Show success modal
            // setTimeout(function() {
            //   $('#removeModal').modal('show');
            // }, 500);


            const message = response.data.message;
            const logoutUrl = response.data.logout_url;

            $('#removeModal').modal('show');

            setTimeout(function() {
            window.location.href = logoutUrl;
          }, 3000);
          } else {
            toastr.error(response.data.message);
            $('#password-form')[0].reset();
            $('#password-form').parsley().reset();
          }
        },
        error: function(xhr) {
          $('#password-form button[type="submit"]').prop('disabled', false);
          alert('Something went wrong.');
        }
      });
    }
  });
});
jQuery(document).ready(function($) {

  $('#edit-profile-form').parsley();

  $('#edit-profile-form').on('submit', function(e) {
    e.preventDefault();

    if ($(this).parsley().isValid()) {

      var formData = $(this).serialize();

      $.ajax({
        type: 'POST',
        url: ajax_obj.ajaxurl, // WordPress will provide this object
        data: {
          action: 'update_user_profile',
          form_data: formData
        },
        beforeSend: function () {
          $("#profile-btn").prop("disabled", true).text("Submitting...");
        },
        success: function(response) {
          $("#profile-btn").prop("disabled", false).text("Submit");
          if (response.success) {
            toastr.success('Profile updated successfully!');
            $('#editProfileModal').modal('hide');
            setTimeout(function(){
                location.reload();
            }, 1500);
          } else {
            toastr.error(response.data);
          }
        },
        error: function() {
          toastr.error('Something went wrong.');
        }
      });

    }
  });

});

jQuery(document).ready(function($) {
    $('#img').on('change', function() {
        var file_data = $('#img').prop('files')[0];
        var form_data = new FormData();
        form_data.append('profile_image', file_data);
        form_data.append('action', 'upload_profile_image');
        // form_data.append('_ajax_nonce', profile_image_vars.nonce);

        $.ajax({
            url: ajax_obj.ajaxurl,
            type: 'POST',
            data: form_data,
            contentType: false,
            processData: false,
            success: function(response) {
                if (response.success) {
                    $('#profile-picture').attr('src', response.data.image_url);
                    toastr.success('Profile image updated');
                } else {
                    toastr.error(response.data.message);
                }
            }
        });
    });
});
jQuery(document).ready(function($) {
    $('#confirmDeleteAccountBtn').on('click', function() {
        // if (confirm('Are you absolutely sure? This cannot be undone.')) {
            $.ajax({
                url: ajax_obj.ajaxurl,
                type: 'POST',
                data: {
                    action: 'delete_user_account'
                },
                beforeSend: function() {
                    $('#confirmDeleteAccountBtn').prop('disabled', true).text('Deleting...');
                },
                success: function(response) {
                    if (response.success) {
                        toastr.success('Account successfully deleted.');
                        window.location.href = ajax_obj.redirect_url;
                    } else {
                        toastr.error(response.data.message);
                        $('#confirmDeleteAccountBtn').prop('disabled', false).text('Remove');
                    }
                }
            });
        // }
    });
});


jQuery(document).ready(function($) {
  // Add New
  $('.add-addrs-btn').on('click', function() {
    $('#address-form')[0].reset();
    $('#address_index').val('');
    $('#address-modal-title').text('Add');
    $('#addressModal').modal('show');
  });

  // Edit
  $(document).on('click', '.update-addrs-btn[data-index]', function(e) {
    e.preventDefault();
    const data = $(this).data('address');
    const index = $(this).data('index');
    $('#address-modal-title').text('Edit');
    $('#address_index').val(index);
    for (let key in data) {
      $('#address-form [name="' + key + '"]').val(data[key]);
    }
    $('#addressModal').modal('show');
  });

  // Save Address
  $('#address-form').on('submit', function(e) {
    e.preventDefault();
    if (!$(this).parsley().isValid()) return;

    const formData = $(this).serialize();
    $("#address-btn").prop("disabled", true).text("Saving...");
    $.post(ajax_obj.ajaxurl, {
      action: 'save_user_address',
      data: formData
    }, function(res) {
      $("#address-btn").prop("disabled", false).text("Save");
      if (res.success) {
        toastr.success('Address saved successfully');
        setTimeout(() => location.reload(), 1000);
      } else {
        toastr.error(res.data.message || 'Failed to save');
      }
    });
  });

  // Delete
  // $(document).on('click', '.delete-addrs-btn', function(e) {
  //   e.preventDefault();
  //   if (!confirm('Delete this address?')) return;
  //   const index = $(this).data('index');

  //   $.post(ajax_obj.ajaxurl, {
  //     action: 'delete_user_address',
  //     index: index
  //   }, function(res) {
  //     if (res.success) {
  //       toastr.success('Address deleted');
  //       setTimeout(() => location.reload(), 800);
  //     } else {
  //       toastr.error('Delete failed');
  //     }
  //   });
  // });
  $('#confirmDeleteAddressBtn').on('click', function() {
        // if (confirm('Are you absolutely sure? This cannot be undone.')) {
            $.ajax({
                url: ajax_obj.ajaxurl,
                type: 'POST',
                data: {
                    action: 'delete_user_address'
                },
                beforeSend: function() {
                    $('#confirmDeleteAddressBtn').prop('disabled', true).text('Deleting...');
                },
                success: function(response) {
                    if (response.success) {
                       toastr.success('Address deleted');
                        setTimeout(() => location.reload(), 800);
                    } else {
                        toastr.error(response.data.message);
                        $('#confirmDeleteAddressBtn').prop('disabled', false).text('Remove');
                    }
                }
            });
        // }
    });
});