<script lang="ts" setup>
		import Navbar from "../../components/Navbar.vue";
		import { ref } from "vue";
		import { useRouter } from 'vue-router'
		import RitzenbergenLib from "../../ritzenbergenlib";
		
		

		const router = useRouter()
		
		RitzenbergenLib.RitzenbergenLib.checkInternetConnection().then((result)=>{
				if(!result) router.push("/");
		});
		
		async function login(){
				const url=new URL(RitzenbergenLib.RitzenbergenLib.api("/admin/login.php"));
				url.searchParams.append("username",username.value);
				url.searchParams.append("password",password.value);
				const token=await fetch(url)
				.then((result)=>result.json())
				.then((result)=>{
						if(result.success) return result.token;
						else{
								window.alert(result.error);
								return "";
						}
				});
				if(token!="") router.push({ name: "adminpanel-main", params: { token } })
		}

		const username=ref("");
		const password=ref("");

		
</script>
<template>
		<br>
		<br>
		<br>
		<br>
		<br>
		<h1>Admin Panel</h1>
		<Navbar/>
		<form @submit.prevent="login()">
				<input type="text" placeholder="Benutzername" v-model="username"/>
				<input type="password" placeholder="Passwort" v-model="password"/>
				<input type="submit" value="Login"/>
		</form>

</template>
<style scoped></style>
