<!DOCTYPE html>
<html lang="en">

<head>
	<title>Wazo SAML login</title>
</head>

<body>

<h1>Wazo SAML login</h1>

	<form id="saml", name="saml" method="post" action="https://stack.wazo.local/api/auth/0.1/saml/sso">
        <input id="redirect-url" name="redirect_url" value="https://app.wazo.local/postacs.html"/>
		<input id="domain" name="domain" value="example.com"/>
		<input id="login_btn" type="submit" value="Login par ici" />
	</form>

	<script>
		let form = document.getElementById("saml");
		form.addEventListener("submit", (e)=>{
			e.preventDefault();
			const body = {
				"redirect_url": document.getElementById("redirect-url").value,
				"domain": document.getElementById("domain").value,
			}
			console.log(body);
			fetch(
				form.getAttribute("action"), {
					method: 'POST',
					headers: {'Content-Type': 'application/json'},
					body: JSON.stringify(body),
				}
			).then(
				response => response.json()
			).then(
				data => {
					console.log(data);
					window.location.href = data.location;
					localStorage.setItem("wazo-saml-session-id", data.saml_session_id);
				}
			).catch((error) => console.error('Error:', error));
		})
	</script>

</body>
</html>
