<script lang="ts">
	import type { PageData } from './$types';
	import { env } from '$env/dynamic/public';
	export let data: PageData;
	let settings = structuredClone(data.settings);

	async function change() {
		await fetch(`${env.PUBLIC_BASE_PATH}/admin/settings`, {
			method: 'POST',
			body: JSON.stringify({
				register: {
					enabled: returnIfNoDup(settings.register.enabled, data.settings.register.enabled)
				}
			} as PageData['settings'])
		});
		data.settings = settings;
		settings = structuredClone(data.settings);
	}

	function returnIfNoDup<T>(value: T, original: T): T | undefined {
		return value != original ? value : undefined;
	}
</script>

<div class="h-full flex flex-col items-center justify-between">
	<div class="grid grid-cols-3 w-full [&>*]:mx-8">
		<div>
			<div class="divider">Anmeldung</div>
			<label class="label cursor-pointer">
				<span class="label-text">Aktiviert</span>
				<input type="checkbox" class="toggle" bind:checked={settings.register.enabled} />
			</label>
		</div>
	</div>
	<div class="mb-6">
		<button
			class="btn btn-success mt-auto"
			class:btn-disabled={JSON.stringify(data.settings) === JSON.stringify(settings)}
			on:click={change}>Speichern</button
		>
	</div>
</div>
