import RegisterArea from './register-area/register-area.vue'
import '../scss/main.scss'
import { createApp } from 'vue'
importAll(require.context('../images', false, /\.(png|jpe?g|svg)$/))

let heroText = document.querySelectorAll(".landing-hero h2,.landing-hero p")
let registerToggles = document.querySelectorAll(".register-btn, .register-area\
	.cancel-button, .services-cards button")
let token = getCookie('XSRF-TOKEN')
const app = createApp(RegisterArea).mount('#app')

function importAll(r) {
	return r.keys().map(r)
}

function getCookie(name) {
  var re = new RegExp(name + "=([^;]+)")
  var value = re.exec(document.cookie)

  return (value != null) ? unescape(value[1]) : null
}

function getToken() {
	fetch("/sanctum/csrf-cookie", {
		method: 'GET'
	}).then( () => {
		token = getCookie('XSRF-TOKEN')
	})
	app.token = token
}

function login(event) {
	fetch("/login", {
		method: 'POST',
		headers: {'Content-Type': 'application/json',
		'X-XSRF-TOKEN': token},
		body: JSON.stringify({"email":
			document.getElementById("login_email").value,
		"password": document.getElementById("login_password").value}),
	}).then(response => {
		console.log(response.json())
		console.log(response.ok)
		console.log(response.status)
		console.log(response.statusText)
	})
	event.preventDefault();
}

//Attempt to resend the verification link
function resendLink(event) {
	console.log("clicked");
	event.preventDefault();

}

function toggleNav() {
	heroText.forEach(item => {
		item.classList.toggle("hidden")
	})
	document.querySelector("nav form.login").classList.toggle("active")
	this.classList.toggle("toggled")
}

if (!token) {getToken()}

if (window.location.pathname == '/') {
	document.getElementById('nav_toggle').addEventListener('click', toggleNav)
	document.getElementById('login_form').addEventListener('submit', login)
	app.token = token
	// console.log(app.token)
	// console.log(app.$data.active)

	//Triggers for registration menu
	for (let i = 0; i < registerToggles.length; i++) {
		registerToggles[i].addEventListener("click", function() {
		document.querySelector(".register-area").classList.add("active")
		app.active = 'register'
		})
	}
	document.getElementById("forgot-password-btn").onclick = event => {
		document.querySelector(".register-area").classList.add("active")
		app.active = 'forgot'
		event.preventDefault()
	}

	//FAQ collapsibles
	let cols = document.getElementsByClassName("collapsible");

	for (let i = 0; i < cols.length; i++) {
		cols[i].addEventListener("click", function() {
			this.classList.toggle("active");
		});
	}
} else if (window.location.pathname == '/verify-email') {
	document.getElementById('resend_verification').addEventListener("click", resendLink)
}

