(function(){
  const form = document.getElementById('changePasswordForm');
  const msgId = 'accountMsg';
  if (!form) return;

  form.addEventListener('submit', async function(event){
    event.preventDefault();
    const submit = form.querySelector('button[type="submit"]');
    if (submit) { submit.disabled = true; submit.textContent = 'Changing...'; }

    const data = Object.fromEntries(new FormData(form).entries());
    data.action = 'change_password';
    const res = await Pyrk.post('account.php', data);
    Pyrk.toast(msgId, res);

    if (res.ok) {
      form.reset();
    }

    if (submit) { submit.disabled = false; submit.textContent = 'Change password'; }
  });
})();
