function setButtonLoading(buttonId, isLoading, originalText = "Login") {
	const button = document.getElementById(buttonId);
	if (!button) return;

	if (isLoading) {
		button.disabled = true;
		button.innerHTML =
			'<span style="display: inline-block; margin-right: 8px;">⏳</span>Processing...';
		button.style.opacity = "0.7";
	} else {
		button.disabled = false;
		button.innerHTML = originalText;
		button.style.opacity = "1";
	}
}

// Handle login form submission
document.getElementById("loginForm").addEventListener("submit", async (e) => {
	e.preventDefault();

	const email = document.getElementById("email").value;
	const password = document.getElementById("password").value;

	setButtonLoading("loginBtn", true);

	const apiRequest = window.apiRequest; // Assuming apiRequest is a global variable or imported
	const API_ENDPOINTS = window.API_ENDPOINTS; // Assuming API_ENDPOINTS is a global variable or imported
	const setAuthToken = window.setAuthToken; // Assuming setAuthToken is a global variable or imported
	const setUserData = window.setUserData; // Assuming setUserData is a global variable or imported
	const showMessage = window.showMessage; // Assuming showMessage is a global variable or imported

	try {
		const response = await apiRequest(API_ENDPOINTS.LOGIN, {
			method: "POST",
			body: JSON.stringify({ email, password }),
		});

		// Save token and user data
		setAuthToken(response.data.token);
		setUserData(response.data.user);

		showMessage("Login successful", "success");

		// Redirect to home
		setTimeout(() => {
			window.location.href = "index.html";
		}, 1500);
	} catch (error) {
		showMessage(error.message || "Login failed", "error");
		setButtonLoading("loginBtn", false, "Login");
	}
});
