import { api } from '/static/js/api.js';

const form = document.getElementById('change-password-form');
const errorBox = document.getElementById('error');

form.addEventListener('submit', async (e) => {
  e.preventDefault();
  errorBox.textContent = '';
  const newPw = document.getElementById('new').value;
  if (newPw !== document.getElementById('confirm').value) {
    errorBox.textContent = 'New passwords do not match.';
    return;
  }
  try {
    await api('POST', '/api/v1/auth/change-password', {
      currentPassword: document.getElementById('current').value,
      newPassword: newPw,
    });
    window.location.href = '/';
  } catch (err) {
    errorBox.textContent = err.message;
  }
});
