<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Change Password</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      background-color: #f5f5f5;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    .container {
      width: 100%;
      max-width: 400px;
      background-color: #ffffff;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    h1 {
      text-align: center;
      color: #333;
      margin-bottom: 20px;
    }

    form {
      display: flex;
      flex-direction: column;
      gap: 15px;
    }

    label {
      font-size: 14px;
      color: #555;
    }

    input {
      padding: 10px;
      font-size: 14px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }

    button {
      padding: 10px 15px;
      background-color: #007BFF;
      color: #ffffff;
      border: none;
      border-radius: 4px;
      font-size: 16px;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    button:hover {
      background-color: #0056b3;
    }

    .error, .success {
      text-align: center;
      font-size: 14px;
      margin-top: 10px;
    }

    .error {
      color: #e74c3c;
    }

    .success {
      color: #2ecc71;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Change Password</h1>
    <form id="changePasswordForm">
      <label for="currentPassword">Current Password</label>
      <input type="password" id="currentPassword" name="currentPassword" required />

      <label for="newPassword">New Password</label>
      <input type="password" id="newPassword" name="newPassword" required />

      <button type="submit">Update Password</button>
    </form>
    <div id="responseMessage" class="response"></div>
  </div>

  <script>
    const API_URL = '/api/auth/change-password'; // Endpoint URL for change password

    document.getElementById('changePasswordForm').addEventListener('submit', async function (e) {
      e.preventDefault();

      const currentPassword = document.getElementById('currentPassword').value;
      const newPassword = document.getElementById('newPassword').value;

      try {
        const response = await fetch(API_URL, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({ currentPassword, newPassword }),
        });

        const result = await response.json();

        const responseMessage = document.getElementById('responseMessage');
        if (response.ok) {
          responseMessage.textContent = result.message;
          responseMessage.className = 'success';
          document.getElementById('changePasswordForm').reset(); // Clear the form
        } else {
          responseMessage.textContent = result.message || 'An error occurred';
          responseMessage.className = 'error';
        }
      } catch (error) {
        console.error('Error:', error);
        const responseMessage = document.getElementById('responseMessage');
        responseMessage.textContent = 'An error occurred while changing the password.';
        responseMessage.className = 'error';
      }
    });
  </script>
</body>
</html>
